dplyr和ggplot中的变量使用 [英] variable use in dplyr and ggplot

查看:43
本文介绍了dplyr和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(), ...

有两个问题:

  1. 如何使用变量 remove_col 代替 filter 语句中的显式 carb ?
  2. 为什么两个变量( remove_col remove_val )在 filter 语句中的行为不同?
  1. How do I use the variable remove_col instead of the explicit carb in the filter statement?
  2. Why do the two variables (remove_col and remove_val) behave differently in the filter 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