Title

since visNetwork_0.2.2, you can add a title (main) 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", 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, since visNetwork_0.2.2) :

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

Since 0.1.2, you can 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 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)

© 2016-2017 DataStorm

Fork me on GitHub