R, ggplot - Graphs sharing the same y-axis but with different x-axis scales

后端 未结 3 1241
青春惊慌失措
青春惊慌失措 2021-01-31 10:18

Context

I have some datasets/variables and I want to plot them, but I want to do this in a compact way. To do this I want them to share the same y-axis but distinct x-

3条回答
  •  长发绾君心
    2021-01-31 10:47

    The accepted answer looks a little too daunting to me. So I find two ways to get around it with less efforts. Both are based on your Attempt #2 grid.arrange() method.

    1. Make plot 1 no y-axis as well
    theme(axis.text.y = element_blank(), 
          axis.ticks.y = element_blank(), 
          axis.title.y = element_blank()
    

    So all the plots will be the same. You won't have problems with different aspects ratios. You will need to generate a separate y-axis with R or your favorite image editting app.

    2. Fix and respect aspects ratio

    Add aspect.ratio = 1 or whatever ratio you desire to theme() of individual plots. Then use respect=TRUE in your grid.arrange()

    This way you can keep y-axis in plot1 and still maintains aspects ratio in all plots. Inspired by this answer.

    Hope you find these helpful!

提交回复
热议问题