简体   繁体   English

在R googleVis sankey图表中指定节点和链接颜色

[英]Assigning node and link colors in R googleVis sankey chart

Picking up on this question , I'm trying to assign a set of colours to nodes, and hopefully links with the gradient mode, in a Sankey chart using GoogleVis package in R. The issue is that I have the same categories in each of the 3 sets of nodes, and I'm having trouble getting it to cooperate. 提出这个问题 ,我正在尝试为节点分配一组颜色,并希望在使用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))

I want nodes A, B, C, which appear in 3 different parts of the chart to have the same colors (respectively blue, orange, green). 我希望节点A,B,C出现在图表的3个不同部分,以具有相同的颜色(分别为蓝色,橙色,绿色)。

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

Unfortunately, I can't figure out how the colours are being assigned. 不幸的是,我无法弄清楚如何分配颜色。

It has been a year, I do not know whether you still need the answer or not but this is what I found: 这是一年,我不知道你是否仍然需要答案,但这是我发现的:

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's Sankey Chart will assign the color based on the appearance order of the nodes. Google的Sankey Chart将根据节点的外观顺序指定颜色。 Here is how I decide the appearance order of the node. 这是我如何决定节点的外观顺序。 Basically I create a string of a list of node connections, split them, and extract the unique nodes, then assign the colors. 基本上我创建一个节点连接列表的字符串,拆分它们,然后提取唯一的节点,然后分配颜色。

# 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"

Is this what you want? 这是你想要的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM