Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

facet_grid with an expression cannot have margins #1864

Open
has2k1 opened this issue Oct 20, 2016 · 6 comments · May be fixed by #5944
Open

facet_grid with an expression cannot have margins #1864

has2k1 opened this issue Oct 20, 2016 · 6 comments · May be fixed by #5944
Labels
facets 💎 feature a feature request or enhancement

Comments

@has2k1
Copy link
Contributor

has2k1 commented Oct 20, 2016

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.

@DanRuderman
Copy link
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

@has2k1
Copy link
Contributor Author

has2k1 commented Oct 23, 2016

@DanRuderman, you need to pull from master.

@DanRuderman
Copy link
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)

@has2k1
Copy link
Contributor Author

has2k1 commented Oct 24, 2016

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.

@hadley hadley added the feature a feature request or enhancement label Oct 24, 2016
@paleolimbot
Copy link
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).

@dmurdoch
Copy link
Contributor

dmurdoch commented Apr 5, 2020

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
Labels
facets 💎 feature a feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants