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