Skip to content

facet_wrap() with two facets and reference layer that includes only one facet variable fails #2810

@paleolimbot

Description

@paleolimbot

I admit this is a pretty niche problem, but it's also been observed in the wild. Basically, when using facet_wrap() with two facet variables, if another layer only has one of those two variables, the plot fails to build. If the new layer has both variables, it is no problem. The same behaivour with facet_grid() does not fail, and additional layers with both facet variables also work.

library(ggplot2)
library(dplyr)

p <- mtcars %>%
  mutate(
    cyl_fct = paste("cyl =", cyl),
    gear_fct = paste("gear =", gear)
  ) %>%
  ggplot(aes(wt, mpg)) +
  geom_point()

bad_geom <- geom_point(
  aes(x = 5, y = mpg_line),
  data = data.frame(
    cyl_fct = c("cyl = 4", "cyl = 6", "cyl = 8"),
    mpg_line = c(15, 25, 35),
    stringsAsFactors = FALSE
  ),
  col = "red"
)

bad_geom2 <- geom_point(
  aes(x = 5, y = mpg_line),
  data = data.frame(
    gear_fct = c("gear = 3", "gear = 4", "gear = 5"),
    mpg_line = c(15, 25, 35),
    stringsAsFactors = FALSE
  ),
  col = "red"
)

good_geom <- geom_point(
  aes(x = 5, y = mpg_line),
  data = data.frame(
    cyl_fct = c("cyl = 4", "cyl = 6", "cyl = 8"),
    gear_fct = c("gear = 3", "gear = 4", "gear = 5"),
    mpg_line = c(15, 25, 35),
    stringsAsFactors = FALSE
  ),
  col = "green"
)

# fails
p + bad_geom + facet_wrap(vars(cyl_fct, gear_fct))
#> Error in gList(structure(list(x = structure(0.5, class = "unit", valid.unit = 0L, unit = "npc"), : only 'grobs' allowed in "gList"
p + bad_geom2 + facet_wrap(vars(cyl_fct, gear_fct))
#> Error in gList(structure(list(x = structure(0.5, class = "unit", valid.unit = 0L, unit = "npc"), : only 'grobs' allowed in "gList"

# works
p + good_geom + facet_wrap(vars(cyl_fct, gear_fct))
p + bad_geom + facet_wrap(vars(cyl_fct))
p + bad_geom2 + facet_wrap(vars(gear_fct))
p + bad_geom + facet_grid(vars(cyl_fct), vars(gear_fct))
p + bad_geom + facet_grid(rows = vars(cyl_fct, gear_fct))
p + bad_geom + facet_grid(cols = vars(cyl_fct, gear_fct))

Created on 2018-08-07 by the reprex
package
(v0.2.0).

Session info
devtools::session_info()
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.4.3 (2017-11-30)
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_CA.UTF-8                 
#>  tz       America/New_York            
#>  date     2018-08-07
#> Packages -----------------------------------------------------------------
#>  package    * version   date       source                          
#>  assertthat   0.2.0     2017-04-11 CRAN (R 3.4.0)                  
#>  backports    1.1.2     2017-12-13 CRAN (R 3.4.3)                  
#>  base       * 3.4.3     2017-12-07 local                           
#>  bindr        0.1.1     2018-03-13 cran (@0.1.1)                   
#>  bindrcpp   * 0.2.2     2018-03-29 CRAN (R 3.4.4)                  
#>  bitops       1.0-6     2013-08-17 CRAN (R 3.4.0)                  
#>  colorspace   1.3-2     2016-12-14 CRAN (R 3.4.0)                  
#>  compiler     3.4.3     2017-12-07 local                           
#>  datasets   * 3.4.3     2017-12-07 local                           
#>  devtools     1.13.4    2017-11-09 CRAN (R 3.4.2)                  
#>  digest       0.6.15    2018-01-28 cran (@0.6.15)                  
#>  dplyr      * 0.7.6     2018-06-29 CRAN (R 3.4.4)                  
#>  evaluate     0.10.1    2017-06-24 CRAN (R 3.4.1)                  
#>  ggplot2    * 3.0.0     2018-07-03 CRAN (R 3.4.4)                  
#>  glue         1.3.0     2018-07-17 cran (@1.3.0)                   
#>  graphics   * 3.4.3     2017-12-07 local                           
#>  grDevices  * 3.4.3     2017-12-07 local                           
#>  grid         3.4.3     2017-12-07 local                           
#>  gtable       0.2.0     2016-02-26 CRAN (R 3.4.0)                  
#>  htmltools    0.3.6     2017-04-28 CRAN (R 3.4.0)                  
#>  knitr        1.17      2017-08-10 CRAN (R 3.4.1)                  
#>  labeling     0.3       2014-08-23 CRAN (R 3.4.0)                  
#>  lazyeval     0.2.1     2017-10-29 CRAN (R 3.4.2)                  
#>  magrittr     1.5       2014-11-22 CRAN (R 3.4.0)                  
#>  memoise      1.1.0     2017-04-21 CRAN (R 3.4.0)                  
#>  methods    * 3.4.3     2017-12-07 local                           
#>  munsell      0.5.0     2018-06-12 cran (@0.5.0)                   
#>  pillar       1.1.0     2018-01-14 cran (@1.1.0)                   
#>  pkgconfig    2.0.1     2017-03-21 CRAN (R 3.4.0)                  
#>  plyr         1.8.4     2016-06-08 CRAN (R 3.4.0)                  
#>  purrr        0.2.5     2018-05-29 CRAN (R 3.4.4)                  
#>  R6           2.2.2     2017-06-17 CRAN (R 3.4.0)                  
#>  Rcpp         0.12.18   2018-07-23 cran (@0.12.18)                 
#>  RCurl        1.95-4.10 2018-01-04 CRAN (R 3.4.3)                  
#>  reshape2     1.4.3     2017-12-11 CRAN (R 3.4.3)                  
#>  rlang        0.2.1     2018-05-30 CRAN (R 3.4.4)                  
#>  rmarkdown    1.8       2017-11-17 CRAN (R 3.4.2)                  
#>  rprojroot    1.3-2     2018-01-03 cran (@1.3-2)                   
#>  scales       1.0.0     2018-08-06 Github (hadley/scales@9b82318)  
#>  stats      * 3.4.3     2017-12-07 local                           
#>  stringi      1.2.4     2018-07-20 cran (@1.2.4)                   
#>  stringr      1.3.1     2018-05-10 CRAN (R 3.4.4)                  
#>  tibble       1.4.2     2018-01-22 cran (@1.4.2)                   
#>  tidyselect   0.2.4     2018-02-26 cran (@0.2.4)                   
#>  tools        3.4.3     2017-12-07 local                           
#>  utils      * 3.4.3     2017-12-07 local                           
#>  withr        2.1.2     2018-07-31 Github (jimhester/withr@fe56f20)
#>  XML          3.98-1.9  2017-06-19 CRAN (R 3.4.1)                  
#>  yaml         2.1.16    2017-12-12 CRAN (R 3.4.3)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions