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

后端 未结 3 1240
青春惊慌失措
青春惊慌失措 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:51

    The accepted answer is exactly what makes people run when comes to plotting using R! This is my solution:

    library('grid')
    g1 <- ggplot(...)  # however you draw your 1st plot
    g2 <- ggplot(...)  # however you draw your 2nd plot
    grid.newpage()
    grid.draw(cbind(ggplotGrob(g1), ggplotGrob(g2), size = "last"))
    

    This takes care of the y axis (minor and major) guide-lines to align in multiple plots, effortlessly.

    Dropping some axis text, unifying the legends, ..., are other tasks that can be taken care of while creating the individual plots, or by using other means provided by grid or gridExtra packages.

提交回复
热议问题