已在for循环中创建的多个ggplot2图的网格 [英] Grid of multiple ggplot2 plots which have been made in a for loop

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

问题描述

作为一个新的ggplot2用户,我有点迷失了很多可能性,并且很难在网上找到我认为是一个简单问题的简单答案。



我想在同一张纸上显示ggplot2的多个图表,但知道这些图表来自for循环。



以下示例不编译,它只是为了说明:

  for(i in c(1:n)){
for(j in c (1:m)){
..........#一些数据产生
p < - ggplot(df.all)+ geom_bar(aes_string(x = class.names [i ],fill = var.names [j])
}}

覆盖,但我想有一个矩阵或一个列表,我可以把所有的p,因为它们是生产,然后一个简单的函数,如

  display_in_a_grid(list_of_ggplot_plots)

但据我所知,我无法列出阴谋矩阵,既不是找到一个只需要一个参数输入的函数。



关于我看过的东西:



来自package gridExtra的arrangeGrob不起作用,因为它需要每个plot的明确名称(例如:p1,p2,p3,...),如 http://code.google.com/p/gridextra/wiki/arrangeGrob



facet方法不适用于我的数据集的组织(或相反:p)

您是否有简单的方法来管理它?

>

谢谢,

François

解决方案我会倾向于同意Richie,但是如果你想自己安排它们:

  library( (i在1:4中){
p [[i]] < - qplot(1:10)的
库(ggplot2)
p < ,10:1,main = i)
}
do.call(grid.arrange,p)

需要一个厕所k在的结尾处的例子中?arrangeGrob 用于消除for循环的方法:

  plots = lapply(1:5,function(.x)qplot(1:10,rnorm(10),main = paste(plot,。x)))
require(gridExtra )
do.call(grid.arrange,plots)


as a new ggplot2 user, I am a bit lost with the amount of possibilities, and struggle to find on the net a simple answer to what I consider a simple problem.

I would like to display multiple plots from ggplot2 on a same sheet, BUT knowing that these plots come from a for loop.

Following example does not compile, it is only to illustrate :

for(i in c(1:n)){                                   
  for(j in c(1:m)){
    ..........  # some data production
    p <- ggplot(df.all) + geom_bar(aes_string(x=class.names[i],fill=var.names[j])
}}

Here, p is overwritten, but I would like to have instead a matrix or a list in which I can put all the p as they are produced, then a simple function like

display_in_a_grid(list_of_ggplot_plots)

But as far as I tried, I was not able to make a list of matrix of plot, neither to find a function that takes only one argument for input.

About things I have had a look at :

"arrangeGrob" from package gridExtra doesn't work because it requires an explicit name for each plot (e.g.: p1,p2,p3,...) like in http://code.google.com/p/gridextra/wiki/arrangeGrob

"facet" method of ggplot2 is not adapted to the organization of my data set (or the contrary :p )

Would you have a simple way to manage this ?

Thank you,

François

解决方案

I would be inclined to agree with Richie, but if you want to arrange them yourself:

library(gridExtra)
library(ggplot2)
p <- list()
for(i in 1:4){
  p[[i]] <- qplot(1:10,10:1,main=i)
}
do.call(grid.arrange,p)

take a look at the examples at the end of ?arrangeGrob for ways to eliminate the for loop altogether:

plots = lapply(1:5, function(.x) qplot(1:10,rnorm(10),main=paste("plot",.x)))
require(gridExtra)
do.call(grid.arrange,  plots)

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

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