Skip to content

Conversation

@mitchelloharawild
Copy link
Contributor

Resolves #4379

Piping a plot into a layer() gives a helpful error with a hint:

ggplot(diamonds) %>% 
  geom_point(aes(price, carat))
#> Error: `mapping` must be created by `aes()`
#> Did you use %>% instead of +?

This pull request adds this hint and better captures the error when piping into facets:

Before

library(ggplot2)
library(magrittr)
ggplot(diamonds) %>% 
  facet_grid(vars(cut))
#> Error: `rows` must be `NULL` or a `vars()` list if `cols` is a `vars()` list
ggplot(diamonds) %>% 
  facet_grid()
#> <ggproto object: Class FacetGrid, Facet, gg>
#>     compute_layout: function
#>     draw_back: function
#>     draw_front: function
#>     draw_labels: function
#>     draw_panels: function
#>     finish_data: function
#>     init_scales: function
#>     map_data: function
#>     params: list
#>     setup_data: function
#>     setup_params: function
#>     shrink: TRUE
#>     train_scales: function
#>     vars: function
#>     super:  <ggproto object: Class FacetGrid, Facet, gg>
ggplot(diamonds) %>% 
  facet_wrap(vars(cut))
#> Warning: Coercing `nrow` to be an integer.
#> Error in sanitise_dim(nrow): 'list' object cannot be coerced to type 'integer'
ggplot(diamonds) %>% 
  facet_wrap()
#> <ggproto object: Class FacetWrap, Facet, gg>
#>     compute_layout: function
#>     draw_back: function
#>     draw_front: function
#>     draw_labels: function
#>     draw_panels: function
#>     finish_data: function
#>     init_scales: function
#>     map_data: function
#>     params: list
#>     setup_data: function
#>     setup_params: function
#>     shrink: TRUE
#>     train_scales: function
#>     vars: function
#>     super:  <ggproto object: Class FacetWrap, Facet, gg>

Created on 2021-03-23 by the reprex package (v1.0.0)

After

library(ggplot2)
library(magrittr)
ggplot(diamonds) %>% 
  facet_grid(vars(cut))
#> Error: `rows` must be `NULL` or a `vars()` list if `cols` is a `vars()` list
#> Did you use %>% instead of +?
ggplot(diamonds) %>% 
  facet_grid()
#> Error: Please use `vars()` to supply facet variables
#> Did you use %>% instead of +?
ggplot(diamonds) %>% 
  facet_wrap(vars(cut))
#> Error: Please use `vars()` to supply facet variables
#> Did you use %>% instead of +?
ggplot(diamonds) %>% 
  facet_wrap()
#> Error: Please use `vars()` to supply facet variables
#> Did you use %>% instead of +?

Created on 2021-03-23 by the reprex package (v1.0.0)

Copy link
Member

@yutannihilation yutannihilation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks perfect, thanks!

@yutannihilation yutannihilation merged commit 1d8483a into tidyverse:master Mar 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Uninformative error for piping into facets

2 participants