ggpubr: 'ggplot2' Based Publication Ready Plots
一款基于ggplot2的可視化包ggpubr,能夠一行命令繪制出符合出版物要求的圖形截碴。
ggplot2 by?Hadley Wickham?is an excellent and flexible package for elegant data visualization in R. However the default generated plots requires some formatting before we can send them for publication. Furthermore, to customize a ggplot, the syntax is opaque and this raises the level of difficulty for researchers with no advanced R programming skills. The 'ggpubr' package provides some easy-to-use functions for creating and customizing 'ggplot2'- based publication ready plots.
1. R包的安裝及加載
ggpubr包可以從CRAN或GitHub中進(jìn)行下載安裝
Install from?CRAN?as follow:
install.packages("ggpubr")
library(ggpubr)
Or, install the latest version from?GitHub?as follow:
if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/ggpubr")
library(ggpubr)
2. 常用基本圖形的繪制
分布圖繪制(Distribution)
01. 帶有均值線和地毯線的密度圖
#構(gòu)建數(shù)據(jù)集
set.seed(1234)
df <- data.frame( sex=factor(rep(c("f", "M"), each=200)),
? ? ? ? ? ? ? ? ?weight=c(rnorm(200, 55), rnorm(200, 58)))
# 預(yù)覽數(shù)據(jù)格式
head(df)
# 繪制密度圖
ggdensity(df, x="weight", add = "mean", rug = TRUE, color = "sex", fill = "sex",
? ? ? ? ?palette = c("#00AFBB", "#E7B800")) #rug參數(shù)添加地毯線孔庭,add參數(shù)可以添加均值mean和中位數(shù)median
圖1. 密度圖展示不同性別分組下體重的分布尺上,X軸為體重,Y軸為自動(dòng)累計(jì)的密度圆到,X軸上添加地毯線進(jìn)一步呈現(xiàn)樣本的分布尖昏;按性別分別組標(biāo)記輪廓線顏色,再按性別填充色展示各組的分布构资,使用palette自定義顏色抽诉,是不是很舒服。
02. 帶有均值線和邊際地毯線的直方圖
gghistogram(df, x="weight", add = "mean", rug = TRUE, color = "sex", fill = "sex",
? ? ? ? ? ?palette = c("#00AFBB", "#E7B800"))
圖2. 帶有均值線和邊際地毯線的直方圖吐绵,只是把密度比例還原為了原始數(shù)據(jù)counts值
箱線/小提琴圖繪制(barplot/violinplot)
01. 箱線圖+分組形狀+統(tǒng)計(jì)
#加載數(shù)據(jù)集ToothGrowth
data("ToothGrowth")
df1 <- ToothGrowth
head(df1)
p <- ggboxplot(df1, x="dose", y="len", color = "dose",
? ? ? ? ? ? ? palette = c("#00AFBB", "#E7B800", "#FC4E07"),
? ? ? ? ? ? ? add = "jitter", shape="dose") #增加了jitter點(diǎn)迹淌,點(diǎn)shape由dose映射
p
圖3. 箱線圖按組著色,同時(shí)樣本點(diǎn)標(biāo)記不同形狀可以一步區(qū)分組或批次
02. 箱線圖+分組形狀+統(tǒng)計(jì)
# 增加不同組間的p-value值己单,可以自定義需要標(biāo)注的組間比較
my_comparisons <- list(c("0.5", "1"), c("1", "2"), c("0.5", "2"))
p+stat_compare_means(comparisons = my_comparisons)+? ? #不同組間的比較
? ? ?stat_compare_means(label.y = 50)
圖4. stat_compare_means添加組間比較連線和統(tǒng)計(jì)P值
03. 內(nèi)有箱線圖的小提琴圖+星標(biāo)記
ggviolin(df1, x="dose", y="len", fill = "dose",
? ? ? ? palette = c("#00AFBB", "#E7B800", "#FC4E07"),
? ? ? ? add = "boxplot", add.params = list(fill="white"))+
?stat_compare_means(comparisons = my_comparisons, label = "p.signif")+? ?#label這里表示選擇顯著性標(biāo)記(星號(hào))
?stat_compare_means(label.y = 50)
圖5. ggviolin繪制小提琴圖唉窃, add = “boxplot”中間再添加箱線圖,stat_compare_means中纹笼,設(shè)置lable=”p.signif”纹份,即可添加星添加組間比較連線和統(tǒng)計(jì)P值按星分類。
條形/柱狀圖繪制(barplot)
data("mtcars")
df2 <- mtcars
df2$cyl <- factor(df2$cyl)
df2$name <- rownames(df2) #添加一行name
head(df2[, c("name", "wt", "mpg", "cyl")])
ggbarplot(df2, x="name", y="mpg", fill = "cyl", color = "white",
? ? ? ? ?palette = "npg", #雜志nature的配色
? ? ? ? ?sort.val = "desc", #下降排序
? ? ? ? ?sort.by.groups=FALSE, #不按組排序
? ? ? ? ?x.text.angle=60)
圖6. 柱狀圖展示不同車的速度廷痘,按cyl為分組信息進(jìn)行填充顏色蔓涧,顏色按nature配色方法(支持 ggsci包中的本色方案,如: “npg”, “aaas”, “l(fā)ancet”, “jco”, “ucscgb”, “uchicago”, “simpsons” and “rickandmorty”)笋额,按數(shù)值降序排列元暴。
# 按組進(jìn)行排序
ggbarplot(df2, x="name", y="mpg", fill = "cyl", color = "white",
? ? ? ? ?palette = "aaas", #雜志Science的配色
? ? ? ? ?sort.val = "asc", #上升排序,區(qū)別于desc,具體看圖演示
? ? ? ? ?sort.by.groups=TRUE,x.text.angle=60) #按組排序 x.text.angle=90
圖7. 由上圖中顏色改為Sciences配色方案兄猩,按組升序排布茉盏,且調(diào)整x軸標(biāo)簽60度角防止重疊。
偏差圖繪制(Deviation graphs)
偏差圖展示了與參考值之間的偏差
df2$mpg_z <- (df2$mpg-mean(df2$mpg))/sd(df2$mpg)????# 相當(dāng)于Zscore標(biāo)準(zhǔn)化枢冤,減均值鸠姨,除標(biāo)準(zhǔn)差
df2$mpg_grp <- factor(ifelse(df2$mpg_z<0, "low", "high"), levels = c("low", "high"))
head(df2[, c("name", "wt", "mpg", "mpg_grp", "cyl")])
ggbarplot(df2, x="name", y="mpg_z", fill = "mpg_grp", color = "white",
? ? ? ? ?palette = "jco", sort.val = "asc", sort.by.groups = FALSE,
? ? ? ? ?x.text.angle=60, ylab = "MPG z-score", xlab = FALSE, legend.title="MPG Group")
圖8. 基于Zscore的柱狀圖,就是原始值減均值淹真,再除標(biāo)準(zhǔn)差讶迁。按jco雜志配色方案,升序排列趟咆,不按組排列添瓷。
# 坐標(biāo)軸變換
ggbarplot(df2, x="name", y="mpg_z", fill = "mpg_grp", color = "white",
? ? ? ? ?palette = "jco", sort.val = "desc", sort.by.groups = FALSE,
? ? ? ? ?x.text.angle=90, ylab = "MPG z-score", xlab = FALSE,
? ? ? ? ?legend.title="MPG Group", rotate=TRUE, ggtheme = theme_minimal()) ????# rotate設(shè)置x/y軸對(duì)換
圖9. rotate=TRUE翻轉(zhuǎn)坐標(biāo)軸,柱狀圖秒變條形圖
棒棒糖圖繪制(Lollipop chart)
棒棒圖可以代替條形圖展示數(shù)據(jù)
ggdotchart(df2, x="name", y="mpg", color = "cyl",
? ? ? ? ? palette = c("#00AFBB", "#E7B800", "#FC4E07"),
? ? ? ? ? sorting = "ascending",
? ? ? ? ? add = "segments", ggtheme = theme_pubr())
圖10. 柱狀圖太多了單調(diào)值纱,改用棒棒糖圖添加多樣性
設(shè)置其他參數(shù)
ggdotchart(df2, x="name", y="mpg", color = "cyl",
? ? ? ? ? palette = c("#00AFBB", "#E7B800", "#FC4E07"),
? ? ? ? ? sorting = "descending", add = "segments", rotate = TRUE,
? ? ? ? ? group = "cyl", dot.size = 6,
? ? ? ? ? label = round(df2$mpg), font.label = list(color="white",
? ? ? ? ? size=9, vjust=0.5), ggtheme = theme_pubr())
圖11. 棒棒糖圖簡(jiǎn)單調(diào)整鳞贷,rotate = TRUE轉(zhuǎn)換坐標(biāo)軸, dot.size = 6調(diào)整糖的大小虐唠,label = round()添加糖心中的數(shù)值搀愧,font.label進(jìn)一步設(shè)置字體樣式
棒棒糖偏差圖
ggdotchart(dfm, x = "name", y = "mpg_z",
????????????????????color = "cyl", # Color by groups
????????????????????palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
????????????????????sorting = "descending", # Sort value in descending order
????????????????????add = "segments", # Add segments from y = 0 to dots
????????????????????add.params = list(color = "lightgray", size = 2), # Change segment color and size
????????????????????group = "cyl", # Order by groups
????????????????????dot.size = 6, # Large dot size
????????????????????label = round(dfm$mpg_z,1), # Add mpg values as dot labels,設(shè)置一位小數(shù)
????????????????????font.label = list(color = "white", size = 9, vjust = 0.5), # Adjust label parameters
? ? ? ? ? ? ? ? ? ? ggtheme = theme_pubr() # ggplot2 theme
????????????????????)+
????????????????????geom_hline(yintercept = 0, linetype = 2, color = "lightgray")
圖12. 同柱狀圖類似疆偿,用Z-score的值代替原始值繪圖咱筛。
Cleveland點(diǎn)圖繪制
ggdotchart(dfm, x = "name", y = "mpg",
????????????????????color = "cyl", # Color by groups
????????????????????palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
????????????????????sorting = "descending", # Sort value in descending order
????????????????????rotate = TRUE, # Rotate vertically
????????????????????dot.size = 2, # Large dot size
????????????????????y.text.col = TRUE, # Color y text by groups
????????????????????ggtheme = theme_pubr() # ggplot2 theme
????????????????????)+
????????????????????theme_cleveland() # Add dashed grids
圖13. theme_cleveland()主題可設(shè)置為Cleveland點(diǎn)圖樣式
3. 常用基本繪圖函數(shù)及參數(shù)
基本繪圖函數(shù)
gghistogram????????Histogram plot #繪制直方圖
ggdensity????????Density plot #繪制密度圖
ggdotplot????????Dot plot #繪制點(diǎn)圖
ggdotchart????????Cleveland's Dot Plots #繪制Cleveland點(diǎn)圖
ggline????????Line plot #繪制線圖
ggbarplot????????Bar plot #繪制柱狀圖
ggstripchart????????Stripcharts #繪制帶狀圖
ggboxplot????????Box plot #繪制箱線圖
ggviolin????????Violin plot #繪制小提琴圖
ggpie????????Pie chart #繪制餅圖
ggqqplot????????QQ Plots #繪制QQ圖
ggscatter????????Scatter plot #繪制散點(diǎn)圖
ggmaplot????????MA-plot from means and log fold changes #繪制M-A圖
ggpaired????????Plot Paired Data #繪制散點(diǎn)圖矩陣
ggerrorplot????????Visualizing Error #繪制誤差圖
基本參數(shù)
ggtext????????Text #添加文本
border????????Set ggplot Panel Border Line #設(shè)置畫(huà)布邊框
grids????????Add Grids to a ggplot #添加網(wǎng)格線
font????????Change the Appearance of Titles and Axis Labels #設(shè)置字體類型
bgcolor????????Change ggplot Panel Background Color #更改畫(huà)布背景顏色
background_image????????Add Background Image to ggplot2 #添加背景圖片
facet????????Facet a ggplot into Multiple Panels #設(shè)置分面
ggpar????????Graphical parameters #添加畫(huà)圖參數(shù)
ggparagraph????????Draw a Paragraph of Text #添加文本段落
ggtexttable????????Draw a Textual Table #添加文本表格
ggadd????????Add Summary Statistics or a Geom onto a ggplot #添加基本統(tǒng)計(jì)結(jié)果或其他幾何圖形
ggarrange????????Arrange Multiple ggplots #排版多個(gè)圖形
gradient_color????????Set Gradient Color #設(shè)置連續(xù)型顏色
xscale????????Change Axis Scale: log2, log10 and more #更改坐標(biāo)軸的標(biāo)度
add_summary????????Add Summary Statistics onto a ggplot #添加基本統(tǒng)計(jì)結(jié)果
set_palette????????Set Color Palette #設(shè)置畫(huà)板顏色
rotate????????Rotate a ggplot Horizontally #設(shè)置圖形旋轉(zhuǎn)
rotate_axis_text????????Rotate Axes Text #旋轉(zhuǎn)坐標(biāo)軸文本
stat_stars????????Add Stars to a Scatter Plot #添加散點(diǎn)圖星標(biāo)
stat_cor????????Add Correlation Coefficients with P-values to a Scatter Plot #添加相關(guān)系數(shù)
stat_compare_means????????Add Mean Comparison P-values to a ggplot #添加平均值比較的P值
theme_transparent????????Create a ggplot with Transparent Background #設(shè)置透明背景
theme_pubr????????Publication ready theme #設(shè)置出版物主題
參考來(lái)源:https://www.rdocumentation.org/packages/ggpubr/versions/0.1.4
? ? ? ? ? ? ? ? ? https://mp.weixin.qq.com/s/ZKxzKZ4NBTcsJ6vFimxoGA
? ? ? ? ? ? ? ? ? http://blog.sciencenet.cn/blog-3334560-1091714.html