#堆叠柱状图美化
rm(list = ls())
library(Seurat)
load('/data1/jiarongf/HGPS2_10X_blood/celltype/RData/LS.tsne.FindClusters.celltype.RData')
Idents(sample.integrated) <- "celltype"
table(Idents(sample.integrated), sample.integrated$orig.ident)#各样本不同细胞群细胞数
Cellratio <- prop.table(table(Idents(sample.integrated), sample.integrated$group), margin = 2)#计算各组样本不同细胞群比例
Cellratio <- as.data.frame(Cellratio)
colnames(Cellratio) <- c("celltype","sample","ratio") #对列名重命名
Cellratio$sample <- factor(Cellratio$sample,levels = c('control','case')) #调整画图的x轴坐标顺序
library(ggplot2)
library(ggalluvial)
library(RColorBrewer)
qual_col_pals = brewer.pal.info[brewer.pal.info$category == 'qual',]
#处理后有73种差异还比较明显的颜色,基本够用
col_vector = unlist(mapply(brewer.pal, qual_col_pals$maxcolors, rownames(qual_col_pals)))
p<-ggplot(Cellratio,aes(x=sample,y=ratio,fill=celltype,stratum=celltype,alluvium=celltype))+
geom_col(width = 0.4,color=NA)+
geom_flow(width=0.4,alpha=0.2,knot.pos=0)+ # knot.pos参数可以使连线变直
scale_fill_manual(values=col_vector)+
theme_bw()+
theme(panel.border = element_rect(fill=NA,color="black", size=0.5, linetype="solid"))+
theme(axis.text.x=element_text(angle=15,hjust = 1,colour="black",family="Times",size=20), #设置x轴刻度标签的字体显示倾斜角度为15度,并向下调整1(hjust = 1),字体簇为Times大小为20
axis.text.y=element_text(family="Times",size=16,face="plain"), #设置y轴刻度标签的字体簇,字体大小,字体样式为plain
axis.title.y=element_text(family="Times",size = 20,face="plain"), #设置y轴标题的字体属性
panel.border = element_blank(),axis.line = element_line(colour = "black",size=1), #去除默认填充的灰色,并将x=0轴和y=0轴加粗显示(size=1)
legend.text=element_text(face="italic", family="Times", colour="black", #设置图例的子标题的字体属性
size=16),
legend.title=element_text(face="italic", family="Times", colour="black", #设置图例的总标题的字体属性
size=18),
panel.grid.major = element_blank(), #不显示网格线
panel.grid.minor = element_blank())
#theme(legend.position = 'none') #取消图例展示
p
ggsave(plot = p,"/data1/jiarongf/HGPS2_10X_blood/celltype/cell_type_percent_bar.pdf",width = 8,height = 6)
每个颜色是一个细胞类型,左边柱子是control右边是case,纵坐标是细胞占比