将for循环中创建的多个ggplots保存到一个图中 [英] saving multiple ggplots created in a for loop to a single plot

查看:408
本文介绍了将for循环中创建的多个ggplots保存到一个图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个地块有多个ggplots地块。
问题:当试图使用for循环创建一个图表列表(稍后在grid.arrange中使用)时,列表返回空。

我使用了这两个函数: / p>

1)
我对R相当陌生,这是我的第一个ggplot2(*请原谅多重geom_line())。因此,它花了我一些时间来弄清楚如何适应分布和绘制它们(谢谢,stackoverflow !!)在第一位。所以,这里的任何帮助将不胜感激。

更新:我可以使用 PIL python包完成上述操作。然而,我真的希望能够在R做到这一点。

解决方案

与您的 plotlist 很可能是您在 ggplot 调用 i code $ for $ 循环直接 -

$ g $ p $ ggplot()+ geom_line(aes(xi, yi))+ geom_line(aes(ai,bi))

i 然后在渲染图时懒惰地评估,也就是说,使用 i 的最后一个值来创建所有如果是这样的话,你可以使用 aes_string 来代替 aes $ b / code>:

$ $ $ $ $ $ $ $ $ $ $ $ ggplot(df,aes_string(x =your_x,y = i))+ geom_line ()

但是你要做的似乎是一个完美的工作 facet_grid facet_wrap ,在这种情况下,您的循环仅用于生成数据:

 模型<  -  c(foo,ba (我在1:长度(模型))
{
#xvar,yvar,avar,bvar())
plotlist<来自任何你得到它们
plotlist [[i]]< - data.frame(modname = models [i],
xvar = c(1:10),
yvar = rnorm (10),
avar = c(1:10),
bvar = rnorm(10,mean = 1),
stringsAsFactors = FALSE)

}

alldata< - do.call(rbind,plotlist)

ggplot(alldata,aes(xvar,yvar))+
geom_line()+
geom_line(aes(avar,bvar))+
facet_grid(〜modname)


I would like to have multiple ggplots plots in a single plot. ISSUE: When trying to create a list of plots (to be used later in grid.arrange) using a for-loop, the list is returned empty.
I used these two posts:

1)create figures in a loop
2)use grid.arrange to save in a single fig

to come up with the following code (simpler version)* to plot probability density curves:

#models = 33 obs of 1 variable
plotlist = list()
for (i in 1:33)
{
 modname = models$col1[i]
 p<- ggplot() + geom_line(aes(xi,yi)) + geom_line(aes(ai,bi)) + 
     ggtitle(modname) ## the x,y,a,b are just illustrative.
 #In reality, each pair is produced using fitdist and dgamma functions for
  # data (single column) from separate .csv
 ggsave(outpath)
 plotlist[[i]] = p   

}
main <- grid.arrange(grobs=plotlist,ncol=6)
main
ggsave("bigplot.png",p)

ISSUE (further explanation): plotlist shows up as an empty list. As a result, grid.arrange just plots the subplot created in the last loop 33 times. But, what is strange is that the grid.arrange plot has the correct title for all subplots (assigned using modname)!!! In the attached picture, you will see that all subplots are the same except for the title. Since I save individual subplots as well, I know that the issue is not with the data/code for subplots. I am fairly new to R and this is my first ggplot2 (* excuse the multiple geom_line()). As a result, it has taken me a while to figure out how to fit distributions and plot them (Thanks, stackoverflow!!) in the first place. So, any help here will be much appreciated.

Update: I was able to accomplish the above using PIL package in python . However, I would really like to be able to do this in R.

解决方案

the problem with your plotlist is probably that you call the i placeholder in your call to ggplot inside your for loop directly-

ggplot() + geom_line(aes(xi,yi)) + geom_line(aes(ai,bi))

i then gets evaluated lazily when the plot is rendered, i.e., the last value of i is used to create all the plots.

If that is the case, you can use aes_string in stead of aes:

ggplot(df, aes_string(x="your_x", y=i)) + geom_line()

BUT what you are trying to do seems to be a perfect job for facet_grid or facet_wrap, in which case your loop is only used to generate the data:

models <- c("foo","bar","thing")
plotlist <- list()
for (i in 1:length(models))
{
   #xvar, yvar, avar, bvar come from wherever you get them
  plotlist[[i]] <- data.frame(modname = models[i], 
                             xvar = c(1:10), 
                             yvar = rnorm(10), 
                             avar = c(1:10),
                             bvar = rnorm(10, mean = 1),
                             stringsAsFactors = FALSE)   

}

alldata <- do.call(rbind, plotlist)

ggplot(alldata, aes(xvar, yvar)) + 
  geom_line() + 
  geom_line(aes(avar,bvar)) +
  facet_grid(~modname)

这篇关于将for循环中创建的多个ggplots保存到一个图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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