分享

ggplot2绘图学习 小提琴图

 公号生信小课堂 2021-10-28

之前我们学习了ggplot绘制单变量,两个连续变量的图形,两个离散型变量。对于一个离散型变量,一个连续型变量,有很多作图方式,包括箱图,点图等等

· geom_boxplot() for box plot
· geom_violin() for violin plot
· geom_dotplot() for dot plot
· geom_jitter() for stripchart
· geom_line() for line plot
· geom_bar() for bar plot

今天我们介绍一下小提琴图

library(ggplot2)data("ToothGrowth")ToothGrowth$dose <- as.factor(ToothGrowth$dose)head(ToothGrowth)

我们先建立一个图层,以dose为X轴,len为Y轴

e <- ggplot(ToothGrowth, aes(x = dose, y = len))

主要函数及参数

· Key function: geom_violin()
· Alternative function: stat_ydensity()
· Key arguments to customize the plot: alpha, color, linetype, size and fill.

#基础e + geom_violin()#旋转e + geom_violin() + coord_flip()#不修剪小提琴的尾部e + geom_violin(trim = FALSE, fill = "steelblue")

加统计值

stat_summary() 可以加均值/中位值等

用fun= mean/median加均值/中位值

e + geom_violin(trim = FALSE) + stat_summary(fun= mean, geom = "point", shape = 23, size = 2, color = "blue")

加均值及标准差

e + geom_violin(trim = FALSE) + stat_summary(fun.data="mean_sdl", fun.args = list(mult=1), geom="pointrange", color = "red")

加框,包含中位数及四分位数

e + geom_violin(trim = FALSE) + geom_boxplot(width = 0.2)

改变颜色

e + geom_violin(aes(color = dose), trim = FALSE)e + geom_violin(aes(fill = dose), trim = FALSE)

自定义颜色

e2 <- e + geom_violin(aes(color = dose), trim = FALSE) + theme_minimal()e2 + scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
e3 <- e + geom_violin(aes(fill = dose), trim = FALSE) + theme_minimal()e3 + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))

添加分组变量

e + geom_violin(aes(fill = supp), trim = FALSE)e + geom_violin(aes(fill = supp), trim = FALSE) + scale_fill_manual(values=c("#999999", "#E69F00"))

资源贴

生信小课堂资源汇总

单基因泛癌分析

TCGA单基因免疫相关泛癌分析,懒人福音, 重磅来袭

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多