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

facet_wrap() doesn't play well with expressions in facets. #2123

Open
eliocamp opened this Issue May 5, 2017 · 0 comments

Comments

Projects
None yet
2 participants

eliocamp commented May 5, 2017

facet_wrap() doesn't get along with faceting by function. It works sometimes, but not always and also the error messages are not informative. For example, it breaks if I add annotations. This is a minimal example of what I'm talking about:

library(ggplot2)
library(lubridate)
library(stringi)

dates <- as.Date(c("2015-01-01", "2016-03-01"))
df <- expand.grid(x = 1:10, date = dates)
df$y <- rnorm(nrow(df))
df$yearnum <- year(df$date)

g1 <- ggplot(df, aes(x, y)) +
    geom_line()
g2 <- g1 + annotate(geom = "text", x = 5, y = 0, label = "test") 


g1 + facet_wrap(~year(date))    # works

g2 + facet_wrap(~year(date))    # error
#> Error in as.POSIXlt.default(x, tz = tz(x)): do not know how to convert 'x' to class "POSIXlt"
g2 + facet_wrap(~yearnum)    # works fine

Is not only a problem with date

g1 + facet_wrap(~sqrt(yearnum))    # works

g2 + facet_wrap(~sqrt(yearnum))    # error
#> Error in eval(var, envir = data, enclos = env): object 'yearnum' not found

You get the same behaviour if you use geoms

df2 <- data.frame(x = 1:10, y = rnorm(10))
g1 + geom_point(data = df2) + facet_wrap(~yearnum)    # works

g1 + geom_point(data = df2) + facet_wrap(~year(date))    # error
#> Error in as.POSIXlt.default(x, tz = tz(x)): do not know how to convert 'x' to class "POSIXlt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment