visLegend {visNetwork} | R Documentation |
Add a legend on a visNetwork object
visLegend(graph, enabled = TRUE, useGroups = TRUE, addNodes = NULL, addEdges = NULL, width = 0.2, position = "left")
graph |
: a visNetwork object |
enabled |
: Boolean. Default to TRUE. |
useGroups |
: use groups options in legend ? Default to TRUE. |
addNodes |
: a data.frame or a list for adding custom node(s) |
addEdges |
: a data.frame or a list for adding custom edges(s) |
width |
: Number, in [0,...,1]. Default to 0.2 |
position |
: one of "left" (Default) or "right" |
visNodes for nodes options, visEdges for edges options, visGroups for groups options, visLegend for adding legend, visOptions for custom option, visLayout & visHierarchicalLayout for layout, visPhysics for control physics, visInteraction for interaction, visNetworkProxy & visFocus & visFit for animation within shiny, visDocumentation, visEvents, visConfigure ...
# minimal example 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) %>% visGroups(groupname = "A", color = "red") %>% visGroups(groupname = "B", color = "lightblue") %>% visLegend() # default, on group, adjust width + change position visNetwork(nodes, edges) %>% visGroups(groupname = "A", color = "red") %>% visGroups(groupname = "B", color = "lightblue") %>% visLegend(width = 0.05, position = "right") # passing custom nodes and/or edges lnodes <- data.frame(label = c("Group A", "Group B"), shape = c( "ellipse"), color = c("red", "lightblue"), title = "Informations", id = 1:2) visNetwork(nodes, edges) %>% visGroups(groupname = "A", color = "red") %>% visGroups(groupname = "B", color = "lightblue") %>% visLegend(addNodes = lnodes, useGroups = FALSE) ledges <- data.frame(color = c("lightblue", "red"), label = c("reverse", "depends"), arrows =c("to", "from")) visNetwork(nodes, edges) %>% visGroups(groupname = "A", color = "lightblue") %>% visGroups(groupname = "B", color = "red") %>% visLegend(addEdges = ledges) # for more complex option, you can use a list(of list...) # or a data.frame with specific notaion nodes <- data.frame(id = 1:3, group = c("B", "A", "B")) edges <- data.frame(from = c(1,2), to = c(2,3)) # using a list 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")) ), addEdges = data.frame(label = "link"), 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)