0.简介
seurat自带的小提琴图,在基因数量多时变得不易读。看到一个漂亮的版本。
比较如下:
1.数据
rm(list = ls())
load("obj.Rdata")
load("markers.Rdata")
ls()
## [1] "pbmc" "pbmc.markers"
class(pbmc)
## [1] "Seurat"
## attr(,"package")
## [1] "SeuratObject"
head(pbmc.markers)
## p_val avg_log2FC pct.1 pct.2 p_val_adj cluster gene
## S100A9 0 4.165490 0.997 0.092 0 0 S100A9
## S100A8 0 3.476109 0.956 0.048 0 0 S100A8
## PPIF 0 3.251813 0.989 0.128 0 0 PPIF
## CD300E 0 3.002149 0.972 0.082 0 0 CD300E
## S100A6 0 2.877628 1.000 0.565 0 0 S100A6
## TYROBP 0 2.818819 1.000 0.209 0 0 TYROBP
pbmc数据是seurat标准流程findcluster得到的,pbmc.markers是findallmarkers得到的结果。
2.选择基因
library(tidyverse)
g = pbmc.markers %>%
group_by(cluster) %>%
top_n(n = 2, wt = avg_log2FC) %>%
pull(gene)
这个数据总共13个簇,选出了26个基因。
3.准备画图数据并画图
自定义的图,不能使用seurat自带的函数啦。要自己搞宽变长,然后用ggplot2画。
vln.df <- pbmc@assays$RNA@scale.data %>%
t() %>%
as.data.frame()%>%
select(g) %>%
rownames_to_column("CB") %>%
mutate(cluster = pbmc$RNA_snn_res.0.5)%>%
pivot_longer(cols = 2:(ncol(.)-1),
names_to = "gene",
values_to = "exp") %>%
mutate(gene = factor(gene,levels = g))
head(vln.df)
## # A tibble: 6 × 4
## CB cluster gene exp
## <chr> <fct> <fct> <dbl>
## 1 AAACCCAAGTAGGGTC 8 S100A9 -0.640
## 2 AAACCCAAGTAGGGTC 8 S100A8 -0.571
## 3 AAACCCAAGTAGGGTC 8 GIMAP7 -0.329
## 4 AAACCCAAGTAGGGTC 8 CD3G -0.704
## 5 AAACCCAAGTAGGGTC 8 LINC02446 -0.348
## 6 AAACCCAAGTAGGGTC 8 IL7R -0.390
# 自定义颜色
library(paletteer)
my_color = paletteer_d(`"ggsci::default_nejm"`)
my_color = colorRampPalette(my_color)(length(unique(vln.df$cluster)))
# 画图
p1 <- ggplot(vln.df,aes(cluster,exp),color=factor(cluster))+
geom_violin(aes(fill=cluster),scale = "width")+
scale_fill_manual(values = my_color)+
facet_grid(gene~.,scales = "free_y")+
scale_y_continuous(expand = c(0,0))+
theme_bw()+
theme(
panel.grid = element_blank(),
axis.title.x.bottom = element_blank(),
axis.ticks.x.bottom = element_blank(),
axis.text.x.bottom = element_text(hjust = 1,vjust = NULL,color = "black",size = 14),
axis.title.y.left = element_blank(),
axis.ticks.y.left = element_blank(),
axis.text.y.left = element_blank(),
legend.position = "none",
panel.spacing.y = unit(0, "cm"),
strip.text.y = element_text(angle=0,size = 14,hjust = 0),
strip.background.y = element_blank()
)
p1
参考:https://blog.csdn.net/qq_38774801/article/details/130791574