|
| 1 | +--- |
| 2 | +output: |
| 3 | + slidy_presentation: |
| 4 | + highlight: haddock |
| 5 | + duration: 15 |
| 6 | + footer: "Quatrièmes Rencontres R - visNetwork - [email protected]" |
| 7 | +--- |
| 8 | + |
| 9 | + |
| 10 | +<div style="text-align:center" markdown="1"> |
| 11 | +<br><br><br><br><br><br> |
| 12 | +<h1>visNetwork, an R package for network visualization</h1> |
| 13 | +<h2>Quatrièmes Rencontres R</h2> |
| 14 | +< h3>B.Thieurmel - [email protected]</ h3> |
| 15 | +<h3>26/06/2015</h3> |
| 16 | +<img src="images/R.jpg" alt="Drawing" style="width: 200px;"/> |
| 17 | +<img src="images/data_medium.png" alt="Drawing" style="width: 400px;"/> |
| 18 | + |
| 19 | + |
| 20 | +</div> |
| 21 | + |
| 22 | +## Introduction |
| 23 | + |
| 24 | +More and more visualization tools are available with R : |
| 25 | + |
| 26 | +* growing use of Javascript libraries in packages since a few years ([rCharts](http://rcharts.io/), [googleVis](http://cran.r-project.org/web/packages/googleVis/vignettes/googleVis_examples.html), ...) |
| 27 | +* amazing [shiny and shiny server](http://shiny.rstudio.com/) tools |
| 28 | +* [htmlwidgets](http://www.htmlwidgets.org/), a new 'best-seller' R package for JavaScript data visualization in the next years ? |
| 29 | + + facilitate JavaScript visualization libraries integration |
| 30 | + + use in R console, or with RStudio viewer |
| 31 | + + embed widgets in R Markdown documents and Shiny web applications |
| 32 | + + and save in .html file |
| 33 | + + already a lot of children ! |
| 34 | + + [leaflet](http://rstudio.github.io/leaflet/) : creating dynamic maps |
| 35 | + + [dygraphs](http://rstudio.github.io/dygraphs/) : time-series |
| 36 | + + [DT](http://rstudio.github.io/DT/) : interactive HTML tables |
| 37 | + + [threejs](https://github.com/bwlewis/rthreejs/) : 3D scatterplot and 3D globe |
| 38 | + + [rpivotTable](https://github.com/smartinsightsfromdata/rpivotTable) : data exploration, drag'n'drop functionality |
| 39 | + + [networkD3](http://christophergandrud.github.io/networkD3/) : network and sankey diagrams using D3.js |
| 40 | + + [sparkline](https://github.com/htmlwidgets/sparkline) : jquery sparkline |
| 41 | + + ... |
| 42 | + |
| 43 | +## visNetwork |
| 44 | + |
| 45 | +**visNetwork** is a R package for network visualization, using [vis.js](http://visjs.org/) javascript library |
| 46 | + |
| 47 | +* easy to use |
| 48 | +* custom shapes, styles, colors, sizes, ... |
| 49 | +* works smooth on any modern browser for up to a few thousand nodes and edges |
| 50 | +* clustering support foo handle a larger amount of nodes |
| 51 | +* interactivity controls (zoom, physics, movement of nodes, tooltip) |
| 52 | + |
| 53 | +<br> |
| 54 | +Based on [htmlwidgets](http://www.htmlwidgets.org/), so : |
| 55 | + |
| 56 | +* compatible with [shiny ](http://shiny.rstudio.com/), R Markdown documents, RStudio viewer, R |
| 57 | + |
| 58 | +<br> |
| 59 | +Actually only available on [DataKnowledge github](https://github.com/DataKnowledge) |
| 60 | + |
| 61 | +```{r, eval = FALSE} |
| 62 | +# for install visNetwork |
| 63 | +devtools::install_github("dataknowledge/visNetwork") |
| 64 | +``` |
| 65 | + |
| 66 | + |
| 67 | +```{r, echo = FALSE, message = FALSE} |
| 68 | +require(visNetwork, quietly = TRUE) |
| 69 | +``` |
| 70 | + |
| 71 | +## Minimal example |
| 72 | + |
| 73 | +**visNetwork** needs at least two informations : |
| 74 | + |
| 75 | +* a __nodes__ data.frame, with __id__ column |
| 76 | +* a __edges__ data.frame, with __from__ and __to__ columns |
| 77 | + |
| 78 | +```{r} |
| 79 | +nodes <- data.frame(id = 1:3) |
| 80 | +edges <- data.frame(from = c(1,2), to = c(1,3)) |
| 81 | +visNetwork(nodes = nodes, edges = edges, width = "1000px", height = "300px") |
| 82 | +
|
| 83 | +visNetwork(nodes = nodes, edges = edges) |
| 84 | +
|
| 85 | +visNetwork(nodes = nodes, edges = edges, width = "1000px", height = "300px") |
| 86 | +``` |
| 87 | + |
| 88 | +## Simple individual nodes and edges customization |
| 89 | + |
| 90 | +* Adding more variables on nodes and edges data.frames |
| 91 | +<br><br> |
| 92 | +```{r} |
| 93 | +nodes <- data.frame(id = 1:4, |
| 94 | + label = paste("Node", 1:4), # add labels on nodes |
| 95 | + group = c("GrA", "GrB"), # control groups on nodes (adding a color per group) |
| 96 | + value = 1:4, # control size adding value |
| 97 | + shape = c("square", "triangle", "box", "circle"), # control shape of nodes |
| 98 | + title = paste0("<p>", 1:4,"<br>Node Tooltip !</p>")) # add tooltip information (html) |
| 99 | +
|
| 100 | +edges <- data.frame(from = c(1,1,2,3), |
| 101 | + to = c(2,3,4,2), |
| 102 | + color = c("purple", "green"), # add color of edges |
| 103 | + style = c("line", "arrow", |
| 104 | + "arrow-center", "dash-line"), # control line style |
| 105 | + label = paste("Edge", 1:4), # add labels on nodes |
| 106 | + title = paste0("Edge Tooltip ", 1:4)) # add tooltip information (character) |
| 107 | +``` |
| 108 | +<br> |
| 109 | + |
| 110 | +* Numerous additional options are available(label alignment, opacity, font and border style, size control, ...) |
| 111 | + |
| 112 | +---- |
| 113 | + |
| 114 | +```{r, echo = FALSE} |
| 115 | +visNetwork(nodes, edges, width = "1000px", height = "600px") |
| 116 | +``` |
| 117 | + |
| 118 | +## Global nodes/edges configuration |
| 119 | + |
| 120 | +* Set global options for nodes and edges using _visNodes_ and _visEdges_, and use options per group using _visGroups_. |
| 121 | +<br> |
| 122 | +```{r} |
| 123 | +nodes <- data.frame(id = 1:5, group = c(rep("A", 2), rep("B", 3))) |
| 124 | +edges <- data.frame(from = c(2,5,3,3), to = c(1,2,4,2)) |
| 125 | +
|
| 126 | +visNetwork(nodes, edges, width = "100%", height = "300px") %>% |
| 127 | + visNodes(shape = "square") %>% |
| 128 | + visEdges(arrow ="from") %>% |
| 129 | + visGroups(groupname = "A", color = "darkblue") %>% |
| 130 | + visGroups(groupname = "B", color = "red") |
| 131 | +``` |
| 132 | + |
| 133 | +## Options |
| 134 | +A lot of configuration options are available in *visOptions* : |
| 135 | + |
| 136 | +```{r, echo=FALSE} |
| 137 | +nb <- 10 |
| 138 | +nodes <- data.frame(id = 1:nb, label = paste("Label", 1:nb), |
| 139 | + group = sample(LETTERS[1:3], nb, replace = TRUE), value = 1:nb, |
| 140 | + title = paste0("<p>", 1:nb,"<br>Tooltip !</p>"), stringsAsFactors = FALSE) |
| 141 | +
|
| 142 | +edges <- data.frame(from = trunc(runif(nb)*(nb-1))+1, |
| 143 | + to = trunc(runif(nb)*(nb-1))+1, |
| 144 | + value = rnorm(nb, 10), label = paste("Edge", 1:nb), |
| 145 | + title = paste0("<p>", 1:nb,"<br>Edge Tooltip !</p>")) |
| 146 | +``` |
| 147 | + |
| 148 | +* Highlight nearest nodes and edges clicking on a node |
| 149 | + |
| 150 | +```{r, eval = FALSE} |
| 151 | +visNetwork(nodes, edges) %>% visOptions(highlightNearest = TRUE) |
| 152 | +``` |
| 153 | + |
| 154 | +```{r, echo = FALSE} |
| 155 | +visNetwork(nodes, edges, width = "100%", height = "450px") %>% visOptions(highlightNearest = TRUE) %>% |
| 156 | + visPhysics(stabilization = list(iterations = 2000)) |
| 157 | +``` |
| 158 | + |
| 159 | +---- |
| 160 | + |
| 161 | +* Add legend on groups |
| 162 | +```{r, eval = FALSE} |
| 163 | +visNetwork(nodes, edges, width = "100%", legend = TRUE) |
| 164 | +``` |
| 165 | + |
| 166 | +```{r, echo = FALSE} |
| 167 | +visNetwork(nodes, edges, width = "100%", legend = TRUE, height = "575px") %>% |
| 168 | + visPhysics(stabilization = list(iterations = 2000)) |
| 169 | +``` |
| 170 | + |
| 171 | +---- |
| 172 | + |
| 173 | +* Select by node id |
| 174 | +```{r, eval = FALSE} |
| 175 | +visNetwork(nodes, edges, width = "100%") %>% visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE) |
| 176 | +``` |
| 177 | + |
| 178 | +```{r, echo = FALSE} |
| 179 | +visNetwork(nodes, edges, width = "100%", height = "575px") %>% visOptions( highlightNearest = TRUE, nodesIdSelection = TRUE) %>% |
| 180 | + visPhysics(stabilization = list(iterations = 2000)) |
| 181 | +``` |
| 182 | + |
| 183 | + |
| 184 | +---- |
| 185 | + |
| 186 | +* Data Manipulation |
| 187 | + |
| 188 | +```{r, eval = FALSE} |
| 189 | +visNetwork(nodes, edges, width = "100%") %>% visOptions(dataManipulation = TRUE) |
| 190 | +``` |
| 191 | + |
| 192 | +```{r, echo = FALSE} |
| 193 | +visNetwork(nodes, edges, width = "100%", height = "575px") %>% visOptions(manipulation = TRUE) %>% |
| 194 | + visPhysics(stabilization = list(iterations = 2000)) |
| 195 | +``` |
| 196 | + |
| 197 | + |
0 commit comments