Assigning colors to nodes and links in a Sankey chart using GoogleVis package in R

1,504 views
Skip to first unread message

Phil Straforelli

unread,
Jun 2, 2016, 8:19:56 PM6/2/16
to Google Visualization API
I have 3 categories (A, B, C) that repeat 3 times (A1, A2, A3, for A, and so forth). I'd like each of those 3 categories to have the same colour, so A is always blue across the 3 times it appears, B is always orange, C is always green. I'd also like that done with the links, with some gradient as it goes from one node to the next. Here's some R code to replicate what I'm getting:

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)) library(GoogleVis)


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

The links and their gradients appear to work fine, but I can't figure out the logic behind the assignment of the colors for the nodes and links. Can it be explained so that I can determine how to apply the colours as described above?

Daniel LaLiberte

unread,
Jun 3, 2016, 9:34:28 AM6/3/16
to Google Visualization API
Phil,

The order of the nodes in the layout is somewhat unpredictable, depending on the number of iterations that you allow and the complexity of the network.  

But the order of the nodes as they appear in the datatable is, I believe, used to determine which color is assigned to each node, independent of the eventual layout of the nodes.  It appears the From nodes are assigned a color separately from the To nodes, and each cycles through the colors in order of first appearance.

Something similar might occur for links, with each row corresponding to a link.  The colors specified for links are then used cyclically in that same order.   But additionally, the link color can be assigned using a 'style' role.  

This is somewhat of a guess, and based on minimal experimentation, so I hope I am not misleading you.


--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-visualizati...@googlegroups.com.
To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-visualization-api.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-visualization-api/e6a52278-35a8-4687-8349-f3f57e871054%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Reply all
Reply to author
Forward
0 new messages