1.效果图
由于大量0值,导致小提琴图不好看,** 下面2个图做了过滤,只保留>1e-4的点 **。这样是否合适,还有待探讨 //todo
2.源码
三个图
- Seurat 函数 VlnPlot
- 从ggplot2获取数据,重新绘制 violin plot
- 使用 gghalves 包 左边散点,右边小提琴图
scObj$tissue.type=factor(scObj$tissue.type,
levels = c("NL", "LP", 'CA', 'LN'))
### (3)Re plot half VlnPlot ----
gene.symbol="PCF11"
g2=VlnPlot(scObj,
features = c(gene.symbol),
#features = c("CD68"),
#group.by="tissue.type",
group.by="cell.type",
#cols = color.head,
pt.size=0)+RotatedAxis(); g2 #fig1
# scale_color_manual(color.head)
head(g2$data)
# PCF11 ident
#AAACGGGCATGACGGA-1 1.370958e-05 T.cells
#AAAGATGAGCAGACTG-1 -5.646982e-06 T.cells
# BiocManager::install("gghalves")
# options(BioC_mirror="https://mirrors.tuna.tsinghua.edu.cn/bioconductor")
# options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
library(gghalves)
dat=data.frame(
ident=g2$data$ident,
value=g2$data[,1]
)
# rm 0 values?
dim(dat)
dat=dat[which(dat$value>1e-4),]
dim(dat)
ggplot(dat, aes(x = ident, y = value, fill = ident) )+
geom_jitter(size=0.1, color="grey80") +
geom_violin(trim=F, #末尾是否截断
aes(fill=ident), #填充颜色
scale="width", #默认是按面积 area,推荐改为width
bw=0.1)+ #取样宽度,越小越精细模拟原始值
geom_boxplot(width=0.1, fill="white", show.legend = F,
outlier.colour = "#00000000") + #离群值 透明
theme_classic(base_size = 14)+
RotatedAxis()+
ggtitle(gene.symbol) #fig2
#
ggplot(dat, aes(x = ident, y = value, fill = ident) ) +
geom_half_point(size=0.1, side="l", aes(color=ident) )+
geom_half_violin(
aes(fill = ident),
alpha=0.7,
#position = position_nudge(x = .15, y = 0),
#adjust=0.5,
trim=F,
colour=NA, #描边颜色
bw=0.1,
side = 'r',
show.legend = F
)+
geom_boxplot(width=0.1, fill="white", show.legend = F,
outlier.colour = "#00000000") + #离群值 透明
#scale_fill_manual(values = color.head) +
#scale_color_manual(values = color.head) +
theme_classic(base_size = 14)+
RotatedAxis()+
labs(x="", y="log(Normalized counts +1)", title = bquote( italic(.(gene.symbol)) ) )+
guides(colour = guide_legend(override.aes = list(alpha = 1, size=3))) #fig3
#
水平图
参考:
# 旋转坐标
ggplot2::ggplot(iris , aes(x = Species, y = Sepal.Length, fill = Species))+
gghalves::geom_half_violin(aes(fill = Species),
position = position_nudge(x = .15, y = 0),
adjust=1.5, trim=FALSE,
colour=NA, side = 'r') +
gghalves::geom_half_dotplot(aes(fill = Species), method="histodot", stackdir="down",
dotsize = 0.55, position=position_nudge(x = .15, y = 0),
binwidth = 0.15, colour = NA
) +
ggplot2::coord_flip() +
ggthemes::theme_base()
ref
- https://www.jianshu.com/p/f9adedcfc71c
- https://zhuanlan.zhihu.com/p/617212689
- https://www.bilibili.com/read/cv12815285/