细胞比例
1
celltype_ratio <- tbimc@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") ##颜色参数
fillcolor <- c("#F8766D","#00BFC4")
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=fillcolor) + #颜色填充
scale_y_continuous(expand=c(0,0)) +
facet_wrap( ~ celltype, ncol=4, 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())
pdf("Cell-ratio-bycluster.pdf")
freq_plot
dev.off()
分开展示
2
pdf("Cell-ratio-Bar.pdf")
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=fillcolor) +
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())
dev.off()
合在一起展示
3
wps做的
4
wps+ai
5堆叠柱状图
Mac<-readRDS("newid_Mac.rds")
Mac_counts <- FetchData(Mac, vars = c("celltype", "group")) %>%
mutate(group = factor(group, levels = c("TBI", "MC")))
#TBI_TTTGGTTTCCAAGCCG-1 Mac3 TBI
#TBI_TTTGTTGCAGACTGCC-1 Mac1 TBI
### count根据patient_id,和tissue_type来统计总数,相当于excel分类汇总了
Mac_counts_pro <- Mac_counts %>% dplyr::count(celltype,group)
#Mac1 TBI 1708
#Mac1 MC 2
write_csv(Mac_counts_pro,"Mac_counts_pro.csv")
use_colors <- c("Mac1"="#F8766D" , "Mac2"="#F8766D" , "Mac3"= "#FFBFF1" , "Mac4"= "#00B0F6" , "Mac5"= "#E76BF3")
ggplot(data = Mac_counts, aes(x = group, fill = celltype)) +
geom_bar(position = "fill") +
scale_fill_manual(values = use_colors) +
coord_flip() +
scale_y_reverse()
ggsave2("barplot_Mac.pdf", width = 8, height = 2)
堆叠柱状图
5-2堆叠柱状图
Mac<-readRDS("newid_Mac.rds")
unique(Mac$sample)
cellnum <- table(Mac$celltype,Mac$sample)
cell.prop<-as.data.frame(prop.table(cellnum))
colnames(cell.prop)<-c("Celltype","sample","Proportion") #给这个数据框的列命名
use_colors <- c("Mac1"="#F8766D" , "Mac2"="#A3A500" , "Mac3"= "#FFBFF1" , "Mac4"= "#00B0F6" , "Mac5"= "#E76BF3")
ggplot(cell.prop,aes(sample,Proportion,fill=Celltype))+
geom_bar(stat="identity",position="fill",width = 0.8,linewidth = 0.2,colour = '#222222')+
scale_fill_manual(values=use_colors)+#自定义fill的颜色
#ggtitle("cell proportion")+coord_flip()+#横着放
theme_classic()+
labs(x='',y = '')+
theme(axis.ticks.length=unit(0.1,'cm'))+
guides(fill=guide_legend(title=NULL))
ggsave2("barplot_Mac3.pdf", width = 3, height = 4)
use_colors <- c("TBI"="#00BFC4" , "MC"="#F8766D")
ggplot(cell.prop,aes(Celltype,Proportion,fill=sample))+
geom_bar(stat="identity",position="fill",width = 0.8,linewidth = 0.2,colour = '#222222')+
scale_fill_manual(values=use_colors)+#自定义fill的颜色
#ggtitle("cell proportion")+coord_flip()+#横着放
theme_classic()+
labs(x='',y = 'Cell Proportion')+
theme(axis.ticks.length=unit(0.1,'cm'))+
guides(fill=guide_legend(title=NULL))
ggsave2("End_cellpro_sample.pdf", width = 4, height = 3)
image.png