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

使用networkD3包的sankeyNetwork函数时自定义链接颜色与细胞类型不匹配的问题求助

使用networkD3包的sankeyNetwork函数时自定义链接颜色与细胞类型不匹配的问题求助

最近我在用R语言的networkD3包里的sankeyNetwork函数绘制桑基图,目的是展示不同层级细胞类型的数量变化,但遇到了一个棘手的问题——我自定义的颜色和设置的细胞类型对应不上,生成的图和我的预期效果有很大出入。

以下是我编写的代码:

library(networkD3)

source <- c("Epithelial", "Epithelial", "Endothelial", "Stroma", "Endothelial", "Immune", 
            "Immune", "Proliferating", "Stroma", "Fibroblast lineage", "Fibroblast lineage", 
            "Alveolar epithelium", "Alveolar epithelium", "Lymphoid", "Airway epithelium", 
            "CEC1", "Airway epithelium", "Airway epithelium", "LEC1", "Myeloid1", "Myeloid1", 
            "Myeloid1", "Myeloid1", "Proliferating2", "SM1", "Lymphoid")
target <- c("Airway epithelium", "Alveolar epithelium", "CEC1", "Fibroblast lineage", 
            "LEC1", "Lymphoid", "Myeloid1", "Proliferating2", "SM1", "Alf1", "Alf2", "AT1", 
            "AT2", "B", "Basal", "CEC2", "Ciliated", "Club", "LEC2", "Macrophages", "MAST", 
            "Monocytes", "Myeloid2", "Proliferating3", "SM2", "T_cell")
value <- c(14612, 18191, 15878, 21459, 4131, 30553, 38800, 743, 10607, 9946, 11513, 11568, 
           6623, 2436, 390, 15878, 11449, 2773, 4131, 11989, 2802, 13628, 10381, 743, 10607, 28117)
source2 <- c(0, 0, 1, 2, 1, 3, 3, 4, 2, 5, 5, 6, 6, 7, 8, 9, 8, 8, 10, 11, 11, 11, 11, 12, 13, 7)
target2 <- c(8, 6, 9, 5, 10, 7, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30)
group <- c("Epithelial", "Epithelial", "Endothelial", "Stroma", "Endothelial", "Immune", 
           "Immune", "Proliferating", "Stroma", "Fibroblast lineage", "Fibroblast lineage", 
           "Alveolar epithelium", "Alveolar epithelium", "Lymphoid", "Airway epithelium", 
           "CEC1", "Airway epithelium", "Airway epithelium", "LEC1", "Myeloid1", "Myeloid1", 
           "Myeloid1", "Myeloid1", "Proliferating2", "SM1", "Lymphoid")


flows <- data.frame(source, target, value, source2, target2, group)
nodes <- data.frame(name = unique(c(flows$source, flows$target)))

colourScale <- JS(
'd3.scaleOrdinal()
    .domain(["Epithelial", "Endothelial", "Stroma", "Immune", 
             "Alveolar epithelium", "Airway epithelium",
             "AT1", "AT2", "Basal", "Ciliated",
             "Fibroblast lineage", "SM1",
             "Alf1", "Alf2", "SM2",
             "CEC1", "LEC1",
             "CEC2", "LEC2",
             "Myeloid1", "Lymphoid",
             "B", "Macrophages", "MAST", "Monocytes", "Myeloid2", "T_cell",
             "Proliferating", "Proliferating2", "Proliferating3"])
    .range(["#3a86ff", "#ffbe0b", "#8338ec", "#d62828", 
            "#0081a7", "#0081a7",
            "#a8dadc", "#a8dadc", "#a8dadc", "#a8dadc",
            "#9d4edd", "#9d4edd",
            "#cdb4db", "#cdb4db", "#cdb4db",
            "#ffffb3", "#ffffb3",
            "#ffffb3", "#ffffb3",
            "#e76f51", "#e76f51",
            "#f7a072", "#f7a072", "#f7a072", "#f7a072", "#f7a072", "#f7a072",
            "#a98467", "#a98467", "#a98467"])'
)

sankeyNetwork(
  Links = flows, 
  Nodes = nodes,
  Source = "source2", 
  Target = "target2", 
  Value = "value", 
  NodeID = "name", 
  units = "T",
  fontSize = 12, 
  nodeWidth = 30,
  LinkGroup = "group",       # link group
  # NodeGroup = "target",       # node group
  colourScale = colourScale  # set color scale
)

现在生成的桑基图颜色和我设定的细胞类型完全对应不上,有没有大佬能帮我看看问题出在哪,怎么调整才能让颜色和细胞类型正确匹配上呢?

备注:内容来源于stack exchange,提问作者Fugwaaaa

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.15 09:19:51