单细胞不同分组细胞比例的展示

2022-09-23  本文已影响0人  KS科研分享与服务

这里介绍一种,单独展示每种细胞在各个分组中的比例,这样的展示会更加直观,可以看出细胞比例的变化。

需要的数据是单细胞seurat对象。计算比例。

library(Seurat)
library(ggplot2)
library(dplyr)

celltype_ratio <- scedata@meta.data %>%
  group_by(group, celltype) %>%#分组
  summarise(n=n()) %>%
  mutate(relative_freq = n/sum(n))
celltype_ratio$celltype <- factor(celltype_ratio$celltype)

ggplot可视化:

dark2 <- RColorBrewer::brewer.pal(5, "Dark2")
freq_plot <- ggplot(celltype_ratio, aes(x=celltype, y=relative_freq)) +
  geom_col(aes(fill=group), color="black", position="dodge") +
  ylab("Relative Frequency") +
  scale_fill_manual(values=dark2[1:2]) + #颜色填充
  scale_y_continuous(expand=c(0,0)) +
  facet_wrap( ~ celltype, ncol=3, scales="free") + #设置排版
  theme_classic() +
  theme(strip.background=element_blank(),
        strip.text = element_text(size=11),
        legend.title = element_blank(),
        legend.text = element_text(size=12),
        axis.text.y=element_text(size=10, color='black'),
        axis.text.x=element_text(size=10, color='black', angle=45, hjust=1),
        axis.title.x=element_blank(),
        axis.ticks.x=element_blank())

freq_plot
image.png

如果需要将所有分组展示在一起,去掉facet_wrap即可。

ggplot(celltype_ratio, aes(x=celltype, y=relative_freq)) +
  geom_col(aes(fill=group), color="black", position="dodge") +
  ylab("Relative Frequency") +
  scale_fill_manual(values=dark2[1:2]) +
  scale_y_continuous(expand=c(0,0)) +
  theme_classic() +
  theme(strip.background=element_blank(),
        strip.text = element_text(size=11),
        legend.title = element_blank(),
        legend.text = element_text(size=12),
        axis.text.y=element_text(size=10, color='black'),
        axis.text.x=element_text(size=10, color='black', angle=45, hjust=1),
        axis.title.x=element_blank(),
        axis.ticks.x=element_blank())
image.png

觉得分享有用的,点个赞、分享下再走呗!记得关注下,之后不迷路!

上一篇 下一篇

猜你喜欢

热点阅读