Side-by-side plots with ggplot2

后端 未结 13 2476
轮回少年
轮回少年 2020-11-22 02:06

I would like to place two plots side by side using the ggplot2 package, i.e. do the equivalent of par(mfrow=c(1,2)).

For example, I would like to have t

13条回答
  •  时光说笑
    2020-11-22 02:30

    One downside of the solutions based on grid.arrange is that they make it difficult to label the plots with letters (A, B, etc.), as most journals require.

    I wrote the cowplot package to solve this (and a few other) issues, specifically the function plot_grid():

    library(cowplot)
    
    iris1 <- ggplot(iris, aes(x = Species, y = Sepal.Length)) +
      geom_boxplot() + theme_bw()
    
    iris2 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
      geom_density(alpha = 0.7) + theme_bw() +
      theme(legend.position = c(0.8, 0.8))
    
    plot_grid(iris1, iris2, labels = "AUTO")
    

    The object that plot_grid() returns is another ggplot2 object, and you can save it with ggsave() as usual:

    p <- plot_grid(iris1, iris2, labels = "AUTO")
    ggsave("plot.pdf", p)
    

    Alternatively, you can use the cowplot function save_plot(), which is a thin wrapper around ggsave() that makes it easy to get the correct dimensions for combined plots, e.g.:

    p <- plot_grid(iris1, iris2, labels = "AUTO")
    save_plot("plot.pdf", p, ncol = 2)
    

    (The ncol = 2 argument tells save_plot() that there are two plots side-by-side, and save_plot() makes the saved image twice as wide.)

    For a more in-depth description of how to arrange plots in a grid see this vignette. There is also a vignette explaining how to make plots with a shared legend.

    One frequent point of confusion is that the cowplot package changes the default ggplot2 theme. The package behaves that way because it was originally written for internal lab uses, and we never use the default theme. If this causes problems, you can use one of the following three approaches to work around them:

    1. Set the theme manually for every plot. I think it's good practice to always specify a particular theme for each plot, just like I did with + theme_bw() in the example above. If you specify a particular theme, the default theme doesn't matter.

    2. Revert the default theme back to the ggplot2 default. You can do this with one line of code:

    theme_set(theme_gray())
    

    3. Call cowplot functions without attaching the package. You can also not call library(cowplot) or require(cowplot) and instead call cowplot functions by prepending cowplot::. E.g., the above example using the ggplot2 default theme would become:

    ## Commented out, we don't call this
    # library(cowplot)
    
    iris1 <- ggplot(iris, aes(x = Species, y = Sepal.Length)) +
      geom_boxplot()
    
    iris2 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
      geom_density(alpha = 0.7) +
      theme(legend.position = c(0.8, 0.8))
    
    cowplot::plot_grid(iris1, iris2, labels = "AUTO")
    

    Updates:

    • As of cowplot 1.0, the default ggplot2 theme is not changed anymore.
    • As of ggplot2 3.0.0, plots can be labeled directly, see e.g. here.

提交回复
热议问题