standard eval with ggplot2 without `aes_string()`
You can do this using the !!
operator on the variable after call to sym
. This will unquote and evaluate variable
in the surrounding environement.
library(rlang)
g1 <- function( variable ) {
ggplot(mtcars, aes(x = wt, y = !! sym(variable) , size = "carb")) +
geom_point()
}
g1("mpg")
variables <- c("mpg", "cyl", "disp")
variables %>%
lapply(g1)
Combine with() and ggplot2
Your code doesn't work because the first argument of ggplot()
is data
. You need to specifically say that you want to use the argument mapping
.
df <- data.frame(a= 1:10, b= 1:10)
with(df, ggplot(mapping = aes(a, b)) + geom_point())
Or you can do this
df <- data.frame(a= 1:10, b= 1:10)
with(df, ggplot() + geom_point(aes(a, b))
The second method works because the first argument for geom_*
is mapping
.
pass character strings to ggplot2 within a function
It is now recommended to use .data
pronoun
FUN <- function(dat, x, y) {
ggplot(dat, aes(x = .data[[x]], y = .data[[y]])) +
geom_point()
}
FUN(mtcars, "mpg", "hp")
Couple of other alternatives -
#Using get
FUN <- function(dat, x, y) {
ggplot(dat, aes(x = get(x), y = get(y))) +
geom_point()
}
#Using sym
FUN <- function(dat, x, y) {
ggplot(dat, aes(x = !!sym(x), y = !!sym(y))) +
geom_point()
}
How to use string variables with tidy evaluation in ggplot v3.0.0?
You are unquoting, but it just yields a character vector.
This works:
mtcars %>% select(!!y_var)
Because this works:
mtcars %>% select('drat')
The ?select
help actually states this as an exception:
# For convenience it also supports strings and character
# vectors. This is unlike other verbs where strings would be
# ambiguous.
vars <- c(var1 = "cyl", var2 ="am")
select(mtcars, !!vars)
rename(mtcars, !!vars)
It cannot be taken as a general working rule for tidy evaluation in the tidyverse.
Case in point, in ggplot character vectors in aes
have a different meaning, you can't just give:
ggplot(mtcars) + geom_point(aes(x = disp, y = 'drat'))
Try for example:
ggplot(mtcars) + geom_point(aes(x = disp, y = !!as.name(y_var)))
standard eval with `dplyr::count()`
To create a list of symbols from strings, you want rlang::syms
(not rlang::sym
). For unquoting a list or a vector, you want to use !!!
(not !!
). The following will work:
library(magrittr)
variables <- c("cyl", "vs")
vars_sym <- rlang::syms(variables)
vars_sym
#> [[1]]
#> cyl
#>
#> [[2]]
#> vs
mtcars %>%
dplyr::count(!!! vars_sym)
#> # A tibble: 5 x 3
#> cyl vs n
#> <dbl> <dbl> <int>
#> 1 4 0 1
#> 2 4 1 10
#> 3 6 0 3
#> 4 6 1 4
#> 5 8 0 14
Non standard evaluation, lapply, and ggplot
lapply
is not going to work with the function you have right now to solve this. x
is simply a vector when passed to that function, it is not the name of that variable, and lapply
isn't passing anything that is the name. In other words, there is nothing in the scope of that function for it to figure out what should be the proper x-axis label.
One solution is similar to @Jimbou:
gg_dens <- function(name, dat) {
ggplot(dat, aes_string(x = name)) + geom_density() + ggtitle(name)
}
lapply(names(mtcars), gg_dens, mtcars)
Or just use facets instead:
mtcars2 <- tidyr::gather(mtcars)
ggplot(mtcars2, aes(value)) + geom_density() + facet_wrap(~key, scales = 'free')
Does aes_string() change any default settings in R? A problem with R Shiny and ggplot input$ interaction
Your selection5
was an issue. The following code gives a reactive data frame.
#example model
selection5 <- reactive({
# df <- iris[, c(input$xcol5, input$ycol5, 'Species')] ## this call does not work
df <- data.frame(x=iris[[input$xcol5]], y=iris[[input$ycol5]], Species=iris[, "Species"])
})
Related Topics
R: "Make" Not Found When Installing a R-Package from Local Tar.Gz
Mass Variable Declaration and Assignment in R
Is There Something Like a Pmax Index
R Bnlearn Eval Inside Function
Rvest Not Recognizing CSS Selector
Rbind Corresponding Elements in Two or More Lists in R
How to Plot Charts with Nested Categories Axes
Add a Series of Elements in Different Locations Within a Vector
R: Holt-Winters with Daily Data (Forecast Package)
How to Filter Rows Based on the Previous Row and Keep Previous Row Using Dplyr
Getting the Minimum of the Rows in a Data Frame
Understanding Bandwidth Smoothing in Ggplot2
R Geom_Tile Ggplot2 What Kind of Stat Is Applied
Changing Line Color in Ggplot Based on Slope
R Dplyr Subset with Missing Columns