我在使用colnames()的数据列中有一个列名称列表。我用一个计数器i遍历该列表,并为每个计数器创建一个ggplot。我遇到的麻烦是ggplot(aes(x = something, y = columns[i]))
因为columns [i]是一个字符串,所以它仅以y轴作图,称为我实际想要选择的列,但没有值。如何选择没有字符串的列。我已经尝试过noquote()
和as.name()
,但这似乎将它们更改为不兼容的类型。
我们可以转换为sym
bol并求值(!!
)
library(ggplot2)
columns <- c("mpg", "wt")
plist <- lapply(columns, function(nm)
ggplot(mtcars, aes(gear, !! rlang::sym(nm))) +
geom_col()
)
如@Markus所评论,您可以看一下aes_string
,这是使用带引号的字符串的示例
library(ggplot2)
ggplot(mtcars, aes_string(x="hp", y="mpg" ))+geom_point()
这应该起作用:
ggplot(df, aes(x = something, y = df[,columns[i]]))