visNetwork是一个用于网络可视化的R包,使用的是vis.js javascript库(
http://visjs.org
)。
visNetwork github :
https://github.com/datastorm-open/visNetwork
.
#安装visNetwork
install.packages("visNetwork")
# devtools::install_github("datastorm-open/visNetwork") for development version
##查看帮助
# vignette
vignette("Introduction-to-visNetwork")
# full javascript documentation
visDocumentation()
# shiny example
shiny::runApp(system.file("shiny", package = "visNetwork"))
##visNetwork至少需要两个信息:
节点,data.frame数据,有id列;
边缘,data.frame数据,至少两列,一列起点信息(from),一列终点信息(to)。
> require(visNetwork, quietly = TRUE)
# minimal example
> nodes <- data.frame(id = 1:3)
> edges <- data.frame(from = c(1,2), to = c(1,3))
from to
> visNetwork(nodes, edges, width = "100%")
value:指定节点大小
group:指定节点所属组,igraph包中有许多包装好的方法可以用于网络类群分析
shape:节点形状,例如circle, square ,triangle
color:节点颜色
title:设置提示信息,当您将鼠标悬停在一个节点上时,提示就会出现
shadow:定义一个节点是否有阴影(TRUE/FALSE)
在node data.frame上添加更多变量。有关可用选项,请参阅visNodes()参数。
visNodes(
graph,
id = NULL,
shape = NULL,
size = NULL,
title = NULL,
value = NULL,
x = NULL,
y = NULL,
label = NULL,
level = NULL,
group = NULL,
hidden = NULL,
image = NULL,
mass = NULL,
physics = NULL,
borderWidth = NULL,
borderWidthSelected = NULL,
brokenImage = NULL,
labelHighlightBold = NULL,
color = NULL,
fixed = NULL,
font = NULL,
icon = NULL,
shadow = NULL,
scaling = NULL,
shapeProperties = NULL,
heightConstraint = NULL,
widthConstraint = NULL,
margin = NULL,
chosen = NULL
> nodes <- data.frame(id = 1:10,
label = paste("Node", 1:10), # add labels on nodes
group = c("GrA", "GrB"), # add groups on nodes
value = 1:10, # size adding value
shape = c("square", "triangle", "box", "circle", "dot", "star",
"ellipse", "database", "text", "diamond"), # control shape of nodes
title = paste0("<p><b>", 1:10,"</b><br>Node !</p>"), # tooltip (html or character)
color = c("darkred", "grey", "orange", "darkblue", "purple"),# color
shadow = c(FALSE, TRUE, FALSE, TRUE, TRUE)) # shadow
> head(nodes)
id label group value shape title color shadow
1 Node 1 GrA 1 square <p><b>1</b><br>Node !</p> darkred FALSE
2 Node 2 GrB 2 triangle <p><b>2</b><br>Node !</p> grey TRUE
3 Node 3 GrA 3 box <p><b>3</b><br>Node !</p> orange FALSE
4 Node 4 GrB 4 circle <p><b>4</b><br>Node !</p> darkblue TRUE
5 Node 5 GrA 5 dot <p><b>5</b><br>Node !</p> purple TRUE
6 Node 6 GrB 6 star <p><b>6</b><br>Node !</p> darkred FALSE
##边属性设置
label:指定边标签的列
title:设置提示信息,当您将鼠标悬停在一个边上时,提示就会出现
shadow:定义一个节点是否有阴影(TRUE/FALSE)
length,width:定义网络内边的长度/宽度
arrows:箭头在边上的位置
dashes:定义边缘是否应该虚线(TRUE/FALSE)
smooth:光滑的线(TRUE/FALSE)
在edges data.frame上添加更多变量。有关可用选项,请参阅visEdges()参数。
在edge data.frame上添加更多变量。有关可用选项,请参阅visEdges。
visEdges(
graph,
title = NULL,
value = NULL,
label = NULL,
length = NULL,
width = NULL,
dashes = NULL,
hidden = NULL,
hoverWidth = NULL,
id = NULL,
physics = NULL,
selectionWidth = NULL,
selfReferenceSize = NULL,
labelHighlightBold = NULL,
color = NULL,
font = NULL,
arrows = NULL,
arrowStrikethrough = NULL,
smooth = NULL,
shadow = NULL,
scaling = NULL,
widthConstraint = NULL,
chosen = NULL
> edges <- data.frame(from = sample(1:10, 8), to = sample(1:10, 8),
label = paste("Edge", 1:8), # add labels on edges
length = c(100,500), # length
arrows = c("to", "from", "middle", "middle;to"), # arrows
dashes = c(TRUE, FALSE), # dashes
title = paste("Edge", 1:8), # tooltip (html or character)
smooth = c(FALSE, TRUE), # smooth
shadow = c(FALSE, TRUE, FALSE, TRUE)) # shadow
> head(edges)
## from to label length arrows dashes title smooth shadow
## 1 4 5 Edge 1 100 to TRUE Edge 1 FALSE FALSE
## 2 10 6 Edge 2 500 from FALSE Edge 2 TRUE TRUE
## 3 7 8 Edge 3 100 middle TRUE Edge 3 FALSE FALSE
## 4 6 4 Edge 4 500 middle;to FALSE Edge 4 TRUE TRUE
## 5 3 3 Edge 5 100 to TRUE Edge 5 FALSE FALSE
## 6 2 1 Edge 6 500 from FALSE Edge 6 TRUE TRUE
visNetwork(nodes, edges, width = "100%")
使用visNodes和visEdges设置节点和边全局属性,并使用visGroups为每个组添加属性。
nodes <- data.frame(id = 1:5, group = c(rep("A", 2), rep("B", 3)))
edges <- data.frame(from = c(2,5,3,3), to = c(1,2,4,2))
visNetwork(nodes, edges, width = "100%") %>%
visNodes(shape = "square") %>% # square for all nodes
visEdges(arrows ="to") %>% # arrow "to" for all edges
visGroups(groupname = "A", color = "darkblue") %>% # darkblue for group "A"
visGroups(groupname = "B", color = "red") # red for group "B"
> nb <- 10
> nodes <- data.frame(id = 1:nb, label = paste("Label", 1:nb),
group = sample(LETTERS[1:3], nb, replace = TRUE), value = 1:nb,
title = paste0("<p>", 1:nb,"<br>Tooltip !</p>"), stringsAsFactors = FALSE)
id label group value title
1 Label 1 A 1 <p>1<br>Tooltip !</p>
2 Label 2 C 2 <p>2<br>Tooltip !</p>
3 Label 3 C 3 <p>3<br>Tooltip !</p>
4 Label 4 A 4 <p>4<br>Tooltip !</p>
5 Label 5 A 5 <p>5<br>Tooltip !</p>
6 Label 6 B 6 <p>6<br>Tooltip !</p>
> edges <- data.frame(from = trunc(runif(nb)*(nb-1))+1,
to = trunc(runif(nb)*(nb-1))+1,
value = rnorm(nb, 10), label = paste("Edge", 1:nb),
title = paste0("<p>", 1:nb,"<br>Edge Tooltip !</p>"))
from to value label title
1 6 10.569034 Edge 1 <p>1<br>Edge Tooltip !</p>
7 9 9.755302 Edge 2 <p>2<br>Edge Tooltip !</p>
4 4 10.618589 Edge 3 <p>3<br>Edge Tooltip !</p>
5 4 9.240707 Edge 4 <p>4<br>Edge Tooltip !</p>
3 1 8.859133 Edge 5 <p>5<br>Edge Tooltip !</p>
4 6 11.003060 Edge 6 <p>6<br>Edge Tooltip !</p>
##添加图例
你现在可以在节点/边缘上添加更多的自定义图例:
visNetwork(nodes, edges, width = "100%") %>% visLegend()
##或传递数据
visNetwork(nodes, edges, width = "100%") %>%
visLegend(useGroups = FALSE, addNodes = data.frame(label = "Nodes", shape = "circle"),
addEdges = data.frame(label = "link", color = "black"))
##按节点id选择(Select by node id)
你也可以选择节点的id/标签与一个列表与nodesIdSelection:
visNetwork(nodes, edges, width = "100%") %>%
visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)
使用selectedBy选项通过列的值选择一些节点:
# can be the column you want
> nodes$sel <- sample(c("sel1", "sel2"), nrow(nodes), replace = TRUE)
id label group value title sel
1 Label 1 A 1 <p>1<br>Tooltip !</p> sel1
2 Label 2 C 2 <p>2<br>Tooltip !</p> sel1
3 Label 3 C 3 <p>3<br>Tooltip !</p> sel2
4 Label 4 A 4 <p>4<br>Tooltip !</p> sel2
5 Label 5 A 5 <p>5<br>Tooltip !</p> sel2
6 Label 6 B 6 <p>6<br>Tooltip !</p> sel2
7 Label 7 B 7 <p>7<br>Tooltip !</p> sel1
8 Label 8 C 8 <p>8<br>Tooltip !</p> sel1
9 Label 9 C 9 <p>9<br>Tooltip !</p> sel1
10 Label 10 A 10 <p>10<br>Tooltip !</p> sel2
> visNetwork(nodes, edges, width = "100%") %>%
visOptions(selectedBy = "sel")
##在网络中使用字体图标
您可以在组或节点选项使用Font Awesome icons。Font Awesome library (http://fortawesome.github.io/Font-Awesome/)不是依赖的一部分。如果需要,使用addFontAwesome()。
nodes <- data.frame(id = 1:3, group = c("B", "A", "B"))
edges <- data.frame(from = c(1,2), to = c(2,3))
visNetwork(nodes, edges, width = "100%") %>%
visGroups(groupname = "A", shape = "icon", icon = list(code = "f0c0", size = 75)) %>%
visGroups(groupname = "B", shape = "icon", icon = list(code = "f007", color = "red")) %>%
visLegend() %>%
addFontAwesome()
visNetworkOutput 和renderVisNetwork. 在shiny中,可以通过nodesIdSelection选择显示数据。进一步需要实现更多的功能,可以了解visNetworkProxy。
output$mynetwork <- renderVisNetwork({... visOptions(nodesIdSelection = TRUE)}) # created input$mynetwork_selected
And with selectedBy, new input input$mynetwork_selectedBy.
##物理、工具提示、事件
可以控制:
physics of network : visPhysics
events : visEvents
network <- visNetwork(nodes, edges, width = "100%")
visSave(network, file = "network.html")
##直接使用网络数据(Use DOT language data)
visNetwork(dot = 'dinetwork {1 -> 1 -> 2; 2 -> 3; 2 -- 4; 2 -> 1 }', width = "100%")