弦图是一种非常常见的图形,表示互作联系,远的不说,就近期一直说的单细胞互作关系的展示上,弦图露了很多次面!这里我们举个例子,说说简单的弦图绘制!
首先准备互作数据,设置颜色,有几个项目就设置几种颜色。这里弦图的绘制采用的是circlize包的chordDiagram函数。
setwd("D:/KS项目/公众号文章/环形弦图绘制")
# 读取数据\设置颜色
net <- read.table("df.csv", header = T, sep = ',')
library(circlize)
mycolor <- c("#DC050C", "#FB8072", "#1965B0", "#7BAFDE", "#882E72",
"#B17BA6", "#FF7F00", "#FDB462", "#E7298A", "#E78AC3",
"#33A02C")
绘图。
chordDiagram(
x = net,
grid.col = mycolor,
directional = 1,
direction.type = c("arrows", "diffHeight"),
diffHeight = -0.01,
annotationTrack = c("name", "grid", "axis"),
annotationTrackHeight = c(0.05, 0.08),
link.arr.type = "big.arrow",
link.sort = TRUE,
link.largest.ontop = TRUE,
transparency = 0.25
)
image.png
还可以将弦取消,使用箭头连接,更加明了,连接线宽度设置数值,表示互作关系的强弱。
chordDiagram(
x = net,
grid.col = mycolor,
directional = 1,
direction.type = 'arrows',
diffHeight = -0.01,
annotationTrack = c("grid","name"),
annotationTrackHeight = c(0.05, 0.08),
link.arr.type = "triangle",
link.sort = TRUE,
link.largest.ontop = TRUE,
transparency = 0.5,
col='#00000000',#弦的颜色,设置为无
link.arr.lwd=net$value, #连线的宽度
column.col = mycolor
)
image.png
最后,我们添加上标题和legend就完成了!
#添加legend
legend("right",
pch=20,
legend=unique(net$SOURCE),
bty="n",
col =mycolor,
cex=1,pt.cex=3,
border="black")
title('Intercellular communication')
image.png
这样就完成了,注释代码及示例数据已上传QQ群!觉得分享对你有用的点个赞、分享一下呗!更多精彩请至我的公众号--KS科研分享与服务!