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

基于指定数据框使用Highcharter创建桑基图的技术问询

用Highcharter创建英国访客桑基图的完整实现

我来帮你补全并优化代码,实现你想要的桑基图效果,以下是可直接运行的完整版本:

library(highcharter)

# 定义访客数据框
UKvisits <- data.frame(
  origin = as.character(c( 
    "France", "Germany", "USA", "Irish Republic", "Netherlands", 
    "Spain", "Italy", "Poland", "Belgium", "Australia", "Other countries", 
    rep("UK", 5)
  )), 
  visit = as.character(c( 
    rep("UK", 11), "Scotland", "Wales", "Northern Ireland", "England", "London"
  )), 
  weights = c( 
    c(12,10,9,8,6,6,5,4,4,3,33)/100*31.8, 
    c(2.2,0.9,0.4,12.8,15.5)
  )
)

# 生成桑基图
hchart(UKvisits, "sankey", hcaes(from = origin, to = visit, weight = weights)) %>%
  hc_title(text = "英国访客来源与境内目的地分布") %>%
  hc_tooltip(pointFormat = "访客量: <b>{point.weight:.2f} 百万</b>") %>%
  hc_add_theme(hc_theme_smpl())

关键细节说明:

  • 数据映射逻辑:桑基图要求核心数据列对应from(起点)、to(终点)、weight(流量权重),你的origin/visit/weights刚好匹配,通过hcaes()完成字段映射即可。
  • 数据合理性:你将境外访客的百分比转换为百万级实际数值,和境内各区域的访客数格式统一,能让桑基图的流量宽度准确反映真实规模。
  • 图表可读性优化:添加了直观标题、格式化tooltip显示小数位数,搭配简洁主题让图表更清晰易读。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:26:02