I tried to generate multiple grid plots with ggplot2. So I would like to generate distribution plot with additional boxplot below x-axis and that for different groups and variab
With the cowplot
package this becomes a bit easier. However, we should properly set the x-axis range to ensure they are the same for both plots. This is because the density plots are naturally a bit wider than pure data plots, and the axis for p1
will therefore be a bit wider. When the axes are fixed we can arrange and align them (axis text and margins will no longer matter).
library(cowplot)
comb <- plot_grid(
p1 + xlim(-5, 5),
p2 + ylim(-5, 5), # use ylim for p2 because of coord_flip()
align = 'v', rel_heights = c(4, 1), nrow = 2
)
Similarly we can arrange multiples of the combination plots:
plot_grid(comb, comb, comb, comb)