ggpubr-专为学术绘图而生
欢迎来到医科研,这里是白介素2的读书笔记,跟我一起聊临床与科研的故事。
ggpubr-专为学术绘图而生
由Hadley Wickham创建的ggplot2(https://ggplot2.tidyverse.org/)非常好用的可视化包了,但是由ggplot2绘制的图形通常不能直接用于发表,还需要经过一定程度的编辑,对于不少那么会编程的研究人员而言可能并不是特别友好。
因此,ggpubr应运而生,它提供了简单易用的函数,用于绘制定制的高质量图,可以直接用于发表。
以下演示官方教程:
1Sys.setlocale('LC_ALL','C')
2library(ggpubr)
3set.seed(1234)
4wdata = data.frame(
5 sex = factor(rep(c("F", "M"), each=200)),
6 weight = c(rnorm(200, 55), rnorm(200, 58)))
7dim(wdata)
8head(wdata, 4)
9#> sex weight
10#> 1 F 53.79293
11#> 2 F 55.27743
12#> 3 F 56.08444
13#> 4 F 52.65430
density plot-ggdensity
color设置轮廓颜色,fill填充颜色设置
palette 颜色设置
1ggdensity(wdata, x = "weight",
2 add = "mean", rug = TRUE,
3 color = "sex",fill = "sex",
4 palette = c("#00AFBB", "#E7B800"))
频数分布图
gghistogram
1gghistogram(wdata, x = "weight",
2 add = "mean", rug = TRUE,
3 color = "sex", fill = "sex",
4 palette = c("#00AFBB", "#E7B800"))
箱线图与小提琴图
1# Load data
2data("ToothGrowth")
3df <- ToothGrowth
4head(df, 4)
5#> len supp dose
6#> 1 4.2 VC 0.5
7#> 2 11.5 VC 0.5
8#> 3 7.3 VC 0.5
9#> 4 5.8 VC 0.51p <- ggboxplot(df, x = "dose", y = "len",
2 color = "dose", palette =c("#00AFBB", "#E7B800", "#FC4E07"),
3 add = "jitter", shape = "dose")
4p
my_comparisons:指定比较方式和Pvaue
stat_compare_means增加global pvalue
1# Add p-values comparing groups
2 # Specify the comparisons you want
3my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
4p + stat_compare_means(comparisons = my_comparisons)+ # Add pairwise comparisons p-value
5 stat_compare_means(label.y = 50)
含有boxplot的小提琴图-ggviolin
label="p.signif"以星号展示pvalue
add="boxplot"在小提琴中增加箱线图
1ggviolin(df, x = "dose", y = "len", fill = "dose",
2 palette = c("#00AFBB", "#E7B800", "#FC4E07"),
3 add = "boxplot", add.params = list(fill = "white"))+
4 stat_compare_means(comparisons = my_comparisons, label = "p.signif")+ # Add significance levels
5 stat_compare_means(label.y = 50)
柱状图
1data("mtcars")
2dfm <- mtcars
3# 将cyl转换为因子
4dfm$cyl <- as.factor(dfm$cyl)
5# Add the name colums
6dfm$name <- rownames(dfm)
7# Inspect the data
8head(dfm[, c("name", "wt", "mpg", "cyl")])
9#> name wt mpg cyl
10#> Mazda RX4 Mazda RX4 2.620 21.0 6
11#> Mazda RX4 Wag Mazda RX4 Wag 2.875 21.0 6
12#> Datsun 710 Datsun 710 2.320 22.8 4
13#> Hornet 4 Drive Hornet 4 Drive 3.215 21.4 6
14#> Hornet Sportabout Hornet Sportabout 3.440 18.7 8
15#> Valiant Valiant 3.460 18.1 6
对barplot排序
按颜色填充时,并不会按分组来排序,只会进行整体排序
1ggbarplot(dfm, x = "name", y = "mpg",
2 fill = "cyl", # 按cyl填充颜色
3 color = "white", # 柱子的边界颜色设置
4 palette = "jco", # jco杂志的颜色板
5 sort.val = "desc", # 降序排列
6 sort.by.groups = FALSE, # 不按分组排序
7 x.text.angle = 90 # x轴字体旋转90度
8 )
按分组降序排列
sort.by.groups=TRUE 参数
这个比较适用于绘制GO的富集情况
1ggbarplot(dfm, x = "name", y = "mpg",
2 fill = "cyl", # change fill color by cyl
3 color = "white", # Set bar border colors to white
4 palette = "jco", # jco journal color palett. see ?ggpar
5 sort.val = "asc", # Sort the value in dscending order
6 sort.by.groups = TRUE, # 按分组内进行排序
7 x.text.angle = 90 # Rotate vertically x axis texts
8 )
偏差图-deviation plot
deviation plot会展示定量数值偏差一个参考值的,相当于对数据进行中心化的处理
以下绘制mpg的z-score,这个过程比较简单,换上自己的数据计算即可
1#计算mpg的zscore
2dfm$mpg_z <- (dfm$mpg -mean(dfm$mpg))/sd(dfm$mpg)
3#按zscore分为high, low两组
4dfm$mpg_grp <- factor(ifelse(dfm$mpg_z < 0, "low", "high"),
5 levels = c("low", "high"))
6# Inspect the data
7head(dfm[, c("name", "wt", "mpg", "mpg_z", "mpg_grp", "cyl")])
8#> name wt mpg mpg_z mpg_grp cyl
9#> Mazda RX4 Mazda RX4 2.620 21.0 0.1508848 high 6
10#> Mazda RX4 Wag Mazda RX4 Wag 2.875 21.0 0.1508848 high 6
11#> Datsun 710 Datsun 710 2.320 22.8 0.4495434 high 4
12#> Hornet 4 Drive Hornet 4 Drive 3.215 21.4 0.2172534 high 6
13#> Hornet Sportabout Hornet Sportabout 3.440 18.7 -0.2307345 low 8
14#> Valiant Valiant 3.460 18.1 -0.3302874 low 6
创建一个根据mpg的值排序的barplot
1ggbarplot(dfm, x = "name", y = "mpg_z",
2 fill = "mpg_grp", # 根据mpg值的高低填充
3 color = "white", # Set bar border colors to white
4 palette = "jco", # jco journal color palett. see ?ggpar
5 sort.val = "asc", # 升序排列
6 sort.by.groups = FALSE, # Don't sort inside each group
7 x.text.angle = 90, # Rotate vertically x axis texts
8 ylab = "MPG z-score", # y轴名称
9 xlab = FALSE, #x轴名称
10 legend.title = "MPG Group" # 图注名称
11 )
旋转图形
ggtheme参数设置主题
rotate=TRUE参数设置图形旋转
1ggbarplot(dfm, x = "name", y = "mpg_z",
2 fill = "mpg_grp", # change fill color by mpg_level
3 color = "white", # Set bar border colors to white
4 palette = "jco", # jco journal color palett. see ?ggpar
5 sort.val = "desc", # Sort the value in descending order
6 sort.by.groups = FALSE, # Don't sort inside each group
7 x.text.angle = 90, # Rotate vertically x axis texts
8 ylab = "MPG z-score",
9 legend.title = "MPG Group",
10 rotate = TRUE,
11 ggtheme = theme_minimal()
12 )
点图
Lollipop chart-棒棒糖图
棒棒糖图比较适用于有大量值需要可视化的情况
ggdotchart函数
add="segments"增加从0到点的棒子
1ggdotchart(dfm, x = "name", y = "mpg",
2 color = "cyl", # Color by groups
3 palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
4 sorting = "ascending", # 升序排列
5 add = "segments", # 增加棒棒
6 ggtheme = theme_pubr() # ggplot2 theme
7 )
1.降序 sorting = “descending”.
2.垂直旋转 rotate = TRUE.
3.按分组排序 group = “cyl”.
4.改变点的大小,dot.soze=6.
5.mpg值作为标签 label = “mpg” or label = round(dfm$mpg).
1ggdotchart(dfm, x = "name", y = "mpg",
2 color = "cyl", # 按分组改变颜色
3 palette = c("#00AFBB", "#E7B800", "#FC4E07"), # 颜色
4 sorting = "descending", # 降序
5 add = "segments", # 增加棒棒
6 rotate = TRUE, # 旋转
7 group = "cyl", # 按分组排序
8 dot.size = 6, # 调整点大小
9 label = round(dfm$mpg), # 增加值作为标签
10 font.label = list(color = "white", size = 9,
11 vjust = 0.5), # 调整标签
12 ggtheme = theme_pubr() # 主题
13 )
Deviation graph
Use y = “mpg_z” 计算zscore
改变棒棒的颜色和大小 add.params = list(color = “lightgray”, size = 2)
1ggdotchart(dfm, x = "name", y = "mpg_z",
2 color = "cyl", # Color by groups
3 palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
4 sorting = "descending", # Sort value in descending order
5 add = "segments", # Add segments from y = 0 to dots
6 add.params = list(color = "lightgray", size = 2), # Change segment color and size
7 group = "cyl", # Order by groups
8 dot.size = 6, # Large dot size
9 label = round(dfm$mpg_z,1), # Add mpg values as dot labels
10 font.label = list(color = "white", size = 9,
11 vjust = 0.5), # Adjust label parameters
12 ggtheme = theme_pubr() # ggplot2 theme
13 )+
14 geom_hline(yintercept = 0, linetype = 2, color = "lightgray")
Cleveland 点图
文字颜色也按分组调整 y.text.col=TRUE
1ggdotchart(dfm, x = "name", y = "mpg",
2 color = "cyl", # Color by groups
3 palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
4 sorting = "descending", # Sort value in descending order
5 rotate = TRUE, # Rotate vertically
6 dot.size = 2, # Large dot size
7 y.text.col = TRUE, # y轴文字的颜色
8 ggtheme = theme_pubr() # ggplot2 theme
9 )+
10 theme_cleveland()
用简单的函数即可对图形进行高度的定制,熟悉这些参数,然后调整自己的数据格式,绘制各种高级的图,R真是包罗万象
参考资料(https://rpkgs.datanovia.com/ggpubr/)
本期内容就到这里,我是白介素2,下期再见
点击留言
你来或不来,我就在那里