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

R语言Plotly绘制桑基图出现异常连接问题求助

问题分析与解决

你的桑基图连接显示错误,核心原因是链接构建方式不对:你直接把每一行数据拆成「基因→条件」「条件→类别」两条链接,导致links里有大量重复的同来源-目标组合。plotly绘制时会把这些小流量链接堆叠,造成视觉上的线条错位,让你误以为Gene3的连接指向了Category1。

不需要重新排列标签,只需要先聚合每一层的唯一连接及对应流量,就能解决问题。

修正后的代码

library(plotly)
library(dplyr) # 用于聚合数据

# 示例数据
example.data <- data.frame(
  genes = c("Gene1", "Gene2", "Gene3", "Gene4", "Gene5", "Gene6", "Gene7", "Gene8", "Gene9"),
  conditions = c("Control", "Control", "Treatment", "Treatment", "Treatment", "Treatment", "Treatment", "Treatment", "Treatment"),
  category = c("Category1", "Category1", "Category2", "Category2", "Category2", "Category2", "Category2", "Category1", "Category2")
)

# 1. 聚合各层级的连接与流量
# 基因→条件的连接
gene_to_condition <- example.data %>%
  count(genes, conditions, name = "value")

# 条件→类别的连接
condition_to_category <- example.data %>%
  count(conditions, category, name = "value")

# 2. 构建节点列表(保持原顺序:基因→条件→类别)
nodes <- data.frame(
  name = unique(c(as.character(example.data$genes),
                  as.character(example.data$conditions),
                  as.character(example.data$category)))
)

# 3. 转换为plotly需要的source/target索引(索引从0开始)
links_gene_condition <- data.frame(
  source = match(gene_to_condition$genes, nodes$name) - 1,
  target = match(gene_to_condition$conditions, nodes$name) - 1,
  value = gene_to_condition$value
)

links_condition_category <- data.frame(
  source = match(condition_to_category$conditions, nodes$name) - 1,
  target = match(condition_to_category$category, nodes$name) - 1,
  value = condition_to_category$value
)

# 合并所有链接
links <- rbind(links_gene_condition, links_condition_category)

# 绘制桑基图
plotly::plot_ly(
  type = "sankey",
  domain = list(x = c(0,1), y = c(0,1)),
  orientation = "h",
  node = list(
    label = nodes$name,
    pad = 15,
    thickness = 15,
    line = list(color = "black", width = 0.5)
  ),
  link = list(
    source = links$source,
    target = links$target,
    value = links$value
  )
)

关键说明

  • 聚合后,「Treatment→Category2」的流量是6,「Treatment→Category1」的流量是1,plotly会用单条粗线展示聚合后的流量,避免了多条细线堆叠导致的视觉错位。
  • 每个基因到条件的连接保持为流量1,确保每个基因的流向清晰,不会和其他基因的链接混淆。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 22:17:09