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

Should facet + empty spec be a noop? #3070

Closed
lionel- opened this issue Jan 16, 2019 · 12 comments · Fixed by #3162
Closed

Should facet + empty spec be a noop? #3070

lionel- opened this issue Jan 16, 2019 · 12 comments · Fixed by #3162

Comments

@lionel-
Copy link
Member

lionel- commented Jan 16, 2019

Should these be equivalent to ggplot(mtcars) instead of failing?

ggplot(mtcars) + facet_wrap(vars())
#> Error: Can't subset with `[` using an object of class NULL.

ggplot(mtcars) + facet_wrap(NULL)
#> Error: Must specify at least one variable to facet by

Allowing empty specs would make sense in functions:

my_plot <- function(.data, .mapping, ...) {
  ggplot(.data, .mapping) +
    geom_point() +
    facet_wrap(vars(...))
}

Then both of these would work, instead of just the latter:

my_plot(starwars, aes(mass, height / 100))

my_plot(starwars, aes(mass, height / 100), gender)
@thomasp85
Copy link
Member

I think the error messages should be better but I don’t think facet_wrap should change itself to facet_null if the input is lacking.

@lionel-
Copy link
Member Author

lionel- commented Jan 17, 2019

Why is an error better? I think it makes sense not to error because NULL/empty facets should only occur in programmatic cases. Handling the empty case sensibly makes the layer more composable and generally useful.

@yutannihilation
Copy link
Member

I think this makes sense, considering facet_grid() generates at least 1 row/col even when there are no variables for row/col.

@thomasp85
Copy link
Member

But in facet_grid you ask for this explicitly by providing a . at either side of the formula...

I think my major gripe is that this can lead to situation where users mistype the name of the faceting variable and then get this single panel view without any errors or warnings... but @hadley should be the judge of that...

@lionel-
Copy link
Member Author

lionel- commented Jan 24, 2019

You should normally get an "object not found" error in the case of an expression, or an "At least one layer must contain all faceting variables" error in the case of a bare symbol.

Related issue: #2986

@thomasp85
Copy link
Member

Ah - this is purely for NULL...

@thomasp85
Copy link
Member

thomasp85 commented Jan 24, 2019

I don't think

my_plot <- function(.data, .mapping, ...) {
  facet_vars <- vars(...)
  p <- ggplot(.data, .mapping) +
    geom_point()

  if (length(facet_vars) != 0) {
    p <- p + facet_wrap(facet_vars)
  }
  p
}

is too complicated, but of course accepting NULL will be more magical. I just tend to dislike objects changing class (FacetWrap -> FacetNull) based on the input data... ¯\(o_O)

@lionel-
Copy link
Member Author

lionel- commented Jan 24, 2019

I don't think it's changing class. An empty FacetWrap behaves as a FacetNull, which makes sense to me. Just like character(0) behaves as NULL in some contexts such as concatenation.

@thomasp85
Copy link
Member

For all intend and purpose it will change class as it will behave just like FacetNull... I'd expect FacetWrap to create panels with strips etc., which will not happen as there is not anything to put in them...

I dunno... I'm against, but not vehemently, so if anyone else weighs in in favour I'll concede...

@yutannihilation
Copy link
Member

But in facet_grid you ask for this explicitly by providing a . at either side of the formula...

You know, it's "for compatibility with the classic interface" as the doc says. Now we have rows and cols and their defaults are NULL.

@thomasp85
Copy link
Member

fair point...

@lock
Copy link

lock bot commented Oct 8, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Oct 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants