Visnetwork图未保存为png图像

时间:2019-06-18 17:43:48

标签: r plot visnetwork

我使用visNetwork绘制了一个简单的网络,但是当我尝试将其另存为png时,会得到一个空白图像。

png("ex.png")
require(visNetwork, quietly = TRUE)
# minimal example
nodes <- data.frame(id = 1:3)
edges <- data.frame(from = c(1,2), to = c(1,3))
visNetwork(nodes, edges, width = "100%")
dev.off()

1 个答案:

答案 0 :(得分:1)

您可以将网络另存为html,然后捕获该文件的内容:

nodes <- data.frame(id = 1:3)
edges <- data.frame(from = c(1,2), to = c(1,3))

library(visNetwork)
plot<- visNetwork(nodes, edges, width = "100%")

html_name <- tempfile(fileext = ".html")
visSave(plot, html_name)

library(webshot); #webshot::install_phantomjs() #in case phantomjs was not installed 

webshot(html_name, zoom = 2, file = "ex.png")