dplyr和ggplot中的变量使用 [英] variable use in dplyr and ggplot
问题描述
我正在尝试使用dplyr/ggplot进行函数式编程.在R的前几周,我主要是在网上找到摘要后,反复尝试并反复尝试,但我试图更好地理解这一点,以使其更加自然.
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.
我以mtcar为例:
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")
并产生预期的结果:
我想出了如何通过变量调用大多数东西:
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:
我正在努力解决的问题是如何解决碳水化合物"一栏.通过变量 remove_col
在 filter
中.我一直在浏览tidyverse.org,但在整理作用域动词
,vars,cross(),...
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(), ...
有两个问题:
- 如何使用变量
remove_col
代替filter
语句中的显式carb
? - 为什么两个变量(
remove_col
和remove_val
)在filter
语句中的行为不同?
- 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?
推荐答案
aes_string
已被弃用,现在的首选方式是使用 .data
代词,该代词也可以是在 filter
中使用.
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`")
您还可以将 sym
与 !!
一起使用:
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`")
这篇关于dplyr和ggplot中的变量使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!