Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ggplot子集数据函数和dplyr_R_Ggplot2_Dplyr - Fatal编程技术网

Ggplot子集数据函数和dplyr

Ggplot子集数据函数和dplyr,r,ggplot2,dplyr,R,Ggplot2,Dplyr,在进行数据分析时,我们通常使用dplyr在特定的geoms中进一步修改数据帧。这使我们可以在以后更改ggplot的默认数据帧,并使所有内容仍然有效 template <- ggplot(db, aes(x=time, y=value)) + geom_line(data=function(db){db %>% filter(event=="Bla")}) + geom_ribbon(aes(ymin=low, ymax=up)) ggsave( templat

在进行数据分析时,我们通常使用dplyr在特定的
geom
s中进一步修改数据帧。这使我们可以在以后更改ggplot的默认数据帧,并使所有内容仍然有效

template <- ggplot(db, aes(x=time, y=value)) + 
     geom_line(data=function(db){db %>% filter(event=="Bla")}) + 
     geom_ribbon(aes(ymin=low, ymax=up))
ggsave( template, "global.png" )
for(i in unique(db$simulation)) 
     ggsave( template %+% subset(db, simulation==i), paste0(i, ".png")
模板%filter(事件==“Bla”)}+
geom_色带(aes(ymin=低,ymax=高))
ggsave(模板“global.png”)
对于(唯一的i(db$模拟))
ggsave(模板%+%子集(db,simulation==i),paste0(i,“.png”)
是否有更好/更短的方法来指定
过滤器
命令,例如使用一些神奇的

编辑
澄清一些评论:使用
geom_行(data=db%>%filter(event==“Bla”))
,当我稍后使用
%+%
更改默认数据帧时,该层将不会更新。我真正的目标是使用geom_*的
数据
参数作为函数。

更好地阅读
%
的文档后,我找到了解决方案:

将点位保持器用作lhs 当点用作lhs时,结果将是一个函数序列,即一个将整个右侧链依次应用于其输入的函数。参见示例

因此,制定上述示例的最佳方法,同时结合上面的建议:

db <- diamonds
template <- ggplot(db, aes(x=carat, y=price, color=cut)) + 
  geom_point() +
  geom_smooth(data=. %>% filter(color=="J")) +
  labs(caption="Smooths only for J color")
ggsave( template, "global.png" )
db %>% group_by(cut) %>% do(
  ggsave( paste0(.$cut[1], ".png"), plot=template %+% .)
)
db%group\u by(cut)%%>%do(
ggsave(粘贴0(.$cut[1],“.png”),plot=template%+%)
)

geom_-line(data=filter(db,event==“Bla”))
geom_-line(data=db%>%filter(event==“Bla”)
您也可以在
ggplot
之前声明db:
db%>%ggplot(,aes(x=time,y=value))+geom_-line(filter(,event==“Bla”)[…])
,并用
替换
(db,db$模拟,函数(subdf){i