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

Facetting fails with "undefined columns selected" error for non-standard variable names #1067

Closed
richierocks opened this Issue Mar 12, 2015 · 2 comments

Comments

Projects
None yet
2 participants
@richierocks
Contributor

richierocks commented Mar 12, 2015

To reproduce:

library(ggplot2)

DF <- data.frame(
  x = 1:10,
  y = runif(10),
  `z z` = gl(5, 2),
  check.names = FALSE
)

ggplot(DF, aes(x, y)) +
  geom_point() +
  facet_wrap(~ `z z`)
## Error in `[.data.frame`(panels, missing_facets) : 
##   undefined columns selected

ggplot(DF, aes(x, y)) +
  geom_point() +
  facet_grid(`z z` ~ .)
## Error in `[.data.frame`(base, names(rows)) : undefined columns selected
@smouksassi

This comment has been minimized.

smouksassi commented Mar 12, 2015

you might want to have a look at:
http://stackoverflow.com/questions/12744282/how-to-deal-with-spaces-in-column-names
the back tick works in aesthetics but not in facets:
DF <- data.frame(
x = 1:10,
y y = runif(10),
z z = gl(5, 2),
check.names = FALSE
)
ggplot(DF, aes(x, y y)) +
geom_point()

@richierocks

This comment has been minimized.

Contributor

richierocks commented Mar 12, 2015

Thanks Samer.

It was the fact that backticks work in aesthetics that led me to believe that they should work in facets too.

For the record, using the standardised version of the column name doesn't work either.

ggplot(DF, aes(x, y)) +
  geom_point() +
  facet_wrap(~ z.z)

If no proper fix is possible (that is, making non-standard names correctly draw facets), then It would be nice to provide a more informative error message.

For example, at the start of facet_wrap, you can check for valid variable names. If you are happy to have a dependency on assertive, then this will do the trick:

library(assertive)
assert_all_are_valid_variable_names(attr(terms(facets), "term.labels"))

(The case for facet_grid is slightly harder because stats::terms is silly and you have to get the LHS separately.)

A note in the ?facet_wrap/?facet_grid help pages might be nice too.

@hadley hadley closed this in c452194 Jun 11, 2015

@lock lock bot locked as resolved and limited conversation to collaborators Jun 20, 2018

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