Arrange many plots using gridExtra

前端 未结 1 1289
情歌与酒
情歌与酒 2021-01-05 11:02

I have spent many hours trying to fit 11 graphs in one plot and arrange them using gridExtra but I have failed miserably, so I turn to you hoping you can help.

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-05 11:07

    I had to change the qplot loop call slightly (i.e. put the factors in the data frame) as it was throwing a mismatched size error. I'm not including that bit since that part is obviously working in your environment or it was an errant paste.

    Try adjusting your widths units like this:

    widths=unit(c(1000,50),"pt")
    

    And you'll get something a bit closer to what you were probably expecting:

    And, I can paste code now a few months later :-)

    library(ggplot2)
    library(gridExtra)
    
    df <- data.frame(price=matrix(sample(1:1000, 100, replace = TRUE), ncol = 1))
    
    df$size1 = 1:nrow(df)
    df$size1 = cut(df$size1, breaks=11)
    df=df[sample(nrow(df)),]
    df$size2 = 1:nrow(df)
    df$size2 = cut(df$size2, breaks=11)
    df=df[sample(nrow(df)),]
    df$clarity = 1:nrow(df)
    df$clarity = cut(df$clarity, breaks=6)
    
    # Create one graph for each size1, plotting the median price vs. the size2 by clarity:
    for (c in 1:length(table(df$size1))) {
    
      mydf = df[df$size1==names(table(df$size1))[c],]
      mydf = aggregate(mydf$price, by=list(mydf$size2, mydf$clarity),median);   
      names(mydf)[1] = 'size2'
      names(mydf)[2] = 'clarity'
      names(mydf)[3] = 'median_price'
      mydf$clarity <- factor(mydf$clarity)
    
      assign(paste("p", c, sep=""), 
             qplot(data=mydf, 
                   x=as.numeric(size2), 
                   y=median_price, 
                   group=clarity, 
                   geom="line", colour=clarity, 
                   xlab = "number of samples", 
                   ylab = "median price", 
                   main = paste("region number is ",c, sep=''), 
                   plot.title=element_text(size=10)) + 
               scale_colour_discrete(name = "clarity") + 
               theme_bw() + theme(axis.title.x=element_text(size = rel(0.8)), 
                                  axis.title.y=element_text(size = rel(0.8)), 
                                  axis.text.x=element_text(size=8),
                                  axis.text.y=element_text(size=8) ))
    }
    
    # Use gridExtra to arrange the 11 plots:
    
    g_legend<-function(a.gplot){
      tmp <- ggplot_gtable(ggplot_build(a.gplot))
      leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
      legend <- tmp$grobs[[leg]]
      return(legend)}
    
    mylegend<-g_legend(p1)
    
    
    grid.arrange(arrangeGrob(p1 + theme(legend.position="none"),
                             p2 + theme(legend.position="none"),
                             p3 + theme(legend.position="none"),
                             p4 + theme(legend.position="none"),
                             p5 + theme(legend.position="none"),
                             p6 + theme(legend.position="none"),
                             p7 + theme(legend.position="none"),
                             p8 + theme(legend.position="none"),
                             p9 + theme(legend.position="none"),
                             p10 + theme(legend.position="none"),
                             p11 + theme(legend.position="none"),
                             top ="Main title",
                             left = ""), mylegend, 
                 widths=unit(c(1000,50),"pt"), nrow=1)
    

    preview here

    Edit (16/07/2015): with gridExtra >= 2.0.0, the main parameter has been renamed top.

    0 讨论(0)
提交回复
热议问题