Network visualization

Share:

Description

Network visualization using vis.js library. For full documentation, have a look at visDocumentation.

Usage

1
2
3
visNetwork(nodes = NULL, edges = NULL, dot = NULL, gephi = NULL,
  width = NULL, height = NULL, main = NULL, submain = NULL,
  footer = NULL, ...)

Arguments

nodes

: data.frame with nodes informations. Needed at least column "id". See visNodes

  • "id" : id of the node, needed in edges information

  • "label" : label of the node

  • "group" : group of the node. Groups can be configure with visGroups

  • "value" : size of the node

  • "title" : tooltip of the node

  • ...

edges

: data.frame with edges informations. Needed at least columns "from" and "to". See visEdges

  • "from" : node id of begin of the edge

  • "to" : node id of end of the edge

  • "label" : label of the edge

  • "value" : size of the node

  • "title" : tooltip of the node

  • ...

dot

: Character DOT language.

gephi

: Json export gephi path file.

width

: Width (optional, defaults to automatic sizing)

height

: Height (optional, defaults to automatic sizing)

main

: For add a title. Character or a named list.

  • "text" : Character. Title.

  • "style" : Optional. Character. HTML style of title. Default to 'font-family:Georgia, Times New Roman, Times, serif;font-weight:bold;font-size:20px;text-align:center;'.

submain

: For add a subtitle. Character or a named list.

  • "text" : Character. Subtitle.

  • "style" : Optional. Character. HTML style of title. Default to 'font-family:Georgia, Times New Roman, Times, serif;font-size:12px;text-align:center;'.

footer

: For add a footer. Character or a named list.

  • "text" : Character. footer.

  • "style" : Optional. Character. HTML style of title. Default to 'font-family:Georgia, Times New Roman, Times, serif;font-size:12px;text-align:center;'.

...

: Don't use.

References

See online documentation http://datastorm-open.github.io/visNetwork/

See Also

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 ...

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# minimal example
nodes <- data.frame(id = 1:3)
edges <- data.frame(from = c(1,2), to = c(1,3))

visNetwork(nodes, edges)

# add a title
visNetwork(nodes, edges, main = "visNetwork minimal example")
visNetwork(nodes, edges, main = list(text = "visNetwork minimal example",
 style = "font-family:Comic Sans MS;color:#ff0000;font-size:15px;text-align:center;"))
 
# and subtitle and footer
visNetwork(nodes, edges, main = "visNetwork minimal example",
 submain = "For add a subtitle", footer = "Fig.1 minimal example")
 
# 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) 

# use more complex configuration : 
# when it's a list, you can use data.frame with specific notation like this
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)


# 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 an id node selection 
visNetwork(nodes, edges) %>% 
 visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)
 
# or add a selection on another column
visNetwork(nodes, edges) %>% 
 visOptions(selectedBy = "group")

nodes$sel <- sample(c("sel1", "sel2"), nrow(nodes), replace = TRUE)
visNetwork(nodes, edges) %>% 
 visOptions(selectedBy = "sel")
   
# add legend
visNetwork(nodes, edges) %>% visLegend()
    
# directed network
visNetwork(nodes, edges) %>% 
 visEdges(arrows = '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)

# use fontAwesome icons using groups or nodes options 
# font-awesome is not part of dependencies. use addFontAwesome() if needed
# http://fortawesome.github.io/Font-Awesome

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()

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

visNetwork(nodes, edges) %>%
  visNodes(shape = "icon", icon = list( face ='FontAwesome', code = "f0c0")) %>%
  addFontAwesome()

# Save a network
## Not run: 
network <- visNetwork(nodes, edges) %>% 
 visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE,
 manipulation = TRUE) %>% visLegend()
 
network %>% visSave(file = "network.html")
# same as
visSave(network, file = "network.html")

## End(Not run)

# Export as png/jpeg (shiny or browser only)
## Not run: 
visNetwork(nodes, edges) %>% 
 visExport()

## End(Not run)

# DOT language
visNetwork(dot = 'dinetwork {1 -> 1 -> 2; 2 -> 3; 2 -- 4; 2 -> 1 }')

# gephi json file
## Not run: 
visNetwork(gephi = 'WorldCup2014.json') %>% visPhysics(stabilization = FALSE,   barnesHut = list(
    gravitationalConstant = -10000,
    springConstant = 0.002,
    springLength = 150
  ))

## End(Not run)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.