geom_tile and facet_grid/facet_wrap for same height of tiles

Solution 1:

You can look at what ggplot contains before plotting, and rescale the panels accordingly.

g <- ggplot_build(p) 
## find out how many y-breaks are in each panel
## to infer the number of tiles
vtiles <- sapply(lapply(g$panel$ranges, "[[", "y.major"), length)

## convert the plot to a gtable object 
gt <- ggplot_gtable(g)
## find out which items in the layout correspond to the panels
## we refer to the "t" (top) index of the layout
panels <- gt$layout$t[grepl("panel", gt$layout$name)]
## replace the default panel heights (1null) with relative sizes 
## null units scale relative to each other, so we scale with the number of tiles
gt$heights[panels] <-lapply(vtiles, unit, "null")
## draw on a clean slate
library(grid)
grid.newpage()
grid.draw(gt)

enter image description here

Solution 2:

It took me some time to find easier solution which is actually part of the facet_grid where you can set the space = "free_y". More info at recent question.