visNetwork是一個用于網(wǎng)絡(luò)可視化的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至少需要兩個信息:
- 節(jié)點仙逻,data.frame數(shù)據(jù),有id列;
- 邊緣,data.frame數(shù)據(jù)叨吮,至少兩列,一列起點信息(from)瞬矩,一列終點信息(to)茶鉴。
> require(visNetwork, quietly = TRUE)
# minimal example
> nodes <- data.frame(id = 1:3)
id
1
2
3
> edges <- data.frame(from = c(1,2), to = c(1,3))
from to
1 1
2 3
> visNetwork(nodes, edges, width = "100%")
#簡單的單個節(jié)點和邊定制
注:這些設(shè)置只是針對單個節(jié)點或著邊;全局所有點或者節(jié)點設(shè)置需要使用visNodes 或 visEdges。
##節(jié)點屬性設(shè)置
- label : 指定節(jié)點標(biāo)簽的列
- value:指定節(jié)點大小
- group:指定節(jié)點所屬組劫侧,igraph包中有許多包裝好的方法可以用于網(wǎng)絡(luò)類群分析
- shape:節(jié)點形狀,例如circle, square 罚缕,triangle
- color:節(jié)點顏色
- title:設(shè)置提示信息割粮,當(dāng)您將鼠標(biāo)懸停在一個節(jié)點上時盾碗,提示就會出現(xiàn)
- shadow:定義一個節(jié)點是否有陰影(TRUE/FALSE)
在node data.frame上添加更多變量。有關(guān)可用選項舀瓢,請參閱visNodes()參數(shù)廷雅。
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
##邊屬性設(shè)置
- label:指定邊標(biāo)簽的列
- title:設(shè)置提示信息,當(dāng)您將鼠標(biāo)懸停在一個邊上時京髓,提示就會出現(xiàn)
- shadow:定義一個節(jié)點是否有陰影(TRUE/FALSE)
- length,width:定義網(wǎng)絡(luò)內(nèi)邊的長度/寬度
- arrows:箭頭在邊上的位置
- dashes:定義邊緣是否應(yīng)該虛線(TRUE/FALSE)
- smooth:光滑的線(TRUE/FALSE)
在edges data.frame上添加更多變量航缀。有關(guān)可用選項,請參閱visEdges()參數(shù)朵锣。
- 在edge data.frame上添加更多變量谬盐。有關(guān)可用選項,請參閱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%")
#全局節(jié)點/邊配置
- 使用visNodes和visEdges設(shè)置節(jié)點和邊全局屬性飞傀,并使用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"
#網(wǎng)絡(luò)配置
- 網(wǎng)絡(luò)配置可以使用visOptions, visInteraction, visLayout, visHierarchicalLayout, visPhysics:
##示例數(shù)據(jù)
> 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>
##添加圖例
- 你現(xiàn)在可以在節(jié)點/邊緣上添加更多的自定義圖例:
visNetwork(nodes, edges, width = "100%") %>% visLegend()
##或傳遞數(shù)據(jù)
visNetwork(nodes, edges, width = "100%") %>%
visLegend(useGroups = FALSE, addNodes = data.frame(label = "Nodes", shape = "circle"),
addEdges = data.frame(label = "link", color = "black"))
##突出臨近點連接(highlight Nearest)
- 使用highlightNearest選項诬烹,單擊一個節(jié)點可以突出顯示節(jié)點和邊:
visNetwork(nodes, edges, width = "100%") %>%
visOptions(highlightNearest = TRUE)
##可以設(shè)置突出程度
visNetwork(nodes, edges, width = "100%") %>%
visOptions(highlightNearest = list(enabled =TRUE, degree = 2))
##按節(jié)點id選擇(Select by node id)
你也可以選擇節(jié)點的id/標(biāo)簽與一個列表與nodesIdSelection:
visNetwork(nodes, edges, width = "100%") %>%
visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)
##通過一列設(shè)置選擇(Select by a column)
使用selectedBy選項通過列的值選擇一些節(jié)點:
# 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")
##有向網(wǎng)絡(luò)
visNetwork(nodes, edges, width = "100%") %>%
visEdges(arrows = 'from')
##自定義導(dǎo)航鍵(Custom navigation)
vis.js提出了一些導(dǎo)航工具:
visNetwork(nodes, edges, width = "100%") %>%
visInteraction(navigationButtons = TRUE)
##數(shù)據(jù)操作
- 添加一些數(shù)據(jù)操作工具
visNetwork(nodes, edges, width = "100%") %>%
visOptions(manipulation = TRUE)
##層次分布
- 可以根據(jù)visHierarchicalLayout和visLayout使用和控制分層布局:
nodes <- data.frame(id = 1:7)
edges <- data.frame(
from = c(1,2,2,2,3,3),
to = c(2,3,4,5,6,7)
)
visNetwork(nodes, edges, width = "100%") %>%
visEdges(arrows = "from") %>%
visHierarchicalLayout()
# same as visLayout(hierarchical = TRUE)
visNetwork(nodes, edges, width = "100%") %>%
visEdges(arrows = "from") %>%
visHierarchicalLayout(direction = "LR")
固定網(wǎng)絡(luò)砸烦,不允許縮放
visNetwork(nodes, edges, width = "100%") %>%
visInteraction(dragNodes = FALSE, dragView = FALSE, zoomView = FALSE)
##在網(wǎng)絡(luò)中使用字體圖標(biāo)
- 您可以在組或節(jié)點選項使用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()
#附加特性
##可視化rpart對象
- visTree函數(shù)允許可視化和自定義一個rpart分類和回歸樹家破。visTreeEditor允許編輯和返回網(wǎng)絡(luò)颜说,或visTreeModuleServer使用定制樹模塊。
##在Shiny中使用
- visNetworkOutput 和renderVisNetwork. 在shiny中汰聋,可以通過nodesIdSelection選擇顯示數(shù)據(jù)门粪。進(jìn)一步需要實現(xiàn)更多的功能,可以了解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
##導(dǎo)出
network <- visNetwork(nodes, edges, width = "100%")
visSave(network, file = "network.html")
##直接使用網(wǎng)絡(luò)數(shù)據(jù)(Use DOT language data)
visNetwork(dot = 'dinetwork {1 -> 1 -> 2; 2 -> 3; 2 -- 4; 2 -> 1 }', width = "100%")
##使用gephi json文件
# don't run here
visNetwork(gephi = 'WorldCup2014.json')
#參考:
Itroduction to visNetwork
visNetwork
系列文章:
R語言進(jìn)行網(wǎng)絡(luò)分析的基礎(chǔ)包 igraph
networkD3 繪制動態(tài)網(wǎng)絡(luò)