Title

You can add a title (main), subtitle (submain) and footer (footer) to your network :

nodes <- data.frame(id = 1:3, group = c("B", "A", "B"))
edges <- data.frame(from = c(1,2), to = c(2,3))

# default, on group
visNetwork(nodes, edges, 
           main = "A really simple example", 
           submain = list(text = "Custom subtitle",
                          style = "font-family:Comic Sans MS;color:#ff0000;font-size:15px;text-align:center;"), 
           footer = "Fig.1 minimal example",
           width = "100%")

Legend based on groups

We can simply add a legend, based on the definition of groups, using visLegend :

# default, on group
visNetwork(nodes, edges, width = "100%") %>%
  visGroups(groupname = "A", color = "red") %>%
  visGroups(groupname = "B", color = "lightblue") %>%
  visLegend()

Placement & title

It’s also possible to adjust position (left or right), and width, and add a title (main) :

visNetwork(nodes, edges, width = "100%") %>%
  visGroups(groupname = "A", color = "red") %>%
  visGroups(groupname = "B", color = "lightblue") %>%
  visLegend(width = 0.1, position = "right", main = "Group")

Custom nodes/edges

And also add custom nodes (addNodes) and/or edges (addEdges) on the legend :

# nodes data.frame for legend
lnodes <- data.frame(label = c("Group A", "Group B"),
                     shape = c( "ellipse"), color = c("red", "lightblue"),
                     title = "Informations", id = 1:2)

# edges data.frame for legend
ledges <- data.frame(color = c("lightblue", "red"),
                     label = c("reverse", "depends"), arrows =c("to", "from"))

visNetwork(nodes, edges, width = "100%") %>%
  visGroups(groupname = "A", color = "red") %>%
  visGroups(groupname = "B", color = "lightblue") %>%
  visLegend(addEdges = ledges, addNodes = lnodes, useGroups = FALSE)

Finally, it’s possible to use the definition of the groups (useGroups = TRUE #default) with custom nodes (addNodes) and/or edges (addEdges) :

ledges <- data.frame(color = c("lightblue", "red"),
                     label = c("reverse", "depends"), arrows =c("to", "from"))

visNetwork(nodes, edges, width = "100%") %>%
  visGroups(groupname = "A", color = "red") %>%
  visGroups(groupname = "B", color = "lightblue") %>%
  visLegend(addEdges = ledges, useGroups = TRUE) 

For more complex elements, if you prefer, you can use a list instead of data.frame :

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) %>%
  visGroups(groupname = "A", shape = "icon", 
            icon = list(code = "f0c0", size = 75)) %>%
  visGroups(groupname = "B", shape = "icon", 
            icon = list(code = "f007", color = "red")) %>%
  addFontAwesome() %>%
  visLegend(addNodes = list(
    list(label = "Group", shape = "icon", 
         icon = list(code = "f0c0", size = 25)),
    list(label = "User", shape = "icon", 
         icon = list(code = "f007", size = 50, color = "red"))), 
    useGroups = FALSE)
# # using a data.frame
# addNodes <- data.frame(label = c("Group", "User"), shape = "icon",
#   icon.code = c("f0c0", "f007"), icon.size = c(25, 50), 
#   icon.color = c(NA, "red"))
# 
# visNetwork(nodes, edges) %>%
#  visGroups(groupname = "A", shape = "icon", 
#     icon = list(code = "f0c0", size = 75)) %>%
#  visGroups(groupname = "B", shape = "icon", 
#     icon = list(code = "f007", color = "red")) %>%
#  addFontAwesome() %>%
#  visLegend(addNodes = addNodes,
#    addEdges = data.frame(label = "link"), useGroups = FALSE)

© 2020-2021 DataStorm

Fork me on GitHub