|
1 | 1 | ---
|
2 |
| -name: Sankey Diagram |
3 |
| -permalink: r/sankey-diagram/ |
4 | 2 | description: How to create sankey diagrams in R with Plotly.
|
5 |
| -layout: base |
6 |
| -thumbnail: thumbnail/sankey.jpg |
7 |
| -language: r |
8 | 3 | display_as: basic
|
| 4 | +language: r |
| 5 | +layout: base |
| 6 | +name: Sankey Diagram |
9 | 7 | order: 16
|
10 | 8 | output:
|
11 | 9 | html_document:
|
12 | 10 | keep_md: true
|
| 11 | +permalink: r/sankey-diagram/ |
| 12 | +thumbnail: thumbnail/sankey.jpg |
13 | 13 | ---
|
14 | 14 |
|
15 | 15 | ```{r, echo = FALSE, message=FALSE}
|
@@ -91,8 +91,6 @@ p <- plot_ly(
|
91 | 91 | xaxis = list(showgrid = F, zeroline = F),
|
92 | 92 | yaxis = list(showgrid = F, zeroline = F)
|
93 | 93 | )
|
94 |
| -
|
95 |
| -p |
96 | 94 | ```
|
97 | 95 |
|
98 | 96 |
|
@@ -134,8 +132,6 @@ p <- plot_ly(
|
134 | 132 | xaxis = list(showgrid = F, zeroline = F),
|
135 | 133 | yaxis = list(showgrid = F, zeroline = F)
|
136 | 134 | )
|
137 |
| -
|
138 |
| -p |
139 | 135 | ```
|
140 | 136 |
|
141 | 137 | ### Add Links
|
@@ -239,7 +235,27 @@ p <- plot_ly(
|
239 | 235 | p
|
240 | 236 | ```
|
241 | 237 |
|
242 |
| -### Reference |
| 238 | +### Define Node Position |
| 239 | +The following example sets [node.x](https://plot.ly/r/reference/#sankey-node-x) and `node.y` to place nodes in the specified locations, except in the `snap arrangement` (default behaviour when `node.x` and `node.y` are not defined) to avoid overlapping of the nodes, therefore, an automatic snapping of elements will be set to define the padding between nodes via [nodepad](https://plot.ly/r/reference/#sankey-node-pad). The other possible arrangements are:<font color='blue'> 1)</font> perpendicular <font color='blue'>2)</font> freeform <font color='blue'>3)</font> fixed |
243 | 240 |
|
244 |
| -See [https://plot.ly/r/reference/#sankey](https://plot.ly/r/reference/#sankey) for more information and options! |
| 241 | +```{r} |
| 242 | +library(plotly) |
| 243 | +p <- plot_ly( |
| 244 | + type = "sankey", |
| 245 | + arrangement = "snap", |
| 246 | + node = list( |
| 247 | + label = c("A", "B", "C", "D", "E", "F"), |
| 248 | + x = c(0.2, 0.1, 0.5, 0.7, 0.3, 0.5), |
| 249 | + y = c(0.7, 0.5, 0.2, 0.4, 0.2, 0.3), |
| 250 | + pad = 10), # 10 Pixel |
| 251 | + link = list( |
| 252 | + source = c(0, 0, 1, 2, 5, 4, 3, 5), |
| 253 | + target = c(5, 3, 4, 3, 0, 2, 2, 3), |
| 254 | + value = c(1, 2, 1, 1, 1, 1, 1, 2))) %>% |
| 255 | + layout(title = "Sankey with manually positioned node") |
245 | 256 |
|
| 257 | +p |
| 258 | +``` |
| 259 | + |
| 260 | +### Reference |
| 261 | +See [https://plot.ly/r/reference/#sankey](https://plot.ly/r/reference/#sankey) for more information and options! |
0 commit comments