dplyr + ggplot2:无法通过管道进行绘图 [英] dplyr + ggplot2: Plotting not working via piping

查看:676
本文介绍了dplyr + ggplot2:无法通过管道进行绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制我的数据框的一个子集。我正在使用dplyr和ggplot2。我的代码仅适用于版本1,而不适用于通过管道的版本2。有什么不同?



版本1(绘图工作):

  - 数据集%>%filter(type ==type1)
ggplot(data,aes(x = year,y = variable))+ geom_line()
/ pre>

版本2与管道(绘图不工作):

  data%>%filter(type ==type1)%>%ggplot(data,aes(x = year,y = variable))+ geom_line()

错误:

  ggplot.data中的错误。 frame(。,data,aes(x = year,:
)应使用aes或aes_string创建映射


$ b $感谢您的帮助!

解决方案

解决方案版本2:一个点,而不是数据:

  data%>%filter(type ==type1)%>%ggplot(。,aes(x = year,y =变量))+ geom_line()


I want to plot a subset of my dataframe. I am working with dplyr and ggplot2. My code only works with version 1, not version 2 via piping. What's the difference?

Version 1 (plotting is working):

data <- dataset %>% filter(type=="type1")
ggplot(data, aes(x=year, y=variable)) + geom_line()

Version 2 with piping (plotting is not working):

data %>% filter(type=="type1") %>% ggplot(data, aes(x=year, y=variable)) + geom_line()

Error:

Error in ggplot.data.frame(., data, aes(x = year,  : 
Mapping should be created with aes or aes_string

Thanks for your help!

解决方案

Solution for version 2: a dot . instead of data:

data %>% filter(type=="type1") %>% ggplot(., aes(x=year, y=variable)) + geom_line()

这篇关于dplyr + ggplot2:无法通过管道进行绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