R可視化——R語言中的多子圖拼接指南

繪制子圖

1、加載繪圖包
#加載繪圖包
library(ggplot2)
library(ggprism)
library(reshape)
library(ggpubr)
2、加載并處理數據
#數據
df <- data.frame(A = c(1, 2, 3, 4, 5, 6, 8, 12, 13),
                 B = c(1, 2, 4, 5, 7, 10,5,6,8),
                 C = c(1, 5, 6, 7, 8, 9, 10, 11, 13),
                 D = c(1, 2, 5, 8, 10, 13,5,8,6))

df1=melt(df)
colnames(df1)=c("group","value")
image.png
3、繪制子圖
# fig.1
p1<-ggplot(df1,aes(x=group,y=value))+
  stat_boxplot(geom = "errorbar", width=0.1)+
  geom_boxplot(aes(fill=group), 
               outlier.colour="white")+
  geom_jitter(width = 0.2)+
  theme_prism(palette = "candy_bright",
              base_fontface = "plain",
              base_family = "serif",
              base_size = 16,
              base_line_size = 0.8, 
              axis_text_angle = 45)+
  scale_fill_prism(palette = "candy_bright")+
  theme(legend.position="none")
p1
image.png
# fig.2
p2<-ggplot( df1, aes(x = group, y=value,color=group,fill=group) ) +
  geom_bar(stat="summary",fun=mean,position="dodge")+
  stat_summary(fun.data = 'mean_sd', geom = "errorbar", width = 0.3)+
  labs(x="Samples",y=NULL)+
  theme_prism(palette = "candy_bright",
              base_fontface = "plain",
              base_family = "serif",
              base_size = 16,
              base_line_size = 0.8, 
              axis_text_angle = 45)+
  scale_fill_prism(palette = "candy_bright")+
  theme(legend.position="none")
p2
image.png
# fig.3
p3<-ggplot(df1, aes(x = group, y=value,fill=group))+
  geom_violin(trim = T,position = position_dodge(width = 0.1))+
  geom_boxplot(alpha=1,outlier.size=0, size=0.3, width=0.2,fill="white")+
  stat_summary(fun="mean",geom="point",shape=21, size=2,fill="blue")+
  labs(x="Samples",y=NULL)+
  theme_prism(palette = "flames",
              base_fontface = "plain",
              base_family = "serif",
              base_size = 16, 
              base_line_size = 0.8,
              axis_text_angle = 45)+
  scale_fill_prism(palette = "candy_bright")+
  theme(legend.position = 'none')
p3
image.png
# fig.4
p4<-ggplot(df) +
  geom_segment(aes(x=A, xend=A, y=0, yend=B), color="grey",size=1) +
  geom_point( aes(x=A, y=B,color=A), size=4 ) +
  theme_prism(palette = "pearl",
              base_fontface = "plain", 
              base_family = "serif",
              base_size = 14,
              base_line_size = 0.8, 
              axis_text_angle = 45) +
  theme(legend.position = "none") +
  ggtitle("Lollipop Chart")
p4
image.png

cowplot包實現多子圖拼接

cowplot::plot_grid(p1,p2,p3,p4,ncol = 2)#按照兩行排列
image.png
cowplot::plot_grid(p1,p2,p3,p4,ncol = 1)#一列排列
image.png
cowplot::plot_grid(p1,p2,p3,p4,ncol = 4)#橫向排列
image.png

gridExtra包實現多子圖拼接

# 圖形在這里就不做展示了,與cowplot包拼接一樣
gridExtra::grid.arrange(p1,p2,p3,p4,
                        layout_matrix=rbind(c(1,2),c(3,4)))#將圖形分割為4個區域,上面兩個放置圖1與圖2,下面兩個區域則放置圖3與圖4
gridExtra::grid.arrange(p1,p2,p3,p4,
                        layout_matrix=rbind(c(1,2,3,4)))#橫向排列
gridExtra::grid.arrange(p1,p2,p3,p4,
                        layout_matrix=rbind(c(1),c(2),c(3),c(4)))#縱向排列

patchwork包實現自定義布局拼圖

p1/p2#上下拼圖
image.png
p1+p2#左右拼圖
image.png
(p1+p2)/p3#混合拼圖
image.png
p1+p2+p3/p4#混合拼圖(不用括號則優先上下拼圖再進行左右拼圖)
image.png
p1+p2+p3+p4+
  patchwork::plot_layout(design = "
                         AB
                         CD
                         ")#分成四份,按照正方形布局排列
image.png
p1+p2+p3+p4+
  patchwork::plot_layout(design = "
                         ABCD
                         ")#分成四份,橫向排列
image.png
p1+p2+p3+p4+
  patchwork::plot_layout(design = "
                         A
                         B
                         C
                         D
                         ")#分成四份,縱向排列
image.png
p1+p2+p3+p4+
  patchwork::plot_layout(design = "
                         AABBBB#
                         AA###D#
                         ##CC###
                         ##CC###
                         ")#自定義布局
image.png

給大家找了一篇寫的比較好的介紹R語言圖形組合的文章,大家可以參考:
https://blog.csdn.net/kMD8d5R/article/details/85182184

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容