variable use in dplyr and ggplot

大憨熊 提交于 2021-02-11 16:36:53

问题


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:

  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?

回答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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!