在同一页面上安排多张ggplot2图片时,普通的标准R函数par()layout已经不能使用。

目前基本的解决办法是使用名叫ggExtra的R包,它包含以下功能:

  • grid.arrange()arrangeGrob()可以在一个页面上排列多个ggplots
  • marrangeGrob()可用于在多个页面上排列多个ggplots

但是,这些功能不会尝试对齐绘图面板; 相反,这些图只是简单地放置在网格中,因此轴不对齐。

因此我们可以使用ggpubr包提供的ggarrange()函数来实现多个ggplot图画的排布,并且提供统一的共同图例。

创建图片

使用演示数据集 ToothGrowthmtcars创建多个图形:

  1. # Box plot (bp)
  2. bxp <- ggboxplot(ToothGrowth, x = "dose", y = "len",
  3. color = "dose", palette = "jco")
  4. bxp

unnamed-chunk-2-1.png

  1. # Dot plot (dp)
  2. dp <- ggdotplot(ToothGrowth, x = "dose", y = "len",
  3. color = "dose", palette = "jco", binwidth = 1)
  4. dp

unnamed-chunk-2-2.png

  1. data("mtcars")
  2. mtcars$name <- rownames(mtcars)
  3. mtcars$cyl <- as.factor(mtcars$cyl)
  4. head(mtcars[, c("name", "wt", "mpg", "cyl")])
  1. ## name wt mpg cyl
  2. ## Mazda RX4 Mazda RX4 2.62 21.0 6
  3. ## Mazda RX4 Wag Mazda RX4 Wag 2.88 21.0 6
  4. ## Datsun 710 Datsun 710 2.32 22.8 4
  5. ## Hornet 4 Drive Hornet 4 Drive 3.21 21.4 6
  6. ## Hornet Sportabout Hornet Sportabout 3.44 18.7 8
  7. ## Valiant Valiant 3.46 18.1 6
  1. # Bar plot (bp)
  2. bp <- ggbarplot(mtcars, x = "name", y = "mpg",
  3. fill = "cyl", # change fill color by cyl
  4. color = "white", # Set bar border colors to white
  5. palette = "jco", # jco journal color palett. see ?ggpar
  6. sort.val = "asc", # Sort the value in ascending order
  7. sort.by.groups = TRUE, # Sort inside each group
  8. x.text.angle = 90 # Rotate vertically x axis texts
  9. )
  10. bp + font("x.text", size = 8)

unnamed-chunk-4-1.png

  1. # Scatter plots (sp)
  2. sp <- ggscatter(mtcars, x = "wt", y = "mpg",
  3. add = "reg.line", # Add regression line
  4. conf.int = TRUE, # Add confidence interval
  5. color = "cyl", palette = "jco", # Color by groups "cyl"
  6. shape = "cyl" # Change point shape by groups "cyl"
  7. )+
  8. stat_cor(aes(color = cyl), label.x = 3) # Add correlation coefficient
  9. sp
  1. ## `geom_smooth()` using formula 'y ~ x'

unnamed-chunk-4-2.png

组合多张图片在一个面板上

使用ggpubr包的ggarrange()进行以下操作:

  1. ggarrange(bxp, dp, bp + rremove("x.text"),
  2. labels = c("A", "B", "C"),
  3. ncol = 2, nrow = 2)

unnamed-chunk-5-1.png

给图片添加注释

ggpubr包提供了函数annotate_figure()可以对面板中图片整体或任意图片添加一定量的注释展示效果如下图:

  1. figure <- ggarrange(sp, bp + font("x.text", size = 10),
  2. ncol = 1, nrow = 2)
  1. ## `geom_smooth()` using formula 'y ~ x'
  1. annotate_figure(figure,
  2. top = text_grob("Visualizing mpg", color = "red", face = "bold", size = 14),
  3. bottom = text_grob("Data source: \n mtcars data set", color = "blue",
  4. hjust = 1, x = 1, face = "italic", size = 10),
  5. left = text_grob("Figure arranged using ggpubr", color = "green", rot = 90),
  6. right = "I'm done, thanks :-)!",
  7. fig.lab = "Figure 1", fig.lab.face = "bold"
  8. )

unnamed-chunk-6-1.png

图片对齐

