桑基图(SanKey)
SanKey plot## 设置路劲
setwd("D:\\小杜的生信筆記\\桑基图")
## 导入R包
#BiocManager::install("ggalluvial")
library("ggplot2")
library("ggalluvial")
</pre>
</pre>
导入数据:
df <- read.table("SangKey_input.txt", header = T, row.names = 1, sep = '\t')
head(df)
数据格式:
> head(df)
gene lncRNA miRNA
1 gene4 lncRNA1 miRNA2
2 gene4 lncRNA3 miRNA3
3 gene3 lncRNA5 miRNA3
4 gene2 lncRNA5 miRNA2
5 gene8 lncRNA4 miRNA1
6 gene8 lncRNA2 miRNA3
开始绘图:
定义颜色
mycol <- rep(c("#223D6C","#D20A13","#FFD121","#088247","#11AA4D","#58CDD9",
"#7A142C","#5D90BA","#029149","#431A3D","#91612D","#6E568C",
"#E0367A","#D8D155","#64495D","#7CC767"),2)
格式转换:
#格式转换
UCB_lodes <- to_lodes_form(df[,1:ncol(df)],
axes = 1:ncol(df),
id = "Cohort")
head(UCB_lodes)
Cohort x stratum
1 1 gene gene4
2 2 gene gene4
3 3 gene gene3
4 4 gene gene2
5 5 gene gene8
6 6 gene gene8
> tail(UCB_lodes)
Cohort x stratum
295 95 miRNA miRNA4
296 96 miRNA miRNA3
297 97 miRNA miRNA3
298 98 miRNA miRNA2
299 99 miRNA miRNA3
300 100 miRNA miRNA2
画图:
pdf("Sankey.pdf",width = 6, height = 6)
ggplot(UCB_lodes,
aes(x = x, stratum = stratum, alluvium = Cohort,
fill = stratum, label = stratum)) +
scale_x_discrete(expand = c(0, 0)) +
geom_flow(width = 1/8) + #线跟方块间空隙的宽窄
geom_stratum(alpha = .9,width = 1/10) + #方块的透明度、宽度
geom_text(stat = "stratum", size = 3,color="black") + #文字大小、颜色
#不喜欢默认的配色方案,用前面自己写的配色方案
scale_fill_manual(values = mycol) +
xlab("") + ylab("") +
theme_bw() + #去除背景色
theme(panel.grid =element_blank()) + #去除网格线
theme(panel.border = element_blank()) + #去除外层边框
theme(axis.line = element_blank(),axis.ticks = element_blank(),axis.text = element_blank()) + #去掉坐标轴
ggtitle("")+
guides(fill = FALSE)
dev.off()
ENDING !!!
“小杜的生信筆記” 公众号
、知乎
、简书平台
,主要发表或收录生物信息学的教程,以及基于R的分析和可视化(包括数据分析,图形绘制等);分享感兴趣的文献和学习资料!
网友评论