我想绘制我的数据帧的子集.我正在使用dplyr和ggplot2.我的代码仅适用于版本1,而不适用于通过管道版本2.有什么不同?
版本1(绘图工作):
data <- dataset %>% filter(type=="type1")
ggplot(data, aes(x=year, y=variable)) + geom_line()
Run Code Online (Sandbox Code Playgroud)
版本2带管道(绘图不起作用):
data %>% filter(type=="type1") %>% ggplot(data, aes(x=year, y=variable)) + geom_line()
Run Code Online (Sandbox Code Playgroud)
错误:
Error in ggplot.data.frame(., data, aes(x = year, :
Mapping should be created with aes or aes_string
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
kbr*_*ner 19
版本2的解决方案:一个点.而不是数据:
data %>% filter(type=="type1") %>% ggplot(., aes(x=year, y=variable)) + geom_line()
Run Code Online (Sandbox Code Playgroud)
我通常会这样做,这也不需要.:
library(dplyr)
library(ggplot2)
mtcars %>%
filter(cyl == 4) %>%
ggplot +
aes(
x = disp,
y = mpg
) +
geom_point()
Run Code Online (Sandbox Code Playgroud)