6

I have created a interactive visualisation using the following code:

library(networkD3)

nodes = data.frame("name" = c("node1", "node2","node3", "node4", "node5", "node6", "node7"))
links = as.data.frame(matrix(c(
0,1,7937,
0,2,6990,
0,3,2483,
1,4,2120,
2,4,666,
3,4,282,
1,5,4583,
2,5,5657,
3,5,731,
1,6,1234,
2,6,756,
3,6,1470), byrow = TRUE, ncol = 3))

names(links) = c("source", "target", "value")

sankey <- sankeyNetwork(Links = links, Nodes = nodes,
          Source = "source", Target = "target",
          Value = "value", NodeID = "name",
          fontSize= 12, nodeWidth = 15)'

This is my first time using the networkD3 package (or any interactive package for that matter) and from playing around I found that to keep it interactive it has to be published as a webpage (or is there another way??) but looking through the documentation for the package I can't see a way to add a title or a caption / comments. I want to share this piece of work round so need to explain what each level means on the published webpage ideally

CJ Yetman
  • 8,373
  • 2
  • 24
  • 56
Callum Smyth
  • 127
  • 2
  • 7

2 Answers2

9

There is no feature built-in to networkD3 to add titles or captions, but you can use functions in the htmlwidgets package to prepend or append content to an htmlwidget. There are numerous options, but for example....

library(htmlwidgets)
library(htmltools)

sankey <- htmlwidgets::prependContent(sankey, htmltools::tags$h1("Title"))
sankey <- htmlwidgets::appendContent(sankey, htmltools::tags$p("Caption"))
CJ Yetman
  • 8,373
  • 2
  • 24
  • 56
  • Thanks for the answer. Can I ask why you call it like htmlwidgets::prepend... instead of how you normally call a function? – Callum Smyth May 02 '18 at 15:27
  • just to be clear where the function is coming from... it's technically unnecessary once you've loaded the packages with `library(htmlwidgets)` and `library(htmltools)` – CJ Yetman May 02 '18 at 16:10
  • `%>%` `prependContent("Text")` I ended up using this to add a title, but it keeps pushing my viz down and cutting the bottom off. This stays off even when saving as a webpage. Is there anyway I can stop this from happening? – Callum Smyth May 03 '18 at 15:12
  • try `sankey$sizingPolicy$viewer$fill <- FALSE` – CJ Yetman May 03 '18 at 21:03
5

Responding to the comment, "I ended up using this to add a title, but it keeps pushing my viz down and cutting the bottom off. This stays off even when saving as a webpage. Is there anyway I can stop this from happening?"

I tried the suggested reply of adding sankey$sizingPolicy$viewer$fill <- FALSE, however, it made my sankey smaller than I wanted it. I found out that you can adjust the width and height of the Sankey prior to adding the HTML widget by adding width=(desired width) and height=(desired height) and this creates the space to then add in the title and the comment, as suggested by CJ Yetman.

library(networkD3)
library(htmlwidgets)
library(htmltools)

nodes = data.frame("name" = c("node1", "node2","node3", "node4", "node5", "node6", "node7"))
links = as.data.frame(matrix(c(
  0,1,7937,
  0,2,6990,
  0,3,2483,
  1,4,2120,
  2,4,666,
  3,4,282,
  1,5,4583,
  2,5,5657,
  3,5,731,
  1,6,1234,
  2,6,756,
  3,6,1470), byrow = TRUE, ncol = 3))

names(links) = c("source", "target", "value")

sankey <- sankeyNetwork(Links = links, Nodes = nodes,
                        Source = "source", Target = "target",
                        Value = "value", NodeID = "name",
                        fontSize= 12, nodeWidth = 15,
                        width= 900, height=600)


sankey <- htmlwidgets::prependContent(sankey, htmltools::tags$h1("Title"))
sankey <- htmlwidgets::appendContent(sankey, htmltools::tags$p("Caption"))

sankey
Christy S
  • 51
  • 1
  • 2