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.
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
pl_test(mtcars, cyl, mpg, vs)
# doesn't interpret yvar correctly
yvar_for_plot <- "mpg"
pl_test(mtcars, cyl, yvar_for_plot, vs)
# 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 forget to add `after_stat()`?
Created on 2024-11-16 with reprex v2.1.1
The 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)