Search code examples
rlayoutplotggplot2gtable

Align ggplot2 plots vertically


With the code found at https://gist.github.com/low-decarie/5886616 A dual dendodogram tile plot can be produced:

dual_dendogram_tile_plot(as.matrix(USArrests), main="USA Arrests")

enter image description here

The problem: Align the vertical dendogram with the tile plot area. (and/or improve the alignment of the horizontal dendogram)

This question relates to:

left align two graph edges (ggplot)
Specifying ggplot2 panel width Plot correlation matrix into a graph


Solution

  • Here's an example to align more basic grobs,

    library(ggplot2)
    library(grid)
    library(gtable)
    
    p <- qplot(1,1)
    g <- ggplotGrob(p)
    
    panel_id <- g$layout[g$layout$name == "panel",c("t","l")]
    g <- gtable_add_cols(g, unit(1,"cm"))
    
    g <- gtable_add_grob(g, rectGrob(gp=gpar(fill="red")),
                         t = panel_id$t, l = ncol(g))
    
    g <- gtable_add_rows(g, unit(1,"in"), 0)
    g <- gtable_add_grob(g, rectGrob(gp=gpar(fill="blue")),
                         t = 1, l = panel_id$l)
    
    grid.newpage()
    grid.draw(g)
    

    enter image description here

    and with your grobs

    enter image description here