Skip to content

facet_grid with an expression cannot have margins #1864

Closed

Description

@has2k1

A visual of the issue,

ggplot(mtcars, aes(wt, mpg)) + geom_point() + facet_grid(vs~I(am+1), margins=T)

grid-expression-margin

The column (expression variable) margin is missing data!

The problem arises because these two lines depend on each other. The margins must be added with all the facet variables present in the data, yet the facet values must be computed with margins already computed and present in the data.

Activity

  1. DanRuderman commented on Oct 21, 2016

    @DanRuderman
    Contributor

    The relevant code (in facet-grid-.r) seems to be:

    margin_vars <- list(intersect(names(rows), names(data)),intersect(names(cols), names(data)))
    data <- reshape2::add_margins(data, margin_vars, params$margins)
    facet_vals <- quoted_df(data, c(rows, cols), params$plot_env)
    

    The call to quoted_df() handles the expression evaluation needed to add the values that are missing, but this comes after the call to add_margins().

    The possible solution I found is to replace those lines with:

    data <- quoted_df(data, as.quoted(unique(c(names(data), names(rows), names(cols)))), params$plot_env)
    margin_vars <- list(intersect(names(rows), names(data)),intersect(names(cols), names(data)))
    data <- reshape2::add_margins(data, margin_vars, params$margins)
    sym <- do.call(c, lapply(c(rows, cols), function(x) as.quoted(as.symbol(as.character(as.expression(x))))))
    facet_vals <- quoted_df(data, sym, params$plot_env)
    

    Since I don't have much facility with conversion from calls to expressions to quoted symbols, I'm sure there is a prettier way. But the idea is to first add in to the data frame data columns representing the faceting variables when they are expressions (thus the first call to quoted_df()).

    @hadley Let me know if this seems like the right direction.

    Thanks

  2. has2k1 commented on Oct 23, 2016

    @has2k1
    ContributorAuthor

    @DanRuderman, you need to pull from master.

  3. DanRuderman commented on Oct 24, 2016

    @DanRuderman
    Contributor

    @has2k1 You're right. But why does % git status give output that makes it look like I'm up to date?
    On branch master
    Your branch is up-to-date with 'hadley/master'.

    The relevant output from % git remote -v is:
    hadley https://github.com/hadley/ggplot2.git (fetch)
    hadley https://github.com/hadley/ggplot2.git (push)

  4. has2k1 commented on Oct 24, 2016

    @has2k1
    ContributorAuthor

    git status is only as accurate as the state created by last git fetch. git status on its own does not initiate a network connection and so it does not show "real time" information.

  5. added
    feature
    on Oct 24, 2016
  6. paleolimbot commented on Jul 7, 2019

    @paleolimbot
    Member

    After playing with this for a while, I'm not sure this can be done without completely rewriting how facets map data. For a few reasons, this may be a good thing (#2689 comes to mind).

  7. dmurdoch commented on Apr 5, 2020

    @dmurdoch
    Contributor

    Would it be possible to detect this and issue a warning? Or at least document it in the help page?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    facets 💎feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions