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.
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).
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. 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?