用ggplot2绘制小提琴图
1. 绘制基础小提琴图
ggplot(data=, aes(x=, y=))+geom_violin(aes(fill=))
ggplot()+geom_violin(data=, aes(x=, y=, fill=))
> ff <- c(1:12, rnorm(8,7,1), c(8,3,4,5,7,1,2,3), c(9,0,3,7,2,67,44))
> ff <- c(ff, sample(ff, 27))
> ff <- c(ff, sample(ff, 32))
>
> kk <- as.data.frame(list(sample(rep(1:5,100),length(ff)), ff))
> kk[,1] <- as.factor(kk[,1])
> kk[,2] <- abs(kk[,2])
>
> colnames(kk) <- c("aa","bb")
>
> head(kk, 10)
aa bb
1 2 1
2 4 2
3 2 3
4 1 4
5 1 5
6 5 6
7 3 7
8 4 8
9 3 9
10 3 10
> kk[,1]
[1] 2 4 2 1 1 5 3 4 3 3 3 1 4 1 1 1 2 5 4 2 4 4 5 2 5 4 3 2 2 4 4 5 3 3 4 3 4 4 2 4 3 2 5 1 1 5 3 1 5 2 3 3 4 4 4 4 1
[58] 5 4 2 5 4 5 5 5 4 3 4 4 2 5 5 1 1 1 5 3 4 1 5 5 3 5 4 1 1 3 1 2 3 5 2 2 5
Levels: 1 2 3 4 5
>
> ggplot()+geom_violin(data=kk, aes(x=aa, y=bb, fill=aa))
图片.png
2. 旋转小提琴图
ggplot()+geom_violin(data=, aes(x=, y=, fill=))+coord_flip()
3. 在小提琴图中添加四分位图
ggplot()+geom_violin(data=, aes(x=, y=))+geom_boxplot(data=, aes(x=, y=), width=)
ggplot()+geom_violin(data=, aes(x=, y=))+geom_boxplot(data=, aes(x=, y=), width=, position = position_dodge())
其实就是小提琴图和箱线图的联用,并要主要控制箱线图的宽度
4. 添加中位数点和SD bar
require(hmisc)
ggplot(kk, aes(x = cluster, y = nCount_RNA)) + geom_violin(aes(fill=cluster)) + scale_y_log10()+stat_summary(fun.data = "mean_sdl", fun.args = list(mult = 1), geom = "pointrange", color = "white", size=1) + theme_cowplot()
image.png
list(mult = 1)
: SD bar 为1
SD
绘制出的图像,点
为平均数,bar
为 SD
网友评论