R: ggplot2 make two geom_tile plots have equal height

前端 未结 1 431
迷失自我
迷失自我 2020-12-10 21:40

I have two ggplot geom_tile plots that I put together using grid.arrange() from the gridExtra library. This is the result

library(ggplot2)
library(plyr)

p3         


        
1条回答
  •  有刺的猬
    2020-12-10 22:08

    enter image description here

    p3b <- ggplot_build(p3)
    p4b <- ggplot_build(p4)
    g3 = ggplot_gtable(p3b) ; g4 = ggplot_gtable(p4b)
    
    # work out the number of horizontal tiles
    n3 <- length(p3b[["panel"]][["ranges"]][[1]][["x.major_source"]])
    n4 <- length(p4b[["panel"]][["ranges"]][[1]][["x.major_source"]])
    
    require(gtable)
    require(grid)
    g <- cbind(g3, g4, size = "first")
    #adjust the panel widths in proportion to n4/n3
    panels <- g$layout$l[grep("panel", g$layout$name)]
    g$widths[panels] <- lapply(c(1,n4/n3), unit, "null")
    grid.newpage()
    grid.draw(g)
    

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