❝1、《KS科研分享与服务》公众号有QQ交流群,但是进入门槛是20元,请考虑清楚。群里有推文的注释代码和示例数据,付费内容半价,还可以与大家交流。
2、单细胞转录组全流程代码需收费,收费代码包含公众号付费内容,也有很多新增加的内容。需进群或者需单细胞代码的小伙伴请添加作者微信了解,请备注目的,除此之外请勿添加,谢谢!
3、付费文章集合有打包价哦!
详情请联系作者:
❞
今天我们来做一下桑基图,之前单细胞系列我们做过细胞比例堆叠柱状图的连线(跟着Cell学单细胞转录组分析(十四):细胞比例柱状图---连线堆叠柱状图),我们还是以此为例,之前是用线连接,这里使用面积连接,也就是桑基图。
构建作图数据。
setwd("D:/KS项目/公众号文章/桑基图")library(Seurat)library(ggplot2)library(dplyr)library(ggalluvial)#构建数据Ratio <- scedata@meta.data %>% group_by(group, celltype) %>%#分组 summarise(n=n()) %>% mutate(relative_freq = n/sum(n))celltype_ratio$celltype <- factor(celltype_ratio$celltype)
简单的做一个堆叠柱状图。
#堆叠柱状图mycolor = c('#efb306', '#7db954', '#852f88', '#4e54ac', '#0f8096')ggplot(Ratio) + geom_bar(aes(x =group, y= relative_freq, fill = celltype),stat = "identity",width = 0.7,size = 0.5,colour = '#222222')+ theme_classic() + labs(x='Sample',y = 'Ratio')+ coord_flip()+ scale_fill_manual(values = mycolor)
使用ggalluvial包的geom_flow函数。
#冲击桑基图ggplot(Ratio, aes(x =group, y= relative_freq, fill = celltype, stratum=celltype, alluvium=celltype)) + geom_col(width = 0.5, color='black')+ geom_flow(width=0.5,alpha=0.4, knot.pos=0)+ theme_classic() + labs(x='Sample',y = 'Ratio')+ coord_flip()+ scale_fill_manual(values = mycolor)
参数knot.pos设置为0.5,连接为曲线面积,就像常见的桑基图了,其他的桑基图也可以类似于这样的方法完成!
ggplot(Ratio, aes(x =group, y= relative_freq, fill = celltype, stratum=celltype, alluvium=celltype)) + geom_col(width = 0.5, color='black')+ geom_flow(width=0.5,alpha=0.4, knot.pos=0.5)+ theme_classic() + labs(x='Sample',y = 'Ratio')+ coord_flip()+ scale_fill_manual(values = mycolor)
library(devtools)devtools::install_github("thackl/thacklr")devtools::install_github("thackl/gggenomes")
install.packages("C:/Users/tq199/Desktop/thackl-thacklr-2ff883e.tar.gz", repos = NULL, type = "source")install.packages("gggenes")install.packages("snakecase")install.packages("C:/Users/tq199/Desktop/thackl-gggenomes-fe5aeb8.tar.gz", repos = NULL, type = "source")library(gggenomes)
df <- read.csv("Ratio1.csv", header = T)
df$start <- 100*df$start
df$end <- 100*df$end
#柱状图
mycolor = c('#efb306',
'#7db954',
'#852f88',
'#4e54ac',
'#0f8096')
gggenomes(genes = df)+
geom_gene(shape=0,aes(fill=celltype), size = 10)+
geom_bin_label(size = 4, expand_left=0,nudge_left =0.01)+
scale_fill_manual(values = mycolor)
df_BM <- subset(df, group=='BM')
df_BM %>% mutate(length=end-start+1) -> df_BM
df_GM <- subset(df, group=="GM")
df_GM %>% mutate(length=end-start+1) -> df_GM
colnames(df_GM) <- c("seq_id2","group2","celltype2","n2","relative_freq2","start2","end2","length2")
data_link <- cbind(df_BM, df_GM)
gggenomes(genes = df,links = data_link)+
geom_gene(shape=0,aes(fill=celltype), size = 10)+
geom_bin_label(size = 4, expand_left=0,nudge_left =0.01)+
scale_fill_manual(values = mycolor)+
geom_link(aes(fill=celltype),offset = 0.25)
联系客服