Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. # Multiple plot function
  2. #
  3. # ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
  4. # - cols: Number of columns in layout
  5. # - layout: A matrix specifying the layout. If present, 'cols' is ignored.
  6. #
  7. # If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
  8. # then plot 1 will go in the upper left, 2 will go in the upper right, and
  9. # 3 will go all the way across the bottom.
  10. #
  11. multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  12. require(grid)
  13.  
  14. # Make a list from the ... arguments and plotlist
  15. plots <- c(list(...), plotlist)
  16.  
  17. numPlots = length(plots)
  18.  
  19. # If layout is NULL, then use 'cols' to determine layout
  20. if (is.null(layout)) {
  21. # Make the panel
  22. # ncol: Number of columns of plots
  23. # nrow: Number of rows needed, calculated from # of cols
  24. layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
  25. ncol = cols, nrow = ceiling(numPlots/cols))
  26. }
  27.  
  28. if (numPlots==1) {
  29. print(plots[[1]])
  30.  
  31. } else {
  32. # Set up the page
  33. grid.newpage()
  34. pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
  35.  
  36. # Make each plot, in the correct location
  37. for (i in 1:numPlots) {
  38. # Get the i,j matrix positions of the regions that contain this subplot
  39. matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
  40.  
  41. print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
  42. layout.pos.col = matchidx$col))
  43. }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement