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,对于排版有个更大的自由,参数有很多,想了解的话直接看大佬的原文档

相关文章
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...
122 1
go package 相关介绍
go package 相关介绍
57 0
Learn R package together--(2)
介绍俩个小众但实用的包wherepackage、rPlotter
80 0
Learn R package together--(1)
ggrepel包 此包可解决标签重合问题
74 0
|
缓存 开发工具 Python
[oeasy]python0140_导入_import_from_as_namespace_
[oeasy]python0140_导入_import_from_as_namespace_
98 0
No package ‘libpeas-1.0‘ found/No package ‘libpeas-gtk-1.0‘
No package ‘libpeas-1.0‘ found/No package ‘libpeas-gtk-1.0‘
50 0
libcanberra development package not found
libcanberra development package not found
63 0
|
Python
报错:import pandas._libs.parsers as parsers RuntimeWarning
报错:import pandas._libs.parsers as parsers RuntimeWarning
126 0
【Unity3D 问题总结】Failed to import package with error: Couldn‘t decompress package
报错问题:Failed to import package with error: Couldn’t decompress package 今天在导入一个资源包的时候突然提示报错:Failed to import package with error: Couldn’t decompress package
【Unity3D 问题总结】Failed to import package with error: Couldn‘t decompress package