如题,这个标题有点长,首先我们需要展示的图是bipartite plot,中文有叫二分图、连线图的,总之就是展示两组之间网络关系。可以应用的地方有很多,不只是我们介绍的互作关系、或者ligands-target。起源是一篇《nature communications》文章的图,它展示的是ligand于targets。原文提供了代码,可以学习!
image.png
(reference:Gliovascular transcriptional perturbations in Alzheimer’s disease reveal molecular mechanisms of blood brain barrier dysfunction)
ggraph,首先相比于igraph,在很多设置上因为与ggplot互通,所以会简单很多,没有那么复杂,可操作性更强。layout可以自己设定,也可以参照上面的!
node_info <- rbind(data.frame(node = rownames(active_ligand_target_links), group="target"),
data.frame(node = colnames(active_ligand_target_links), group="ligands"))
no <- c(rep("no", 20), sample(c('up','down'), size = nrow(node_info)-20, replace = TRUE))
node_info$reg <- sample(no, size =nrow(node_info), replace = TRUE)
#构建ggraph作图数据
df_graph <- as_tbl_graph(as.matrix(active_ligand_target_links)) %>%
mutate(group=node_info$group, #添加分组信息
reg = node_info$reg)
#plot
# sugiyama
ggraph(df_graph, layout = 'igraph', algorithm='bipartite') +
geom_edge_link(aes(colour=weight), edge_width =1)+
scale_edge_color_gradientn(colours = c("grey80","grey50","grey30","black"))+
geom_node_point(aes(fill=reg, filter= group =='ligands'),
size=2,shape=21) +
geom_node_point(aes(fill=reg, filter= group =='target'),
size=2,shape=23)+
scale_fill_manual(values = c("#E4502E","#69B7CE","black"),
breaks = c("up",'down',"no"),
labels = c("up",'down',"no"))+
geom_node_text(aes(filter= group =='ligands',
label = name),
fontface = "italic",
hjust=1.1)+
geom_node_text(aes(filter= group =='target',
label = name),
fontface = "italic",
hjust=-0.1)+
coord_flip()+
theme_minimal() +
scale_y_discrete(expand = c(0.2,0.2))+
ggraph::th_no_axes()
#调整layout
LO1 <- layout_as_bipartite(df_graph, maxiter=0);
re_pos <- c(seq(from = 0.5, by = 1, length.out = length(LO1[which(LO1[,2]==1)])),
seq(from = 0.5, by = 1.1, length.out = length(LO1[which(LO1[,2]==0)])))
LO1[,1] <- re_pos
ggraph(df_graph, layout = LO1) +
geom_edge_link(aes(colour=weight), edge_width =1)+ #连线
scale_edge_color_gradientn(colours = c(alpha("grey90",0.5),
alpha("grey60",0.5),
alpha("grey30",0.5),"black"))+#连线颜色
geom_node_point(aes(fill=reg, filter= group =='ligands'),
size=2,shape=21) +#节点设置
geom_node_point(aes(fill=reg, filter= group =='target'),
size=2,shape=23)+
scale_fill_manual(values = c("#E4502E","#69B7CE","black"),
breaks = c("up",'down',"no"),
labels = c("up",'down',"no"))+
geom_node_text(aes(filter= group =='ligands',
label = name),
fontface = "italic",
hjust=1.1)+ #文字标注
geom_node_text(aes(filter= group =='target',
label = name),
fontface = "italic",
hjust=-0.1)+
coord_flip()+
theme_minimal() +
scale_y_discrete(expand = c(0.2,0.2))+
ggraph::th_no_axes()
image.png