在 ggplot 中,如何将 y 列的名称作为变量传递?

and*_*rew 1 r ggplot2

我正在用 ggplot 构建一个图,但我事先不知道 y 列的名称。相反,y 列的名称包含在变量 yname 中。这显然不起作用:

ggplot(df, aes(x=date, y=yname))
Run Code Online (Sandbox Code Playgroud)

因为 ggplot 在 df 中查找字面上名为“yname”的列。如何将 y 列的名称作为变量传递给 ggplot?

luk*_*keA 5

使用aes_string

library(ggplot2)
yname <- "a"
df <- data.frame(x=runif(10), a=runif(10))
ggplot(df, aes_string(x="x", y=yname)) + geom_point()
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

230 次

最近记录:

12 年,1 月 前