visNetworkR包:交互式网络可视化

通常是使用Cytoscape、igraph包等来可视化网络,虽然能够创建美观的网络图,但它们只是静态的。对于创建交互式网络可视化,可以使用R中的特定包—visNetwork,有许多参数来创建个性化网络。

数据准备
install.packages("visNetwork")
devtools::install_github("datastorm-open/visNetwork")
rm(list=ls())library(visNetwork)library(igraph)
a nodes data.frame, with id column
a edges data.frame, with from and to columns
**列名必须是指定的,node至少一列“id”,edge至少两列“from”“to”。

举例介绍功能
01
基础网络图visNetwork
nodes <- data.frame(id = 1:6)edges <- data.frame(from = c(1,2,2,3,3,4,4), to = c(2,3,4,4,6,5,6))
visNetwork(nodes, edges,  width = 500, height = 500, #图形尺寸  main = "visNetwork minimal example", submain = "add a subtitle", #主副标题footer = "EXP.1",#页脚  background = "lightgray"#背景)

02
设置点边属性
(1)建立点边属性数据框
nodes <- data.frame(id = 1:6,   label = paste("N", 1:6),# 点标签  group = c("GrA", "GrB", "GrC"), # 分组  value = 1:6, # 点大小   shape = c("square", "triangle", "box", "circle", "dot", "star"),# 形状  color = c("orange", "grey", "purple"),# 颜色  shadow = c(TRUE, FALSE, TRUE) # 阴影)edges <- data.frame(from = c(1,2,2,3,3,4,4), to = c(2,3,4,4,6,5,6),  label = paste("E", 1:7), # 边标签  length = c(100,200,300,400,450,500,500), # lengthcolor = list(color = "gray",highlight = "red"), #设置颜色,字符串或列表,点击选中的点的边显示红色  arrows = c("to", "from","to", "middle","to","to", "middle;to"), # 有向网络的方向  dashes = c(TRUE, FALSE,TRUE, FALSE,FALSE,TRUE, FALSE), # 绘制虚线边  title = paste("Edge", 1:7), # 提示信息  smooth = c(FALSE, TRUE ,TRUE, FALSE,FALSE,TRUE, FALSE), # 绘制为动态二次贝塞尔曲线,时间更长但更好看。  shadow = c(FALSE, TRUE, FALSE, TRUE,TRUE, FALSE,FALSE) # 边投射阴影)visNetwork(nodes, edges, width = 500, height = 500, main = "visNetwork example", submain = "add a subtitle", footer = "EXP.2")

(2)通过visEdges和visNodes设置属性。
nodes <- data.frame(id = 1:6)edges <- data.frame(from = c(1,2,2,3,3,4,4), to = c(2,3,4,4,6,5,6))visNetwork(nodes, edges, width = 500, height = 500, main = "visNetwork example", submain = "add a subtitle", footer = "EXP.3") %>% visNodes(group = c("GrA", "GrB", "GrC"), shape = "square", title = "I'm a node", borderWidth = 3 ,color = list(background = "red", border = "lightgray", highlight = "yellow"), shadow = list(enabled = TRUE, size = 50)) %>% visEdges(width = 1,color = list(color = "gray", highlight = "yellow"),arrows = list(to = list(enabled = TRUE, scaleFactor = 2, type = 'circle'),smooth = list(enabled = TRUE, type = "diagonalCross"), dashes = TRUE) )

03
加图例visLegend
nodes <- data.frame(id = 1:6,   label = paste("N", 1:6),# 点标签  group = c("GrA", "GrB", "GrC"), # 分组  color = list(border = "lightgray", highlight = "yellow"),  shadow = list(enabled = TRUE, size = 50))edges <- data.frame(from = c(1,2,2,3,3,4,4), to = c(2,3,4,4,6,5,6),  label = paste("E", 1:7), # 边标签width = 1,color = list(color = "gray", highlight = "yellow"))
visNetwork(nodes, edges, width = 500, height = 300, main = "visNetwork example", submain = "add a subtitle", footer = "EXP.4") %>% visLegend(width = 0.05, position = "right", main = "Group")

04
通用选项visOption
(1)突出显示最近的节点
nodes <- data.frame(id = 1:6,   label = paste("N", 1:6),# 点标签  group = c("GrA", "GrB", "GrC"), # 分组  color = list(border = "lightgray", highlight = "orange"),  shadow = list(enabled = TRUE, size = 50))edges <- data.frame(from = c(1,2,2,3,3,4,4), to = c(2,3,4,4,6,5,6),  label = paste("E", 1:7), # 边标签width = 1,color = list(color = "gray", highlight = "orange"))
visNetwork(nodes, edges, width = 500, height = 300, main = "visNetwork example", submain = "add a subtitle", footer = "EXP.5") %>% visLegend(width = 0.05, position = "right", main = "Group") %>% visOptions(highlightNearest = list(enabled = T, degree = 2, hover = T)) #单击节点时突出显示最近的节点

(2)根据点data.frame的列添加多个选择,自定义筛选项
visNetwork(nodes, edges, width = 500, height = 300, main = "visNetwork example", submain = "add a subtitle", footer = "EXP.7") %>% visLegend(width = 0.05, position = "right", main = "Group") %>% visOptions(highlightNearest = TRUE, selectedBy = list(variable = "group", selected = "GrA")) #根据点data.frame的列添加多个选择,自定义筛选项

05
布局

visHierarchicalLayout等同于visLayout(hierarchical = TRUE)

visNetwork(nodes, edges, width = 500, height = 300, main = "visNetwork example", submain = "add a subtitle", footer = "EXP.8") %>% visLegend(width = 0.05, position = "right", main = "Group") %>% visHierarchicalLayout()

visIgraphLayout,可以使用igraph中的所有可用布局并计算坐标。

visNetwork(nodes, edges, width = 500, height = 300, main = "visNetwork example", submain = "add a subtitle", footer = "EXP.9") %>% visIgraphLayout( layout = "layout_in_circle")

06
控制网络动态
visNetwork(nodes, edges, width = 500, height = 300, main = "visNetwork example", submain = "add a subtitle", footer = "EXP.10") %>% visInteraction(dragNodes = FALSE, dragView = FALSE,#点边不可移动,可点击选中zoomView = FALSE#不可缩放)

实例演示
01
基因网络

(1)在STRING中下载差异基因(node.txt)的互作关系,为了后边方便使用将前两列改成了“from”和“to”。

library(visNetwork)setwd("E:/visNetwork")
#为node&egde设置属性node.df=data.frame(id=node[,],  value = node_tag$degree,  label = node[,],  group = node_tag$mut,#根据突变状态0-1进行点分组  color = list(color = ncolor, border = "lightgray", highlight = "yellow"),  shadow = list(enabled = TRUE, size = 50),shape = shape)  

edge.df <- data.frame(from = interactions[,1], to = interactions[,2],  label = paste("Edge",1:579),#标签  length = interactions$coexpression*10,#边长color = list(color = "gray" , highlight = "yellow"),  dashes = sample(c(TRUE, FALSE),579,replace=TRUE),#破折号  smooth = sample(c(TRUE, FALSE),579,replace=TRUE),# smooth  shadow = sample(c(TRUE, FALSE),579,replace=TRUE)# shadow) visNetwork(node.df, edge.df, width = 500, height = 300, main = "visNetwork example", submain = "STRING network", footer = "175 nodes - 579 edges")%>% visGroups(groupname = "1", color = "red") %>%  visGroups(groupname = "0", color = "lightgray") %>%  visLegend(width = 0.05, position = "right", main = "Mutated gene")%>% visIgraphLayout( layout = "layout_in_circle") %>%visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)

(2)层次聚类,网络密度大时不适用。

visNetwork(node.df, edge.df, width = 500, height = 300, main = "visNetwork example", submain = "STRING network", footer = "HierarchicalLayout")%>% visGroups(groupname = "1", color = "red",shape="triangle") %>%visGroups(groupname = "0", color = "lightgray") %>%visLegend(width = 0.05, position = "right", main = "Mutated gene")%>% visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE) %>%visHierarchicalLayout()

02
药靶网络

从GDSC中下载LAML的药物靶点数据,选用其中1,3,4,5列。

node.df=data.frame(id=c(drug,target,pathway,feature),  group=rep(c("drug","target","pathway","feature"),c(length(drug),length(target),length(pathway),length(feature))),  value = dg,#用度来表示点大小  color = list(highlight = "red"),  label = nodes$id,  shadow = list(enabled = TRUE, size = 50))
edge.df = data.frame(from=edges$from,to=edges$to,  color = list(color = "gray" , highlight = "red"),  dashes = TRUE,  smooth = TRUE)  visNetwork(node.df, edge.df, width = 500, height = 300, main = "visNetwork",submain="GDSC - LAML", footer = "56 drug - 45 target - 17 pathway - 5feature")%>% visGroups(groupname = "feature", color = "red") %>%visGroups(groupname = "pathway", color = "orange") %>%visGroups(groupname = "target", color = "purple") %>%visGroups(groupname = "drug", color = "green") %>%#请原谅我的死亡配色visLegend(width = 0.1, position = "right", main = "node-genetype")%>% visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)

小编总结

visNetwork包生成一个html文件,可以保存发送。优于常规的网络可视化方法是交互式的动态呈现,生动有趣。简单的几行代码就可以满足你对网络图更“苛刻”的要求,一起来探索他的更多功能吧!

生物信息学

coMETR包可视化EWAS结果

2020-8-28 4:08:19

生物信息学

基于甲基化评估肿瘤纯度R包-InfiniumPurify

2020-8-28 4:13:19

声明 本网站部分文章源于互联网,出于传递更多信息和学习之目的转载,并不保证内容正确或赞同其观点。
如转载稿涉及失效、版权等问题,请立即联系管理员;我们会予以修改、删除相关文章,请留言反馈
Notice: When your legal rights are being violated, please send an email to: [email protected]
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索