dplyr + ggplot2:绘图不通过管道工作
dplyr + ggplot2: Plotting not working via piping
我想绘制我的数据框的一个子集。我正在使用 dplyr 和 ggplot2。我的代码仅适用于版本 1,不适用于通过管道传输的版本 2。有什么不同?
版本 1(绘图有效):
data <- dataset %>% filter(type=="type1")
ggplot(data, aes(x=year, y=variable)) + geom_line()
带有管道的版本 2(绘图不起作用):
data %>% filter(type=="type1") %>% ggplot(data, aes(x=year, y=variable)) + geom_line()
错误:
Error in ggplot.data.frame(., data, aes(x = year, :
Mapping should be created with aes or aes_string
感谢您的帮助!
在使用管道输入的过程中,如果您重新输入数据名称,就像我在下面用粗体显示的那样,函数会混淆参数的顺序。
data %>% filter(type=="type1") %>% ggplot(***data***, aes(x=year, y=variable)) + geom_line()
希望对你有用。
版本 2 的解决方案:一个点。而不是数据:
data %>% filter(type=="type1") %>% ggplot(., aes(x=year, y=variable)) + geom_line()
我通常这样做,这也省去了 .
:
library(dplyr)
library(ggplot2)
mtcars %>%
filter(cyl == 4) %>%
ggplot +
aes(
x = disp,
y = mpg
) +
geom_point()
我想绘制我的数据框的一个子集。我正在使用 dplyr 和 ggplot2。我的代码仅适用于版本 1,不适用于通过管道传输的版本 2。有什么不同?
版本 1(绘图有效):
data <- dataset %>% filter(type=="type1")
ggplot(data, aes(x=year, y=variable)) + geom_line()
带有管道的版本 2(绘图不起作用):
data %>% filter(type=="type1") %>% ggplot(data, aes(x=year, y=variable)) + geom_line()
错误:
Error in ggplot.data.frame(., data, aes(x = year, :
Mapping should be created with aes or aes_string
感谢您的帮助!
在使用管道输入的过程中,如果您重新输入数据名称,就像我在下面用粗体显示的那样,函数会混淆参数的顺序。
data %>% filter(type=="type1") %>% ggplot(***data***, aes(x=year, y=variable)) + geom_line()
希望对你有用。
版本 2 的解决方案:一个点。而不是数据:
data %>% filter(type=="type1") %>% ggplot(., aes(x=year, y=variable)) + geom_line()
我通常这样做,这也省去了 .
:
library(dplyr)
library(ggplot2)
mtcars %>%
filter(cyl == 4) %>%
ggplot +
aes(
x = disp,
y = mpg
) +
geom_point()