Side-by-side plots with ggplot2

后端 未结 13 2634
轮回少年
轮回少年 2020-11-22 02:06

I would like to place two plots side by side using the ggplot2 package, i.e. do the equivalent of par(mfrow=c(1,2)).

For example, I would like to have t

13条回答
  •  野性不改
    2020-11-22 02:32

    Update: This answer is very old. gridExtra::grid.arrange() is now the recommended approach. I leave this here in case it might be useful.


    Stephen Turner posted the arrange() function on Getting Genetics Done blog (see post for application instructions)

    vp.layout <- function(x, y) viewport(layout.pos.row=x, layout.pos.col=y)
    arrange <- function(..., nrow=NULL, ncol=NULL, as.table=FALSE) {
     dots <- list(...)
     n <- length(dots)
     if(is.null(nrow) & is.null(ncol)) { nrow = floor(n/2) ; ncol = ceiling(n/nrow)}
     if(is.null(nrow)) { nrow = ceiling(n/ncol)}
     if(is.null(ncol)) { ncol = ceiling(n/nrow)}
            ## NOTE see n2mfrow in grDevices for possible alternative
    grid.newpage()
    pushViewport(viewport(layout=grid.layout(nrow,ncol) ) )
     ii.p <- 1
     for(ii.row in seq(1, nrow)){
     ii.table.row <- ii.row 
     if(as.table) {ii.table.row <- nrow - ii.table.row + 1}
      for(ii.col in seq(1, ncol)){
       ii.table <- ii.p
       if(ii.p > n) break
       print(dots[[ii.table]], vp=vp.layout(ii.table.row, ii.col))
       ii.p <- ii.p + 1
      }
     }
    }
    

提交回复
热议问题