# minimal example
nodes <- data.frame(id = 1:3)
edges <- data.frame(from = c(1,2), to = c(1,3))
visNetwork(nodes, edges)
# customization adding more variables (see visNodes and visEdges)
nodes <- data.frame(id = 1:10,
label = paste("Node", 1:10), # labels
group = c("GrA", "GrB"), # groups
value = 1:10, # size
shape = c("square", "triangle", "box", "circle", "dot", "star",
"ellipse", "database", "text", "diamond"), # shape
title = paste0("<p><b>", 1:10,"</b><br>Node !</p>"), # tooltip
color = c("darkred", "grey", "orange", "darkblue", "purple"),# color
shadow = c(FALSE, TRUE, FALSE, TRUE, TRUE)) # shadow
edges <- data.frame(from = sample(1:10,8), to = sample(1:10, 8),
label = paste("Edge", 1:8), # labels
length = c(100,500), # length
arrows = c("to", "from", "middle", "middle;to"), # arrows
dashes = c(TRUE, FALSE), # dashes
title = paste("Edge", 1:8), # tooltip
smooth = c(FALSE, TRUE), # smooth
shadow = c(FALSE, TRUE, FALSE, TRUE)) # shadow
visNetwork(nodes, edges)
# highlight nearest
nodes <- data.frame(id = 1:15, label = paste("Label", 1:15),
group = sample(LETTERS[1:3], 15, replace = TRUE))
edges <- data.frame(from = trunc(runif(15)*(15-1))+1,
to = trunc(runif(15)*(15-1))+1)
visNetwork(nodes, edges) %>% visOptions(highlightNearest = TRUE)
# try a legend...
visNetwork(nodes, edges, legend = TRUE)
# try an id node selection
visNetwork(nodes, edges) %>%
visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)
# directed network
visNetwork(nodes, edges) %>%
visEdges(arrow = 'from', scaling = list(min = 2, max = 2))
# custom navigation
visNetwork(nodes, edges) %>%
visInteraction(navigationButtons = TRUE)
# data Manipulation
visNetwork(nodes, edges) %>% visOptions(manipulation = TRUE)
# Hierarchical Layout
visNetwork(nodes, edges) %>% visHierarchicalLayout()
# freeze network
visNetwork(nodes, edges) %>%
visInteraction(dragNodes = FALSE, dragView = FALSE, zoomView = FALSE)
# Save a network
network <- visNetwork(nodes, edges, legend = TRUE) %>%
visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE,
manipulation = TRUE)
htmlwidgets::saveWidget(network, "network.html")
# DOT language
visNetwork(dot = 'dinetwork {1 -> 1 -> 2; 2 -> 3; 2 -- 4; 2 -> 1 }')
# gephi json file
visNetwork(gephi = 'WorldCup2014.json') %>% visPhysics(stabilization = FALSE, barnesHut = list(
gravitationalConstant = -10000,
springConstant = 0.002,
springLength = 150
))
Run the code above in your browser using DataLab