最经典的例子就是在绘制生存曲线时,把risk table放在生存曲线图的下边。此时,我们需要调节表和图的大小和相对位置,使其分布更为合理

  1. # Fit survival curves
  2. library(survival)
  3. fit <- survfit( Surv(time, status) ~ adhere, data = colon )
  4. # Plot survival curves
  5. library(survminer)
  6. ggsurv <- ggsurvplot(fit, data = colon,
  7. palette = "jco", # jco palette
  8. pval = TRUE, pval.coord = c(500, 0.4), # Add p-value
  9. risk.table = TRUE # Add risk table
  10. )
  1. ## Warning: Vectorized input to `element_text()` is not officially supported.
  2. ## Results may be unexpected or may change in future versions of ggplot2.
  1. ggarrange(ggsurv$plot, ggsurv$table, heights = c(2, 0.7),
  2. ncol = 1, nrow = 2)

unnamed-chunk-7-1.png

此时,我们可以看出图和表并不是完全垂直对齐的,要对齐他们需要调用align参数

  1. ggarrange(ggsurv$plot, ggsurv$table, heights = c(2, 0.7),
  2. ncol = 1, nrow = 2, align = "v")

unnamed-chunk-8-1.png

改变图形的行列布局排布

使用ggpubr

我们将使用嵌套的ggarrange()函数来更改图的列/行跨度。

例如,我们使用下边的代码可以实现这样的布局

  • 散点图(sp)将位于第一行并跨越两列
  • 箱形图(bxp)和点图(dp)将首先排列,并且将在第二行中生活两列不同的列
  1. ggarrange(sp, # First row with scatter plot
  2. ggarrange(bxp, dp, ncol = 2, labels = c("B", "C")), # Second row with box and dot plots
  3. nrow = 2,
  4. labels = "A" # Labels of the scatter plot
  5. )
  1. ## `geom_smooth()` using formula 'y ~ x'

unnamed-chunk-9-1.png

使用cowplot

函数ggdraw()+ draw_plot()+ draw_plot_label()的组合可用于将图形放置在特定大小的特定位置。

ggdraw()可以初始化一个空的绘图画布,默认情况是这样的:
canva.png

draw_plot()可以将绘图放置在绘图画布上的某处:

  1. draw_plot(plot, x = 0, y = 0, width = 1, height = 1)

draw_plot_label() 可以向图的左上角添加绘图标签。它可以处理带有关联坐标的标签向量。

  1. draw_plot_label(label, x = 0, y = 1, size = 16, ...)

如果需要组合多张图形,可以通过类似于下边的代码来实现:

  1. library("cowplot")
  2. ggdraw() +
  3. draw_plot(bxp, x = 0, y = .5, width = .5, height = .5) +
  4. draw_plot(dp, x = .5, y = .5, width = .5, height = .5) +
  5. draw_plot(bp, x = 0, y = 0, width = 1, height = 0.5) +
  6. draw_plot_label(label = c("A", "B", "C"), size = 15,
  7. x = c(0, 0.5, 0), y = c(1, 1, 0.5))

unnamed-chunk-12-1.png

使用gridExtra

gridExtra包的使用方法和ggpubr包类似

  1. library("gridExtra")
  1. ##
  2. ## Attaching package: 'gridExtra'
  1. ## The following object is masked from 'package:dplyr':
  2. ##
  3. ## combine
  1. grid.arrange(sp, # First row with one plot spaning over 2 columns
  2. arrangeGrob(bxp, dp, ncol = 2), # Second row with 2 plots in 2 different columns
  3. nrow = 2)
  1. ## `geom_smooth()` using formula 'y ~ x'

unnamed-chunk-13-1.png

我们同样可以使用gird.arrange()函数中的lay_matrix参数来设置,使用方法如下:

  1. grid.arrange(bp, # bar plot spaning two columns
  2. bxp, sp, # box plot and scatter plot
  3. ncol = 2, nrow = 2,
  4. layout_matrix = rbind(c(1,1), c(2,3)))
  1. ## `geom_smooth()` using formula 'y ~ x'

unnamed-chunk-14-1.png

在上面的R代码中,layout_matrix是一个2×2矩阵(2列和2行)。 第一行全是1,这是第一幅图占的地方,横跨两列; 第二行包含分别占据一列的图2和图3

此时,如果需要对图形进行注释,ggpubr包已经不足以实现,需要我们使用cowplot包来进行进一步的注释

