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

Already on GitHub? Sign in to your account

Adding geom_hline with boolean logic in facet_wrap causes failure #2091

Open
billdenney opened this Issue Mar 29, 2017 · 4 comments

Comments

Projects
None yet
3 participants

When I have logic in the equation for facet_wrap (like facet_wrap(~(Z < 0.5))), it works fine without a geom_hline modifier, but with the modifier, it gets an error that "object 'Z' not found.

  library(ggplot2)
  d <- data.frame(X=rnorm(10),
                  Y=rnorm(10),
                  Z=rnorm(10))
  ggplot(d, aes(x=X, y=Y)) +
    geom_point() +
    facet_wrap(~(Z < 0.5))
  # Plot appears as expected
  ggplot(d, aes(x=X, y=Y)) +
    geom_point() +
    facet_wrap(~(Z < 0.5)) +
    geom_hline(yintercept=0)
  # Error in eval(expr, envir, enclos) : object 'Z' not found
Contributor

has2k1 commented Mar 29, 2017

Try this geom_hline(yintercept=0, inherit.aes=TRUE)

geom_hline doesn't appear to have an inherit.aes argument though it is mentioned in the documentation (perhaps that's another bug?):

library(ggplot2)
d <- data.frame(X=rnorm(10),
                Y=rnorm(10),
                Z=rnorm(10))
ggplot(d, aes(x=X, y=Y)) +
  geom_point() +
  facet_wrap(~(Z < 0.5))
# Plot appears as expected
ggplot(d, aes(x=X, y=Y)) +
  geom_point() +
  facet_wrap(~(Z < 0.5)) +
  geom_hline(yintercept=0, inherit.aes=TRUE)
# Warning: Ignoring unknown parameters: inherit.aes
# Error in eval(expr, envir, enclos) : object 'Z' not found
Contributor

has2k1 commented Mar 29, 2017

What about this stat_identity(yintercept=0, geom='hline', inherit.aes=TRUE)

stat_identity works and is a reasonable work-around. Thanks for the suggestion!

If possible, it would be nice to have geom_hline work directly in the next version of ggplot2.

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