[R]dplyr及ggplot2中的变量引用列的问题

问题描述:

存在这么一个场景,当需要动态选择列作为dplyr或ggplot2的输入时,列名的指定会出现问题。

以iris举例:

# 以iris dataset为例
colnames <- c("Sepal.Length", "Sepal.Width", "Petal.Length","Petal.Width","Species")

for (col in colnames) {
    ggplot(data = iris, aes(x = Sepal.Length, y = col)) + geom_point()  
}

上面的代码会报错。在ggplot2和dplyr中,我们指定列是`y = Sepal.Length`, 而通过变量指定列时,是` y = 'Sepal.Length'`, 注意这个引号。这个引号导致了dplyr和ggplot2包匹配列的失效。

相关的问题,Hadley(dplyr和ggplot2包作者)在这里有描述:https://dplyr.tidyverse.org/articles/programming.html

猜你喜欢

转载自www.cnblogs.com/oDoraemon/p/9298890.html