Skip to content

Instantly share code, notes, and snippets.

@sckott
Last active March 10, 2023 16:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sckott/8444444 to your computer and use it in GitHub Desktop.
Save sckott/8444444 to your computer and use it in GitHub Desktop.

The function modified

multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL, 
                      labs=list(), labpos=list(c(0.5,0.03), c(0.03,0.5))) {
  require(grid)
  
  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)
  
  numPlots = length(plots)
  
  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                     ncol = cols, nrow = ceiling(numPlots/cols))
  }
  
  if (numPlots==1) {
    print(plots[[1]])
    
  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
    
    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
      
      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
    
    if(!length(labs) == 0){
      grid.text(labs[1], x=labpos[[1]][1], y=labpos[[1]][2], gp=gpar(fontsize=16))
      grid.text(labs[2], x=labpos[[2]][1], y=labpos[[2]][2], rot=90, gp=gpar(fontsize=16))
    }
  }
}

The plots

library(ggplot2)

p1 <- 
  ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet, group=Chick)) +
  geom_line() +
  ggtitle("Growth curve for individual chicks") +
  labs(x="", y="")

# Second plot
p2 <- 
  ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet)) +
  geom_point(alpha=.3) +
  geom_smooth(alpha=.2, size=1) +
  ggtitle("Fitted growth curve per diet") +
  labs(x="", y="")

# Third plot
p3 <- 
  ggplot(subset(ChickWeight, Time==21), aes(x=weight, colour=Diet)) +
  geom_density() +
  ggtitle("Final weight, by diet") +
  labs(x="", y="")

# Fourth plot
p4 <- 
  ggplot(subset(ChickWeight, Time==21), aes(x=weight, fill=Diet)) +
  geom_histogram(colour="black", binwidth=50) +
  facet_grid(Diet ~ .) +
  ggtitle("Final weight, by diet") +
  theme(legend.position="none") +
  labs(x="", y="")       # No legend (redundant in this graph)  

Then you can do

multiplot(p1, p2, p3, p4, cols=2, labs=list("cool x label", "cool y label"))

@AaronRYilmaz
Copy link

AaronRYilmaz commented Mar 10, 2023

Awesome code! Any way to make the Y-axis label a mix of italics and normal font? For example: Mean Mus musculus per trap

I tried: bquote("Mean) " * italic("Mus musculus") * " per trap"

Thanks again!

@sckott
Copy link
Author

sckott commented Mar 10, 2023

@AaronRYilmaz I'm not sure how to do that sorry.

By the way, you might want to try the package patchwork https://patchwork.data-imaginist.com/ - when I wrote this code I don't think patchwork existed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment