ggplot2绘图学习 小提琴图
之前我们学习了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"))
资源贴
单基因泛癌分析
赞 (0)