You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R中将riverplot绘制的桑基图保存为SVG格式?

解决gvisSankey桑基图导出SVG的问题

你用googleVis包的gvisSankey生成的交互式桑基图,因为依赖浏览器JS渲染,直接导出SVG确实有难度。下面给你几个可行的解决方案,从简单适配到替代方案都覆盖到了:

方案一:用webshot2包将HTML渲染为SVG

这个方法借助无头浏览器把gvisSankey生成的HTML页面渲染后导出为SVG,能完整保留你设置的所有样式:

  1. 先安装并加载所需工具包:
install.packages("webshot2")
library(googleVis)
library(webshot2)
  1. 生成桑基图并保存为临时HTML文件:
# 你的原有代码逻辑
df <- your_data_here
colors_node <- c('green','blue','red','yellow','brown','orange','darkviolet', 'grey','navy','aquamarine','darkgreen','firebrick')
colors_node_array <- paste0("[", paste0("'", colors_node,"'", collapse = ','), "]")
opts <- paste0("{ iterations: 0, link: { color: { stroke: 'black', strokeWidth: 1 } }, node: { colors: ", colors_node_array ,", label: { fontName: 'Arial', fontSize: 8, color: '#871b47', bold: true, italic: true }} }" )
sankey_plot <- gvisSankey(df, from="origin", to="visit", weight="weight", options=list( height=400, width = 300, sankey=opts ))

# 保存为临时HTML文件
html_file <- tempfile(fileext = ".html")
print(sankey_plot, file = html_file)
  1. 用webshot2导出为SVG:
webshot(html_file, "sankey_plot.svg", vwidth = 300, vheight = 400)

这个方法调用Chrome无头模式渲染页面,导出的SVG会完整保留节点颜色、链接边框和标签格式。

方案二:换用支持直接导出SVG的桑基图包

如果不想依赖HTML渲染,推荐用networkD3或者ggsankey包,它们能直接生成可导出为SVG的图表:

用networkD3的示例:

install.packages("networkD3")
library(networkD3)
library(dplyr)

# 转换数据为networkD3要求的结构
nodes <- data.frame(
  name = unique(c(as.character(df$origin), as.character(df$visit)))
)
links <- df %>% 
  mutate(
    source = match(origin, nodes$name) - 1,
    target = match(visit, nodes$name) - 1
  )

# 匹配你原来的节点颜色列表
node_colors <- colors_node[1:nrow(nodes)]

# 生成桑基图
sankey_net <- sankeyNetwork(
  Links = links, Nodes = nodes,
  Source = "source", Target = "target", Value = "weight",
  NodeID = "name",
  nodeStroke = "black", nodeWidth = 20,
  nodePadding = 10, fontSize = 8,
  colourScale = JS(paste0("d3.scaleOrdinal().range(['", paste(node_colors, collapse = "', '"), "'])"))
)

# 保存为SVG(需依赖rsvg包)
install.packages("rsvg")
library(htmlwidgets)
saveWidget(sankey_net, "sankey_temp.html")
webshot2::webshot("sankey_temp.html", "sankey_networkD3.svg", vwidth = 300, vheight = 400)

用ggsankey(ggplot2生态,定制更直观):

install.packages(c("ggsankey", "ggplot2"))
library(ggsankey)
library(ggplot2)

# 转换数据格式
df_long <- df %>%
  make_long(origin, visit, weight = weight)

# 绘制桑基图
ggplot(df_long, aes(x = x, next_x = next_x, node = node, next_node = next_node, fill = node, label = node)) +
  geom_sankey(flow.fill = "gray", color = "black", flow.width = 1) +
  geom_sankey_label(size = 2.5, color = "#871b47", fontface = "bold.italic", family = "Arial") +
  scale_fill_manual(values = colors_node) +
  theme_sankey(base_size = 8) +
  theme(legend.position = "none")

# 直接导出SVG
ggsave("sankey_ggsankey.svg", width = 300/72, height = 400/72, dpi = 72)

这个方案完全基于ggplot2生态,导出SVG的操作非常直接,样式也能和你原本的需求精准匹配。

方案三:RSelenium手动控制浏览器导出(复杂但灵活)

如果前两个方案都不适用,可以用RSelenium打开浏览器加载HTML后手动触发SVG导出,但步骤相对繁琐,适合需要高度自定义的场景,这里优先推荐前两个更简便的方案。

内容的提问来源于stack exchange,提问作者Paperwing

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:56:52