Problem: Passing a global variable to an argument in custom ggplot functions
Ultimately the goal is to paramaterize an argument (yval
in this example) so that multiple reports can be generated.
I'm running into issues with the ggplot function not interpreting the global value as I want. I'm sure this is my misunderstanding of NSE but haven't found an answer here.
It only works when the yvar
is explicity entered rather than passed via a global variable.
basic plot example
I want to make a function with this and then pass different values to x, y, color, etc.
library(ggplot2)
ggplot(mtcars, aes(cyl, mpg,
color=factor(vs))) +
geom_point()
pl_test <- function(df, xvar, yvar, gvar){
ggplot(df, aes(!!ensym(xvar), !!ensym(yvar),
color={{gvar}})) +
geom_point() }
mtcars$vs <- factor(mtcars$vs)
Works fine when variable is entered explicitly
# works fine
pl_test(mtcars, cyl, mpg, vs)
Doesn't work passed from global variable. This is the problem:
# doesn't interpret yvar correctly
yvar_for_plot <- "mpg"
pl_test(mtcars, cyl, yvar_for_plot, vs)
Doesn't work passed with symbol either
# doesn't work
yvar_for_plot <- sym("mpg")
pl_test(mtcars, cyl, yvar_for_plot, vs)
#> Don't know how to automatically pick scale for object of type <name>.
#> Defaulting to continuous.
#> Error in `geom_point()`:
#> ! Problem while computing aesthetics.
#> ℹ Error occurred in the 1st layer.
#> Caused by error in `compute_aesthetics()`:
#> ! Aesthetics are not valid data columns.
#> ✖ The following aesthetics are invalid:
#> ✖ `y = yvar_for_plot`
#> ℹ Did you mistype the name of a data column or fet to add `after_stat()`?
Created on 2024-11-16 with reprex v2.1.1
Problem: Passing a global variable to an argument in custom ggplot functions
Ultimately the goal is to paramaterize an argument (yval
in this example) so that multiple reports can be generated.
I'm running into issues with the ggplot function not interpreting the global value as I want. I'm sure this is my misunderstanding of NSE but haven't found an answer here.
It only works when the yvar
is explicity entered rather than passed via a global variable.
basic plot example
I want to make a function with this and then pass different values to x, y, color, etc.
library(ggplot2)
ggplot(mtcars, aes(cyl, mpg,
color=factor(vs))) +
geom_point()
pl_test <- function(df, xvar, yvar, gvar){
ggplot(df, aes(!!ensym(xvar), !!ensym(yvar),
color={{gvar}})) +
geom_point() }
mtcars$vs <- factor(mtcars$vs)
Works fine when variable is entered explicitly
# works fine
pl_test(mtcars, cyl, mpg, vs)
Doesn't work passed from global variable. This is the problem:
# doesn't interpret yvar correctly
yvar_for_plot <- "mpg"
pl_test(mtcars, cyl, yvar_for_plot, vs)
Doesn't work passed with symbol either
# doesn't work
yvar_for_plot <- sym("mpg")
pl_test(mtcars, cyl, yvar_for_plot, vs)
#> Don't know how to automatically pick scale for object of type <name>.
#> Defaulting to continuous.
#> Error in `geom_point()`:
#> ! Problem while computing aesthetics.
#> ℹ Error occurred in the 1st layer.
#> Caused by error in `compute_aesthetics()`:
#> ! Aesthetics are not valid data columns.
#> ✖ The following aesthetics are invalid:
#> ✖ `y = yvar_for_plot`
#> ℹ Did you mistype the name of a data column or fet to add `after_stat()`?
Created on 2024-11-16 with reprex v2.1.1
Share Improve this question asked Nov 16, 2024 at 19:03 Matt L.Matt L. 2,9741 gold badge17 silver badges27 bronze badges 3 |1 Answer
Reset to default 3The problem is that you want the function to be able to handle arguments in two very different ways: it should be able to take unquoted column names passed directly as symbols, but on occasion and without specific instruction also be expected to realise that the passed symbol is not a column name, but a symbol representing a variable in the calling environment which should be evaluated and converted from a string to a symbol.
You need to write conditional code that can identify the likely intention of the user and generate the appropriate symbol for it.
library(ggplot2)
pl_test <- function(df, xvar, yvar, gvar) {
xvar <- deparse(substitute(xvar))
yvar <- deparse(substitute(yvar))
gvar <- deparse(substitute(gvar))
if(xvar %in% names(df)) {
xvar <- str2lang(xvar)
} else if(xvar %in% ls(envir = parent.frame())) {
xvar <- str2lang(get(xvar, envir = parent.frame()))
}
if(yvar %in% names(df)) {
yvar <- str2lang(yvar)
} else if(yvar %in% ls(envir = parent.frame())) {
yvar <- str2lang(get(yvar, envir = parent.frame()))
}
if(gvar %in% names(df)) {
gvar <- str2lang(gvar)
} else if(gvar %in% ls(envir = parent.frame())) {
gvar <- str2lang(get(gvar, envir = parent.frame()))
}
ggplot(df, aes(!!xvar, !!yvar, color = !!gvar)) +
geom_point()
}
So now we can do:
mtcars$vs <- factor(mtcars$vs)
yvar_for_plot <- "mpg"
pl_test(mtcars, cyl, mpg, vs)
and also
pl_test(mtcars, cyl, yvar_for_plot, vs)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745646534a4638003.html
pl_test
works if called like this:yvar_for_plot <- sym("mpg"); pl_test(mtcars, cyl, !!yvar_for_plot, vs)
. – G. Grothendieck Commented Nov 18, 2024 at 19:42