本文轉載自微信公眾號 “生信補給站”,https://mp.weixin.qq.com/s/41iKTulTwGcY-dHtqqSnLA
多個圖形進行組圖展示,可以既展示一個“事情”的多個角度,也可以進行異同的比較,同時也是發表論文所必須的。
可以利用PS或者AI進行處理,但是圖形的大小,位置,布局,字體等的調整也不是一個小工程。本文利用R包 - ggpubr 函數從0開始介紹組圖的合並方式,也許…… 比AI或者PS更簡單易學呢。
基礎函數進行組圖合並可參考[R |繪圖邊距及布局
一 載入數據,R包
加載函數包及數據集
#install.packages("ggpubr")
library(ggpubr)
# ToothGrowth數據集
data("ToothGrowth")
head(ToothGrowth)
len supp dose
1 4.2 VC 0.5
2 11.5 VC 0.5
3 7.3 VC 0.5
4 5.8 VC 0.5
5 6.4 VC 0.5
6 10.0 VC 0.5
# mtcars 數據集
data("mtcars")
mtcars$name <- rownames(mtcars)
mtcars$cyl <- as.factor(mtcars$cyl)
head(mtcars[, c("name", "wt", "mpg", "cyl")])
name wt mpg cyl
Mazda RX4 Mazda RX4 2.620 21.0 6
Mazda RX4 Wag Mazda RX4 Wag 2.875 21.0 6
Datsun 710 Datsun 710 2.320 22.8 4
Hornet 4 Drive Hornet 4 Drive 3.215 21.4 6
Hornet Sportabout Hornet Sportabout 3.440 18.7 8
Valiant Valiant 3.460 18.1 6
創建用於圖形組合的圖:
#箱線圖
Box_plot <- ggboxplot(ToothGrowth, x = "dose", y = "len",color = "dose", palette = "jco")
#點圖
Dot_plot <- ggdotplot(ToothGrowth, x = "dose", y = "len", color = "dose", palette = "jco", binwidth = 1)
#有序條形圖
Bar_plot <- ggbarplot(mtcars, x = "name", y = "mpg",
fill = "cyl", # change fill color by cyl
color = "white", # Set bar border colors to whit
palette = "jco", # jco journal color palett. see ?ggpar
sort.val = "asc", # Sort the value in ascending order
sort.by.groups = TRUE, # Sort inside each group
x.text.angle = 90 # Rotate vertically x axis texts
) + font("x.text", size = 8)
# 散點圖
Scatter_plots <- ggscatter(mtcars, x = "wt", y = "mpg",
add = "reg.line", # Add regression line
conf.int = TRUE, # Add confidence interval
color = "cyl", palette = "jco", # Color by groups "cyl"
shape = "cyl" # Change point shape by groups "cyl"
) + stat_cor(aes(color = cyl), label.x = 3) # Add correlation coefficien
二 圖形組合
使用ggpubr包的函數ggarrange()中在一頁上進行組合展示
1)ToothGrowth數據集的箱線圖,點圖 組合展示
ggarrange(Box_plot, Dot_plot,labels = c("A", "B"),ncol = 2, nrow = 1)
#圖的邊緣放置共同的唯一圖例:common.legend = TRUE參數
ggarrange(bxp, dp, labels = c("A", "B"), common.legend = TRUE, legend = "bottom")
2)mtcars 數據集的條形圖,散點圖組合展示
figure <- ggarrange(Scatter_plots, Bar_plot + font("x.text", size = 10),ncol = 1, nrow = 2)
添加圖形的注釋信息(標題,副標題,坐標軸,字體,顏色等)
annotate_figure(figure,
top = text_grob("Visualizing mpg", color = "red", face = "bold", size = 14),
bottom = text_grob("Data source: mtcars data set", color = "blue",
hjust = 1, x = 1, face = "italic", size = 10),
left = text_grob("Figure arranged using ggpubr", color = "green", rot = 90),
right = "Here )!",
fig.lab = "Figure 1", fig.lab.face = "bold"
)
3)ggarrange()函數更改繪圖的列/行跨度
散點圖在第一行跨兩列,箱形圖和點圖並於第二行
ggarrange(Scatter_plots, # First row with scatter plot
ggarrange(Box_plot, Dot_plot, ncol = 2, labels = c("B", "C")), # Second row with box and dot plots
nrow = 2,
labels = "A" # Labels of the scatter plot
)
4)利用NULL構建空白圖
示例:繪制具有邊際密度圖的散點圖
繪制主要散點圖
Scatter_plots <- ggscatter(iris, x = "Sepal.Length", y = "Sepal.Width",
color = "Species", palette = "jco",
size = 3, alpha = 0.6)+ border()
上側,右側添加密度圖
xplot <- ggdensity(iris, "Sepal.Length", fill = "Species",
palette = "jco")
yplot <- ggdensity(iris, "Sepal.Width", fill = "Species",
palette = "jco")+ rotate()
# 設置主題
yplot <- yplot + clean_theme()
xplot <- xplot + clean_theme()
# 通過width和height參數調整圖的大小
# 利用NULL設置空白圖
ggarrange(xplot, NULL, Scatter_plots, yplot,
ncol = 2, nrow = 2, align = "hv",
widths = c(2, 1), heights = c(1, 2),
common.legend = TRUE)
5)添加統計圖表及文本信息
繪制變量“Sepal.Length” 的密度圖以及描述性統計(mean,sd,…)的匯總表。
Sepal.Length密度圖
density.p <- ggdensity(iris, x = "Sepal.Length",
fill = "Species", palette = "jco")
#Sepal.Length描述性統計
stable <- desc_statby(iris, measure.var = "Sepal.Length",
grps = "Species")
stable <- stable[, c("Species", "length", "mean", "sd")]
#設置table的主題
stable.p <- ggtexttable(stable, rows = NULL,
theme = ttheme("mOrange"))
# text 信息
text <- paste("iris data set gives the measurements in cm",
"of the variables sepal length and width",
"and petal length and width, reScatter_plotsectively,",
"for 50 flowers from each of 3 Scatter_plotsecies of iris.",
"The Scatter_plotsecies are Iris setosa, versicolor, and virginica.", sep = " ")
text.p <- ggparagraph(text = text, face = "italic", size = 11, color = "black")
組圖展示,調整高度和寬度
ggarrange(density.p, stable.p, text.p,
ncol = 1, nrow = 3,
heights = c(1, 0.5, 0.3))
子母圖展示
density.p + annotation_custom(ggplotGrob(stable.p),
xmin = 5.5, ymin = 0.7,
xmax = 8)
6)嵌套布局展示
p1 <- ggarrange(Scatter_plots, Bar_plot + font("x.text", size = 9),
ncol = 1, nrow = 2)
p2 <- ggarrange(density.p, stable.p, text.p,
ncol = 1, nrow = 3,
heights = c(1, 0.5, 0.3))
#先組合P1,P2,然后自定義行 列 ,嵌套組合展示
ggarrange(p1, p2, ncol = 2, nrow = 1)
參考鏈接:
【關注“生信補給站”公眾號,對話框回復 R組圖 即可獲得上述組圖R代碼】
更多關於生信,R,Python的內容請掃碼關注“生信補給站”小號,有驚喜!!!😁