R: Grid layout title

后端 未结 2 1207
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-16 15:24

I\'m using the grid package to display an array of plots like this:

layout <- grid.layout(2, 4)
pushViewport(viewport(layout = layout))
# print various pl         


        
相关标签:
2条回答
  • 2020-12-16 15:51

    Another way:

    library(gridExtra)
    g = rectGrob() # dummy "plot"
    grid.arrange(g, g, g, g, ncol=2, top = "Main Title")
    
    0 讨论(0)
  • 2020-12-16 16:05

    Dummy example based on a similar SO question: Place title of multiplot panel with ggplot2

    1. First create a layout with the required number of rows + 1 short one for title:

      pushViewport(viewport(layout = grid.layout(3, 2, heights = unit(c(0.5, 5, 5), "null"))))   
      
    2. Create some plots there:

      print(ggplot(mtcars, aes(hp)) + geom_histogram(), vp = viewport(layout.pos.row = 2, layout.pos.col = 1:2))
      print(ggplot(mtcars, aes(wt)) + geom_histogram(), vp = viewport(layout.pos.row = 3, layout.pos.col = 1))
      print(ggplot(mtcars, aes(mpg)) + geom_histogram(), vp = viewport(layout.pos.row = 3, layout.pos.col = 2))
      
    3. Add a title to the top row:

      grid.text("MAIN TITLE", vp = viewport(layout.pos.row = 1, layout.pos.col = 1:2))
      

    Resulting in:

    enter image description here

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