Place title of multiplot panel with ggplot2

前端 未结 2 1134
生来不讨喜
生来不讨喜 2020-12-04 16:08

From the help found here I\'ve managed to create this multiplot panel: \"enter with the follow

相关标签:
2条回答
  • 2020-12-04 16:41

    If I understand correctly what you want to do, probably you can use +opts(title = XXX):

    p1 <- ggplot(mtcars, aes(factor(cyl))) + geom_bar()
    p2 <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
    p3 <- p2 + geom_line()
    
    pushViewport(viewport(layout = grid.layout(2, 2)))  
    print(p1 + opts(title = "bar"), 
      vp = viewport(layout.pos.row = 1, layout.pos.col = 1))     
    print(p2 + opts(title = "point"), 
      vp = viewport(layout.pos.row = 1, layout.pos.col = 2))     
    print(p3 + opts(title = "point and line"), 
      vp = viewport(layout.pos.row = 2, layout.pos.col = 1:2))
    

    enter image description here

    UPDATED

    here is an example:

    pushViewport(viewport(layout = grid.layout(3, 2, heights = unit(c(1, 4, 4), "null"))))
    grid.text("title of this panel", vp = viewport(layout.pos.row = 1, layout.pos.col = 1:2))
    print(p1, vp = viewport(layout.pos.row = 2, layout.pos.col = 1))
    print(p2, vp = viewport(layout.pos.row = 2, layout.pos.col = 2))
    print(p3, vp = viewport(layout.pos.row = 3, layout.pos.col = 1:2))
    

    what you need to do is:

    1. Make one extra row in grid.layout
    2. Adjust width
    3. Draw textGrob on the extra viewport row.

    enter image description here

    0 讨论(0)
  • 2020-12-04 16:41
    library(gridExtra)
    
    p <- ggplot()
    
    grid.arrange(p,p,p,p,p, top = "Title",
                layout_matrix = matrix(c(1,2,3,4,5,5), ncol=2, byrow=TRUE))
    

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