We can customize nodes with a lot of options, and by various ways.
Individual configuration
As seen in the introduction, nodes must be a data.frame, with at least one column id
. You can add properties simply by adding variables on data.frame. See ?visNodes for available options.
The most used are presented in the following example :
nodes <- data.frame(id = 1:10,
# add labels on nodes
label = paste("Node", 1:10),
# add groups on nodes
group = c("GrA", "GrB"),
# size adding value
value = 1:10,
# control shape of nodes
shape = c("square", "triangle", "box", "circle", "dot", "star",
"ellipse", "database", "text", "diamond"),
# tooltip (html or character), when the mouse is above
title = paste0("<p><b>", 1:10,"</b><br>Node !</p>"),
# color
color = c("darkred", "grey", "orange", "darkblue", "purple"),
# shadow
shadow = c(FALSE, TRUE, FALSE, TRUE, TRUE))
# 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
edges <- data.frame(from = c(1,2,5,7,8,10), to = c(9,3,1,6,4,7))
visNetwork(nodes, edges, height = "500px", width = "100%")
Global configuration
If you want to set some configuration for all nodes, it’s possible in a simpler way, using the visNodes function directly :
nodes <- data.frame(id = 1:4)
edges <- data.frame(from = c(2,4,3,3), to = c(1,2,4,2))
visNetwork(nodes, edges, width = "100%") %>%
visNodes(shape = "square",
color = list(background = "lightblue",
border = "darkblue",
highlight = "yellow"),
shadow = list(enabled = TRUE, size = 10)) %>%
visLayout(randomSeed = 12) # to have always the same network
Finally, you can also combine individual and global options :
nodes <- data.frame(id = 1:4,
shape = c("circle", "square"),
label = LETTERS[1:4])
edges <- data.frame(from = c(2,4,3,3), to = c(1,2,4,2))
visNetwork(nodes, edges, width = "100%") %>%
visNodes(color = list(background = "lightblue",
border = "darkblue",
highlight = "yellow"),
shadow = list(enabled = TRUE, size = 10)) %>%
visLayout(randomSeed = 12) # to have always the same network
Use complex configuration individually
When options are available using a list, you can use it in a data.frame with specific notation like this (a dot per level):
nodes <- data.frame(id = 1:3,
color.background = c("red", "blue", "green"),
color.highlight.background = c("red", NA, "red"),
shadow.size = c(5, 10, 15))
edges <- data.frame(from = c(1,2), to = c(1,3),
label = LETTERS[1:2],
font.color =c ("red", "blue"),
font.size = c(10,20))
visNetwork(nodes, edges)