r
作者

zsc

发布日期

2018年7月15日

在一个页面上自由组合各种图(可以毫无关联),省空间,又能表达自己需求。

Show the code
require(ggplot2)
require(grid)
#####现将图画好,并且赋值变量,储存#####
p1 <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point()
p2 <- ggplot(diamonds, aes(carat, depth, colour = color)) + geom_point()
p3 <- ggplot(diamonds, aes(carat, depth, colour = color)) + geom_point() + facet_grid(.~color,scale = "free")
p1

Show the code
p2

Show the code
p3

这里有三张图p1,p1,p3,我想把它输出到一个页面上,不单独输出,可以用一些方法

方法一: 使用grid包

Show the code
########新建画图页面###########
grid.newpage()  ##新建页面
pushViewport(viewport(layout = grid.layout(2,2))) ####将页面分成2*2矩阵
vplayout <- function(x,y){
  viewport(layout.pos.row = x, layout.pos.col = y)
}
print(p3, vp = vplayout(1,1:2))   ###将(1,1)和(1,2)的位置画图p3
print(p2, vp = vplayout(2,1))   ###将(2,1)的位置画图p2
print(p1, vp = vplayout(2,2))  ###将(2,2)的位置画图p1

Show the code
#dev.off() ##画下一幅图,记得关闭窗口

方法二: 使用patchwork包

这个包需要从github上安装

devtools::install_github("thomasp85/patchwork")
Show the code
library(ggplot2)
library(patchwork)
p1 <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point()
p2 <- ggplot(diamonds, aes(carat, depth, colour = color)) + geom_point()
p1 + p2 #把图p1 ,p2画在一页上

Show the code
# 可以直接进行拼凑
ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point() + 
  ggplot(diamonds, aes(carat, depth, colour = color)) + geom_point()# 默认的页面布局是1行多列

使用plot_layout()函数调整页面布局

Show the code
p1 + p2 + plot_layout(ncol = 1, heights = c(3, 1))

图表之间添加或删除空格–plot_spacer()

Show the code
p1 + plot_spacer() + p2

拼凑提供的一个非常有用的功能是它使用户能够创建“子图”

Show the code
p3 <- ggplot(mtcars) + geom_smooth(aes(disp, qsec), method = 'loess',formula= y ~ x)
p4 <- ggplot(mtcars) + geom_bar(aes(carb))

p4 + {
    p1 + {
        p2 +p3 +plot_layout(ncol = 1)
      }
  } +plot_layout(ncol = 1)

高级功能

拼凑的有趣之处在于您可以使用+,-运算符来定义嵌套级别:

Show the code
p1 + p2 + p3 + plot_layout(ncol = 1)

Show the code

# 看看下面的代码并注意到现在p1和p2是“嵌套的”:
p1 + p2 - p3 + plot_layout(ncol = 1)

Show the code


## 接下来两个操作是|和/分别用于水平和垂直布局。您可以在同一操作中使用它们
(p1 | p2 | p3) /p4

最后,您可以使用*而不必为每个单独的绘图设置布局设置。这是非常有用的功能,特别是如果您需要将多个图组合在一起。 *修改当前嵌套级别的图:

Show the code
(p1 + (p2 + p3) + p4 + plot_layout(ncol = 1)) * theme_bw() # * 只会影响当前嵌套级别的图

Show the code
p1 + (p2 + p3) + p4 + plot_layout(ncol = 1) & theme_bw() # & 将递归到嵌套级别

Show the code
sessionInfo()
#> R version 4.2.1 (2022-06-23)
#> Platform: aarch64-apple-darwin20 (64-bit)
#> Running under: macOS Monterey 12.5.1
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> attached base packages:
#> [1] grid      stats     graphics  grDevices utils     datasets  methods  
#> [8] base     
#> 
#> other attached packages:
#> [1] patchwork_1.1.2 ggplot2_3.3.6  
#> 
#> loaded via a namespace (and not attached):
#>  [1] pillar_1.8.1      compiler_4.2.1    tools_4.2.1       digest_0.6.29    
#>  [5] lattice_0.20-45   nlme_3.1-159      viridisLite_0.4.1 jsonlite_1.8.0   
#>  [9] evaluate_0.16     lifecycle_1.0.1   tibble_3.1.8      gtable_0.3.0     
#> [13] mgcv_1.8-40       pkgconfig_2.0.3   rlang_1.0.4       Matrix_1.4-1     
#> [17] cli_3.3.0         DBI_1.1.3         rstudioapi_0.14   yaml_2.3.5       
#> [21] xfun_0.32         fastmap_1.1.0     withr_2.5.0       stringr_1.4.1    
#> [25] dplyr_1.0.9       knitr_1.40        generics_0.1.3    htmlwidgets_1.5.4
#> [29] vctrs_0.4.1       tidyselect_1.1.2  glue_1.6.2        R6_2.5.1         
#> [33] fansi_1.0.3       rmarkdown_2.16.1  farver_2.1.1      purrr_0.3.4      
#> [37] magrittr_2.0.3    splines_4.2.1     scales_1.2.1      htmltools_0.5.3  
#> [41] assertthat_0.2.1  colorspace_2.0-3  labeling_0.4.2    utf8_1.2.2       
#> [45] stringi_1.7.8     munsell_0.5.0