问题
I'm trying to sort out functional programming with dplyr/ggplot. In my first couple of weeks of R I mostly went by trial and error following snippets found on the web, but I'm trying to understand this better so it comes more natural.
I'm playing around with mtcars as an example:
library(tidyverse)
data <- mtcars
data$carb <- as.factor(data$carb)
My sample code w/o using variables looks like this:
data %>% filter(carb != 4) %>%
ggplot() + geom_point(aes(x = mpg, y = hp, color = carb)) +
ggtitle("Explicit/no variables")
and produces the expected:
I figured out how to call most everything via variables:
remove_col <- "carb"
remove_val <- 4
x_value <- "mpg"
y_value <- "hp"
data %>% filter( carb != remove_val ) %>%
ggplot() + geom_point(aes_string(x = x_value, y = y_value, color = remove_col )) +
ggtitle("Variables for `geom_point with aes_string` and for value to remove from `carb`")
This gives the same plot as above:
The thing I'm struggling with is how to address the column "carb" in filter
via the variable remove_col
. I have been going through tidyverse.org, but I am having a hard time sorting out scoped verbs
, vars, across(), ...
So two questions:
- How do I use the variable
remove_col
instead of the explicitcarb
in thefilter
statement? - Why do the two variables (
remove_col
andremove_val
) behave differently in thefilter
statement?
回答1:
aes_string
has been deprecated and the preferred way now is to use .data
pronoun which can also be used in filter
.
library(dplyr)
library(ggplot2)
remove_col <- "carb"
remove_val <- 4
x_value <- "mpg"
y_value <- "hp"
data %>%
filter(.data[[remove_col]] != remove_val ) %>%
ggplot() + geom_point(aes(x = .data[[x_value]], y = .data[[y_value]],
color = .data[[remove_col]])) +
ggtitle("Variables for `geom_point with aes` and for value to remove from `carb`")
You can also use sym
with !!
:
data %>%
filter(!!sym(remove_col) != remove_val ) %>%
ggplot() + geom_point(aes(x = !!sym(x_value), y = !!sym(y_value), color = !!sym(remove_col))) +
ggtitle("Variables for `geom_point with aes` and for value to remove from `carb`")
来源:https://stackoverflow.com/questions/65479529/variable-use-in-dplyr-and-ggplot