需要注意的是grid.arrange()/ arrangeGrob()输出的是一个gtable,首先需要使用ggpubr包中的as_ggplot()函数将其转换为ggplot。 接下来,就可以使用函数draw_plot_label()对其进行注释。

  1. library("gridExtra")
  2. library("cowplot")
  3. # Arrange plots using arrangeGrob
  4. # returns a gtable (gt)
  5. gt <- arrangeGrob(bp, # bar plot spaning two columns
  6. bxp, sp, # box plot and scatter plot
  7. ncol = 2, nrow = 2,
  8. layout_matrix = rbind(c(1,1), c(2,3)))
  1. ## `geom_smooth()` using formula 'y ~ x'
  1. # Add labels to the arranged plots
  2. p <- as_ggplot(gt) + # transform to a ggplot
  3. draw_plot_label(label = c("A", "B", "C"), size = 15,
  4. x = c(0, 0, 0.5), y = c(1, 0.5, 0.5)) # Add labels
  5. p

unnamed-chunk-15-1.png

在上面的R代码中,我们使用了arrangeGrob()而不是grid.arrange()。 请注意,这两个函数的主要区别在于,grid.arrange()会自动绘制排列好的图 由于我们要在绘制之前注释已排列的图,因此在这种情况下,函数arrangeGrob()是首选。

使用grid

通过函数grid.layout(),可以使用gridR包创建复杂的图像布局。 它还提供了函数viewport()来定义布局上图像的区域或位置。 函数print()用于将图放置在指定区域中。

总结起来共以下5个顺序步骤:

  1. 创建绘图:p1,p2,p3,…
  2. 使用函数grid.newpage()创建画板
  3. 创建画板布局2X2 - 列数= 2; 行数= 2
  4. 定义图像输出位置:画板中图像显示的区域
  5. 在画板中输出显示图片
  1. library(grid)
  2. # Move to a new page
  3. grid.newpage()
  4. # Create layout : nrow = 3, ncol = 2
  5. pushViewport(viewport(layout = grid.layout(nrow = 3, ncol = 2)))
  6. # A helper function to define a region on the layout
  7. define_region <- function(row, col){
  8. viewport(layout.pos.row = row, layout.pos.col = col)
  9. }
  10. # Arrange the plots
  11. print(sp, vp = define_region(row = 1, col = 1:2)) # Span over two columns
  1. ## `geom_smooth()` using formula 'y ~ x'
  1. print(bxp, vp = define_region(row = 2, col = 1))
  2. print(dp, vp = define_region(row = 2, col = 2))
  3. print(bp + rremove("x.text"), vp = define_region(row = 3, col = 1:2))

unnamed-chunk-16-1.png

通用图例的设定

在我们绘制的图片当中,有的可能具有相同的图例,函数ggarrange()可以与以下参数一起使用,使两幅图使用相同的图例

  • common.legend = TRUE:使用公共图例
  • legend:指定图例位置。 允许的值包括c(“top”,“bottom”,“left”,“right”)
  1. ggarrange(bxp, dp, labels = c("A", "B"),
  2. common.legend = TRUE, legend = "bottom")

unnamed-chunk-17-1.png

实战

给散点图添加边界密度图

  1. # Scatter plot colored by groups ("Species")
  2. sp <- ggscatter(iris, x = "Sepal.Length", y = "Sepal.Width",
  3. color = "Species", palette = "jco",
  4. size = 3, alpha = 0.6)+
  5. border()
  6. # Marginal density plot of x (top panel) and y (right panel)
  7. xplot <- ggdensity(iris, "Sepal.Length", fill = "Species",
  8. palette = "jco")
  9. yplot <- ggdensity(iris, "Sepal.Width", fill = "Species",
  10. palette = "jco")+
  11. rotate()
  12. # Cleaning the plots
  13. yplot <- yplot + clean_theme()
  14. xplot <- xplot + clean_theme()
  15. # Arranging the plot
  16. ggarrange(xplot, NULL, sp, yplot,
  17. ncol = 2, nrow = 2, align = "hv",
  18. widths = c(2, 1), heights = c(1, 2),
  19. common.legend = TRUE)

unnamed-chunk-18-1.png


Reference:

ggplot2 - Easy Way to Mix Multiple Graphs on The Same Page