Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upWarn about discouraged aes() usage during plot build #3346
Conversation
|
I don't see a good reason to issue warnings on zero mapped columns. I think it'll get in the way of meaningful use cases, in particular annotations. library(ggplot2)
ggplot() + geom_text(aes(x = 1, y = 1, label = "annotation"))Created on 2019-05-30 by the reprex package (v0.2.1) |
|
I'm with you that it's probably too strict, although the example you posted doesn't issue a warning (because the layer data is empty). If the layer has data (which is still super common because there's no idomatic way to stop a layer from inheriting the plot data), the warning will fire: library(ggplot2)
ggplot(mpg) + geom_text(aes(x = 1, y = 1, label = "annotation"))
#> Warning: Mapping contains zero mapped columns from dataggplot(mpg) + geom_text(aes(x = 1, y = 1, label = "annotation"), data = data.frame())Created on 2019-05-30 by the reprex package (v0.2.1) |
…for extract usage
|
Worth checking to see if this flags problems with any existing examples or vignettes. And worth adding a sentence or two to the parameter documentation of |
…ent documentation in aes(), improved the name of the warning functions, improved function detecting whether or not an expression refers to the plot data
|
There are no problems in the examples/vignettes (as assessed by changing |
| } | ||
|
|
||
| extract_target_is_likely_data <- function(x, data, env) { | ||
| if(!is.name(x[[2]])) { |
hadley
Jun 4, 2019
Member
Suggested change
if(!is.name(x[[2]])) {
if (!is.name(x[[2]])) {
| if(!is.name(x[[2]])) { | |
| if (!is.name(x[[2]])) { |
| #' @param x,y,... List of name-value pairs in the form `aesthetic = column_name` | ||
| #' describing which variables in the layer data should be mapped to which | ||
| #' aesthetics used by paired geom/stat. The names for x and y aesthetics are typically | ||
| #' omitted because they are so common; all other aesthetics must be named. |
hadley
Jun 4, 2019
Member
I do think it's worth being explicit about the wrong way to do things here. Maybe start a new paragraph and say something like: It's not necessary to refer to the dataset in aes(). Instead of writing aes(x = data$myvariable), all you need is aes(myvariable).
Maybe @mine-cetinkaya-rundel has thoughts on this?
I do think it's worth being explicit about the wrong way to do things here. Maybe start a new paragraph and say something like: It's not necessary to refer to the dataset in aes(). Instead of writing aes(x = data$myvariable), all you need is aes(myvariable).
Maybe @mine-cetinkaya-rundel has thoughts on this?
mine-cetinkaya-rundel
Jun 4, 2019
Contributor
I agree with @hadley that it's good to be explicit about what not to do, especially for those coming to ggplot2 from plot() where data$column_name usage is common. I might suggest matching the wording to the warning and say
"It's discouraged to refer to the data in aes(). Instead of aes(x = data$variable), use aes(variable)."
and similarly use aesthetic = variable earlier in the description (or use column or column_name or variable_name but keep it consistent throughout). I think it's more correct to avoid _name since it's not the name (character string) but the variable itself we're referring to, but I'm not sure.
I agree with @hadley that it's good to be explicit about what not to do, especially for those coming to ggplot2 from plot() where data$column_name usage is common. I might suggest matching the wording to the warning and say
"It's discouraged to refer to the data in aes(). Instead of aes(x = data$variable), use aes(variable)."
and similarly use aesthetic = variable earlier in the description (or use column or column_name or variable_name but keep it consistent throughout). I think it's more correct to avoid _name since it's not the name (character string) but the variable itself we're referring to, but I'm not sure.
paleolimbot
Jun 4, 2019
Author
Collaborator
I went for a mashup of both your suggestions and it feels much better now. Thank you!
I went for a mashup of both your suggestions and it feels much better now. Thank you!
… documentation for aes()
|
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/ |



This is a PR that implements warnings when the layer
mappingcontains syntax likedata$columninstead of.data$columnor (even better)column. This PR also implements an experimental and very open to discussion warning about layers that never map any columns from the data. Collectively, this is designed to encourage the correct usage of data and mappings, particularly in packages where this is sometimes done to avoid non-standard evaluation. This PR fixes #2693.Warnings for
$and[[usage inaes():Created on 2019-05-30 by the reprex package (v0.2.1)
As discussed in #2693, it is probably going too far to check for zero column mappings. I implemented it here just for discussion...it causes 5 warnings in the test cases (all of which fit the description of zero mapped columns from data).
Created on 2019-05-30 by the reprex package (v0.2.1)