当我在 rmarkdown 中编写报告时,我的所有数据都会自动保存在该文件夹下graphs
。然而,由于桑基图不同,它不会自动保存为图片(.png 等)。有解决方法吗? (无需通过 RStudio Plots 面板手动保存每个图表?)
I saw 这个问题 https://stackoverflow.com/questions/43846931/saving-networkd3-sankey-diagram-using-code-only前。但选择从rbokeh
产生低质量的图形。我尝试使用第二个选项,但代码中似乎有错误,因为该函数抛出object 'vl' not found
.
由于这个问题是三年前的,我认为到目前为止可能有更好的解决方案。
我要保存的示例图形:
---
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)
```
像这样的事情怎么样:
--
title: "sankey as image"
author: "..."
date: "12/5/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
fig.path = "graph/")
```
```{r, fig.align='center'}
library(networkD3)
URL <- paste0('https://cdn.rawgit.com/christophergandrud/networkD3/',
'master/JSONdata/energy.json')
energy <- jsonlite::fromJSON(URL)
# Plot
sn <- sankeyNetwork(Links = energy$links, Nodes = energy$nodes, Source = 'source',
Target = 'target', Value = 'value', NodeID = 'name',
units = 'TWh', fontSize = 12, nodeWidth = 30)
# you save it as an html
saveNetwork(sn, "sn.html")
library(webshot)
# you convert it as png
webshot("sn.html","sn.png", vwidth = 1000, vheight = 900)
```
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)