【问题标题】:Assigning node and link colors in R googleVis sankey chart在 R googleVis sankey 图中分配节点和链接颜色
【发布时间】:2016-06-02 21:44:56
【问题描述】:

拿起这个question,我正在尝试为节点分配一组颜色,并希望在使用 R 中的 GoogleVis 包的 Sankey 图表中与渐变模式链接。问题是我有相同的3 组节点中的每一个中的类别,我无法让它合作。

datSK <- data.frame(From=c(rep("A1",3), rep("B1", 3), rep("C1", 3), rep("A2", 3), rep("B2", 3), rep("C2",3)), 
                To=c(rep(c("A2", "B2", "C2"), 3), rep(c("A3", "B3", "C3"), 3)),
                Weight=c(5,7,6,2,9,4,3,4,5))

我希望出现在图表 3 个不同部分的节点 A、B、C 具有相同的颜色(分别为蓝色、橙色、绿色)。

plot(gvisSankey(datSK, from="From", 
       to="To", weight="Weight",
       options=list(sankey="{
                    link: { colorMode: 'gradient', colors: ['blue', 'orange', 'green']}, 
                    node: { colors: ['blue', 'orange', 'green']}}")))

很遗憾,我无法弄清楚颜色是如何分配的。

【问题讨论】:

  • 我也想知道这个问题的答案!

标签: r googlevis sankey-diagram


【解决方案1】:

已经一年了,不知道你是否还需要答案,但这是我发现的:

plot(gvisSankey(datSK, from="From", 
       to="To", weight="Weight",
       options=list(sankey="{
                    link: { colorMode: 'gradient'}, 
                    node: { colors: ['blue', 'blue', 'orange',
                                     'green','orange', 'green',
                                     'blue','orange','green']}
                             }")))

Google 的 Sankey Chart 将根据节点的出现顺序分配颜色。这是我决定节点出现顺序的方法。基本上,我创建一个节点连接列表的字符串,拆分它们,提取唯一节点,然后分配颜色。

# Create a stringlist of node pairs
nodestringlist <- paste(datSK$From,datSK$To, collapse=' ')

# Split them up
nodestringvector <- strsplit(nodestringlist, split =' ')

# Find the unique nodes in order they appear
node_order <- unique(nodestringvector[[1]])
#output: "A1" "A2" "B2" "C2" "B1" "C1" "A3" "B3" "C3"

这是你想要的吗?

【讨论】:

  • 我现在不记得了。 :) 但看起来@svenhalvorson 也需要一个答案,所以希望它能帮助他。另外,我认为最终将通过ggforce 提供 ggplot 版本的桑基图。
  • 感谢您标记我的答案。 ggplot 的 sankey 听起来是个好主意。我也去看看。
猜你喜欢
  • 1970-01-01
  • 2015-04-14
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
  • 1970-01-01
  • 2022-11-02
  • 2022-10-18
  • 1970-01-01
相关资源
最近更新 更多