我想在同一个调用中使用另一个函数中传递给data函数参数的数据帧变量。ggplotggplot2
例如,在下面的示例中,我想引用x传递给另一个函数中的data参数的数据帧中的变量,例如:ggplot scale_x_continuous
library(ggplot2)
set.seed(2017)
samp <- sample(x = 20, size= 1000, replace = T)
ggplot(data = data.frame(x = samp), mapping = aes(x = x)) + geom_bar() +
scale_x_continuous(breaks = seq(min(x), max(x)))
Run Code Online (Sandbox Code Playgroud)
我得到错误:
Error in seq(min(x)) : object 'x' not found
Run Code Online (Sandbox Code Playgroud)
我明白。当然,我可以通过执行以下操作来避免这个问题:
df <- data.frame(x = samp)
ggplot(data = df, mapping = aes(x = x)) + geom_bar() +
scale_x_continuous(breaks = seq(min(df$x), max(df$x)))
Run Code Online (Sandbox Code Playgroud)
但我不想被迫df在调用 ggplot 之外定义对象。我希望能够直接引用我传入的数据帧中的变量data。 …