networkD3 graphs

I am trying to save a Sankey plot that I created using the sankeyNetwork() function of the "networkD3" package in a .png format. I have explored different options but have not figure out to get the high-resolution graph. Please give me some suggestions.

One of the option that I tried:
saveNetwork(p, "sn2.html")
webshot::webshot("sn2.html","sn.png", vwidth = 1000, vheight = 900)

Hi @gtmbini

htmlwidgetsor webshot are the only ones I know. What I do is saving the image by simply using the pull-down menu from the graphic window in R-Studio ('save as...') . You can specify resolution, but I don't know whether it is close to what you need. Have you tried that already?

JW

1 Like

This works for me. Maybe check your package versions and that phantoms is installed?

library(jsonlite)
library(networkD3)
library(webshot)

packageVersion("networkD3")
# [1] ‘0.4’
packageVersion("webshot")
# [1] ‘0.5.2’
is_phantomjs_installed()
# [1] TRUE

URL <- paste0('https://cdn.rawgit.com/christophergandrud/networkD3/',
              'master/JSONdata/energy.json')
energy <- jsonlite::fromJSON(URL)

p <- sankeyNetwork(Links = energy$links, Nodes = energy$nodes, Source = 'source',
              Target = 'target', Value = 'value', NodeID = 'name',
              units = 'TWh', fontSize = 12, nodeWidth = 30)

saveNetwork(p, "sn2.html")
webshot::webshot("sn2.html","sn.png", vwidth = 1000, vheight = 900)

or try webshot2, which can be installed with remotes::install_github("rstudio/webshot2")... it requires Chrome or a Chromium based browser to be installed.

library(jsonlite)
library(networkD3)
library(webshot2)

packageVersion("networkD3")
# [1] ‘0.4’
packageVersion("webshot2")
# [1] ‘0.0.0.9000’

URL <- paste0('https://cdn.rawgit.com/christophergandrud/networkD3/',
              'master/JSONdata/energy.json')
energy <- jsonlite::fromJSON(URL)

p <- sankeyNetwork(Links = energy$links, Nodes = energy$nodes, Source = 'source',
                   Target = 'target', Value = 'value', NodeID = 'name',
                   units = 'TWh', fontSize = 12, nodeWidth = 30)

saveNetwork(p, "sn2.html")
webshot("sn2.html", "sn.png", vwidth = 1000, vheight = 900)

Thank you for the suggestion, I end up using webshot, I explore the zoom option that is available in webshot function. It worked pretty well for me what I was looking for.

Thank you @cjyetman , for the suggestion and codes. I did not have any installation issue.

I was looking for some other options than webshot. But now I think it is one of the good option.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.