Learn R package together--(3)

简介: 论文中作图经常会有合并图排版需求,一般会放入PPT中或AI手动调节,若是在R中出图前直接调好岂不是更美滋滋,So本次就介绍几种关于合并图形的包cowplot、gridExtra、ggpubr、patchwork

图形合并策略

前三种策略

library(cowplot)
library(gridExtra)
library(ggpubr)
data("mtcars")
data("ToothGrowth")
rm(list = ls())
# 前三种策略
mtcars$name <- rownames(mtcars)
mtcars$cyl <- as.factor(mtcars$cyl)
mtcars
mtcars$name <- factor(mtcars$name,levels = mtcars[order(mtcars$cyl),'name'])#此处设置可使同组排列一起,调整因子的顺序
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
bxp <- ggplot(ToothGrowth,aes(x=dose,y=len,color=dose))+geom_boxplot()
dp <- ggplot(ToothGrowth,aes(x=dose,y=len,fill=dose))+geom_dotplot(binaxis='y',stackdir = 'center',
                                                               stackratio = 1.5,dotsize = 1.2)
bp <- ggplot(mtcars,aes(x=name,y=mpg,fill=cyl))+geom_bar(stat = 'identity')+theme(axis.text.x=element_text(angle = 75,hjust=1,size=8)) # 
sp <- ggplot(mtcars,aes(x=wt,y=mpg,color=cyl,shapecyl))+geom_point()+geom_smooth(method = ls,aes(fill=cyl))
plot_grid(bxp,dp,bp,sp,labels = c('A','B','C','D'),ncol = 2,nrow = 2) #cowplot
ggarrange(bxp,dp,bp,sp,labels = c('A','B','C','D'),ncol = 2,nrow = 2) #ggpubr
grid.arrange(bxp,dp,bp,sp,ncol = 2,nrow = 2) #gridExtra

5f5ef00db48b94d45ffe14be664b49d.png

第四种策略用新包patchwork

# install.packages('patchwork')
library('patchwork')
bxp+dp+bp+sp + plot_annotation(tag_levels = 'A')+ #图例都放右边
  plot_layout(guides = 'collect')& theme_minimal() #&符号可以修改所有子图主题

471705e719812350a1e168d0f261e37.png

个人比较看好patchwork,对于排版有个更大的自由,参数有很多,想了解的话直接看大佬的原文档

相关文章
|
4月前
|
Python
【Python】解决Can‘t find model ‘en‘. It doesn‘t seem to be a shortcut link, a Python package or a valid
在使用以下代码时,报错Can’t find model ‘en’. It doesn’t seem to be a shortcut link, a Python package or a valid path to a data directory.
66 1
tensorflow.python.framework.errors_impl.InvalidArgumentError
tensorflow.python.framework.errors_impl.InvalidArgumentError:logits and labels must be broadcastable: logits_size=[8,4] labels_size=[8,2]训练模型时报错,tensorflow.python.framework.errors_impl.InvalidArgu...
125 1
Learn R package together--(2)
介绍俩个小众但实用的包wherepackage、rPlotter
91 0
Learn R package together--(1)
ggrepel包 此包可解决标签重合问题
78 0
【YOLOV5-6.x讲解】常用工具类 models/common.py
【YOLOV5-6.x讲解】常用工具类 models/common.py
407 0
|
缓存 开发工具 Python
[oeasy]python0140_导入_import_from_as_namespace_
[oeasy]python0140_导入_import_from_as_namespace_
102 0
|
异构计算 Python
【谁说视频不能P】之基于PaddleHub的100行代码P阿力木视频
【谁说视频不能P】之基于PaddleHub的100行代码P阿力木视频
104 0
【谁说视频不能P】之基于PaddleHub的100行代码P阿力木视频
No package ‘libpeas-1.0‘ found/No package ‘libpeas-gtk-1.0‘
No package ‘libpeas-1.0‘ found/No package ‘libpeas-gtk-1.0‘
53 0
|
Python
报错:import pandas._libs.parsers as parsers RuntimeWarning
报错:import pandas._libs.parsers as parsers RuntimeWarning
132 0