visNetwork needs at least two informations :
a nodes data.frame, with id column
a edges data.frame, with from and to columns
library(tidyverse)
library(visNetwork)
# minimal example
nodes <- data.frame(id = 1:10)
edges <- data.frame(from = c(1,2), to = c(1,3))
edges
## from to
## 1 1 1
## 2 2 3
visNetwork(nodes, edges, width = "100%")
Besides the help R functions, a vignette is available, and you can access and read the full javascript API :
# visDocumentation()
# vignette("Introduction-to-visNetwork") # with CRAN version
# shiny ?
# shiny::runApp(system.file("shiny", package = "visNetwork"))
Or visit our online help page :
Adding more variables on nodes data.frame. See visNodes for available options.
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 1 Node 1 GrA 1 square <p><b>1</b><br>Node !</p> darkred FALSE
## 2 2 Node 2 GrB 2 triangle <p><b>2</b><br>Node !</p> grey TRUE
## 3 3 Node 3 GrA 3 box <p><b>3</b><br>Node !</p> orange FALSE
## 4 4 Node 4 GrB 4 circle <p><b>4</b><br>Node !</p> darkblue TRUE
## 5 5 Node 5 GrA 5 dot <p><b>5</b><br>Node !</p> purple TRUE
## 6 6 Node 6 GrB 6 star <p><b>6</b><br>Node !</p> darkred FALSE
Adding more variables on edges data.frame. See visEdges for available options.
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 6 4 Edge 1 100 to TRUE Edge 1 FALSE FALSE
## 2 2 2 Edge 2 500 from FALSE Edge 2 TRUE TRUE
## 3 9 1 Edge 3 100 middle TRUE Edge 3 FALSE FALSE
## 4 7 10 Edge 4 500 middle;to FALSE Edge 4 TRUE TRUE
## 5 1 7 Edge 5 100 to TRUE Edge 5 FALSE FALSE
## 6 10 3 Edge 6 500 from FALSE Edge 6 TRUE TRUE
visNetwork(nodes, edges, width = "100%")
Set global options for nodes and edges using visNodes and visEdges, and use options per group using 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))
nodes
## id group
## 1 1 A
## 2 2 A
## 3 3 B
## 4 4 B
## 5 5 B
edges
## from to
## 1 2 1
## 2 5 2
## 3 3 4
## 4 3 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"
Configuration options are available in visOptions, visInteraction, visLayout, visHierarchicalLayout, visPhysics :
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)
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>"))
nodes
## id label group value title
## 1 1 Label 1 A 1 <p>1<br>Tooltip !</p>
## 2 2 Label 2 A 2 <p>2<br>Tooltip !</p>
## 3 3 Label 3 A 3 <p>3<br>Tooltip !</p>
## 4 4 Label 4 B 4 <p>4<br>Tooltip !</p>
## 5 5 Label 5 B 5 <p>5<br>Tooltip !</p>
## 6 6 Label 6 C 6 <p>6<br>Tooltip !</p>
## 7 7 Label 7 C 7 <p>7<br>Tooltip !</p>
## 8 8 Label 8 B 8 <p>8<br>Tooltip !</p>
## 9 9 Label 9 A 9 <p>9<br>Tooltip !</p>
## 10 10 Label 10 A 10 <p>10<br>Tooltip !</p>
edges
## from to value label title
## 1 6 8 8.123436 Edge 1 <p>1<br>Edge Tooltip !</p>
## 2 4 2 11.543169 Edge 2 <p>2<br>Edge Tooltip !</p>
## 3 6 2 9.631859 Edge 3 <p>3<br>Edge Tooltip !</p>
## 4 5 8 9.020768 Edge 4 <p>4<br>Edge Tooltip !</p>
## 5 5 2 11.378099 Edge 5 <p>5<br>Edge Tooltip !</p>
## 6 2 5 7.973995 Edge 6 <p>6<br>Edge Tooltip !</p>
## 7 6 7 10.851901 Edge 7 <p>7<br>Edge Tooltip !</p>
## 8 8 7 10.987652 Edge 8 <p>8<br>Edge Tooltip !</p>
## 9 4 1 9.902518 Edge 9 <p>9<br>Edge Tooltip !</p>
## 10 1 5 9.933396 Edge 10 <p>10<br>Edge Tooltip !</p>
New feature in visNetwork_0.1.2 : you can now add more custom legend on nodes / edges ! Default on groups (like in previous versions) :
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"))
You can highlight nearest nodes and edges clicking on a node with highlightNearest option :
visNetwork(nodes, edges, width = "100%") %>%
visOptions(highlightNearest = TRUE)
It’s now possible to control the degree of depth (visNetwork >= 0.1.2) :
visNetwork(nodes, edges, width = "100%") %>%
visOptions(highlightNearest = list(enabled =TRUE, degree = 2))
You can also select nodes by id/label with a list with nodesIdSelection :
visNetwork(nodes, edges, width = "100%") %>%
visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)
# can be the column you want
nodes$sel <- sample(c("sel1", "sel2"), nrow(nodes), replace = TRUE)
visNetwork(nodes, edges, width = "100%") %>%
visOptions(selectedBy = "sel")
visNetwork(nodes, edges, width = "100%") %>%
visEdges(arrows = 'from') %>%
visHierarchicalLayout()
You can use and control hierarchical layout with visHierarchicalLayout and 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)
)
nodes
## id
## 1 1
## 2 2
## 3 3
## 4 4
## 5 5
## 6 6
## 7 7
edges
## from to
## 1 1 2
## 2 2 3
## 3 2 4
## 4 2 5
## 5 3 6
## 6 3 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")
visNetwork(nodes, edges, width = "100%") %>%
visInteraction(dragNodes = FALSE, dragView = FALSE, zoomView = FALSE)
You can use Font Awesome icons using groups or nodes options. Font Awesome library (http://fortawesome.github.io/Font-Awesome/) is not part of dependencies. use addFontAwesome() if needed.
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()
New visTree function allows to visualize and customize a rpart classification and regression tree. Have a look to visTreeEditor to edity and get back network, or to visTreeModuleServer to use custom tree module in R.
library(rpart)
# install.packages("sparkline")
library(sparkline)
# Complex tree
data("solder")
res <- rpart(Opening~., data = solder, control = rpart.control(cp = 0.00005))
visTree(res, height = "800px", nodesPopSize = TRUE, minNodeSize = 10, maxNodeSize = 30)
With visNetworkOutput and renderVisNetwork. Using with shiny, and enabled options nodesIdSelection, you can access to new input with current selection value. Morevoer, you can do a lot of things with visNetworkProxy.