When I write a report in rmarkdown, all my figures as save automatically under the folder graphs
. However, as a Sankey diagram is different, it does not save as a picture (.png, etc...) automatically. Is there a workaround for this? (without manually saving each diagram via RStudio Plots panel?)
I saw this question before. But the option from rbokeh
produces a low quality graphic. I tried to use the second option, but it seems that there is an error in the code, because the function throws object 'vl' not found
.
As that question is from three years ago, I think that there may be a better solution so far.
Example graphic that I want to save:
---
title: "Untitled"
author: "Guilherme"
date: "12/5/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
fig.path = "graph/")
```
```{r}
library(networkD3)
URL <- paste0('https://cdn.rawgit.com/christophergandrud/networkD3/',
'master/JSONdata/energy.json')
energy <- jsonlite::fromJSON(URL)
# Plot
sankeyNetwork(Links = energy$links, Nodes = energy$nodes, Source = 'source',
Target = 'target', Value = 'value', NodeID = 'name',
units = 'TWh', fontSize = 12, nodeWidth = 30)
```