r语言学习ggplot2绘图R - tips

R成精系列-一页多图

2018-09-07  本文已影响252人  果果哥哥BBQ

在许多时候,我们需要将多张图表放在一起,当然我们可以用PS来处理,但是作为一个R的Master,怎么可能容忍这种事情发生。本文总结了如何在一页放多张绘画,供大家参考。在网友的提醒下,从新梳理了方法。

方法一:multiplot function

用函数来处理,但是效果不是很好。
This is the definition of multiplot. It can take any number of plot objects as arguments, or if it can take a list of plot objects passed to plotlist.

# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols:   Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  library(grid)

  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)

  numPlots = length(plots)

  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                    ncol = cols, nrow = ceiling(numPlots/cols))
  }

 if (numPlots==1) {
    print(plots[[1]])

  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))

      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}

方法二:用ggpubr包

ggpubr包的全称为'ggplot2' Based Publication Ready Plots,基于ggplot2的出版级图形制作包。
Hadley Wickham创建的可视化包ggplot2可以流畅地进行优美的可视化,但是如果要通过ggplot2定制一套图形,尤其是适用于杂志期刊等出版物的图形,对于那些没有深入了解ggplot2的人来说就有点困难了,ggplot2的部分语法是很晦涩的。为此Alboukadel Kassambara创建了基于ggplot2的可视化包ggpubr用于绘制符合出版物要求的图形。
ggpubr的主要特点:
- ggpubr改写了一些ggplot的图形函数,省去了ggplot中许多晦涩的语法,对于初学者来说使用起来更容易。
-能够让非高级R用户绘制准出版级的精美图形;
- 更简单地设置图形的参数,包括颜色、标签;
- 轻松实现一页多图,添加图形注释;
- 为条形图、箱线图、线图等自动添加p值和显著性水平;
- ……
先放一张图


一页放多图
#安装ggpubr
library(ggpubr)
#未安装请用install.packages("ggpubr")安装
library(ggplot2)
library(tibble)
#生成图形数据
x<-1:1000
y<-1:1000
n<-rnorm(1000,0.5,1)
z<--x*(x-500)
data<-as.tibble(cbind(x,y,z,n,f))
#生成p1-p6六个ggplot图形对象
p1 <- ggplot(data = data,aes(x=n))+geom_density()
p2 <- ggplot(data = data,aes(x=n,y=y,color=x))+geom_point()
p3 <- ggplot(data = data,aes(x=x,y=z,color=x))+geom_line()+geom_point(x=x,y=n*100)
p4 <- ggplot(data = data,aes(x=n,color="red"))+geom_histogram()
p5 <- ggplot(data = data,aes(x=n,y=y*n,color=x))+geom_point()
p6 <- ggplot(data = data,aes(x=n))+geom_histogram(bins = 20,fill=1:20)+coord_polar()
#利用ggarrange将图像排列在一起
ggarrange(p1,p2,p3,p4,p5,p6,ncol = 2,nrow =3,widths = c(1,2),heights = c(1,1,2))

解读
一页放置多张图主要利用ggarrange()函数:

ggarrange(..., plotlist = NULL, ncol = NULL, nrow = NULL, labels = NULL,label.x = 0, label.y = 1, hjust = -0.5, vjust = 1.5,
font.label = list(size = 14, color = "black", face = "bold", family = NULL),
align = c("none", "h", "v", "hv"), widths = 1, heights = 1,
legend = NULL, common.legend = FALSE)

  • ...需要画的图对象,plotlist需要画的图清单,ncolnrow是要排列成几行几列
  • labels每个图的标签,label.x label.y hjust vjust font.label都是调整labels的位置参数,
  • align各个图的排列方式,是纵向还是横向排列,widthsheights调整各图的长宽比列
  • legend是各图的图例,commn.legend公共的图例

函数能画 "ggplot", "gtable", "grob", "recordedplot"等类, 或图形函数。

方法三:用gredExtra包grid.arrange函数

library(gridExtra)
#安装ggpubr时,会依赖于该包
grid.arrange(p1,p2,p3,p4,p5,p6,ncol=3,nrow=2)

方法四:用cowplot包的plot_grid函数

library("cowplot")
plot_grid(p1,p2,p3,p4,p5,p6,   labels = c("A", "B", "C","D","E","F"), ncol = 2, nrow = 3)

参考:
ggpubr: Publication Ready Plots

sessionInfo()信息

sessionInfo()

R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
locale:
[1] zh_CN.UTF-8/zh_CN.UTF-8/zh_CN.UTF-8/C/zh_CN.UTF-8/zh_CN.UTF-8
attached base packages:
 [1] grid      parallel  stats4    stats     graphics  grDevices utils     datasets  methods  
[10] base     
上一篇 下一篇

猜你喜欢

热点阅读