ggplot2 - multiple plots scaling

后端 未结 1 1109
走了就别回头了
走了就别回头了 2021-02-08 08:39

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

1条回答
  •  -上瘾入骨i
    2021-02-08 09:08

    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)
    

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