mages / googleVis

Interface between R and the Google Chart Tools
https://mages.github.io/googleVis/
358 stars 156 forks source link

Sankey color individual links #69

Open WHMan opened 6 years ago

WHMan commented 6 years ago

Hi there,

Suppose I have these data:

df <- data.frame(From=c(rep("A",3), rep("B", 3)),
                 To=c(rep(c("X", "Y", "Z"),2)),
                 Weight=c(5,7,6,2,9,4))
df <- rbind(cbind(df, group = "One"), 
            cbind(df, group = "Two"))
df_plotdata <- df[, 1:3]

plot( gvisSankey(df_plotdata, from="From", to="To", weight="Weight"))

Is it possible to color the links by group (e.g. group One red and group Two blue).

Thanks so much in advance!

mages commented 6 years ago

I think you can. The Google documentation has more details: https://developers.google.com/chart/interactive/docs/gallery/sankey

Here is an example with your data:

library(googleVis)
df <- data.frame(From=c(rep("A",3), rep("B", 3)),
                 To=c(rep(c("X", "Y", "Z"),2)),
                 Weight=c(5,7,6,2,9,4))
df <- rbind(cbind(df, group = "One"), 
            cbind(df, group = "Two"))
df_plotdata <- df[, 1:3]

plot( gvisSankey(df_plotdata, 
                 from="From", to="To", 
                 weight="Weight",
                 options = list(
                   sankey = "{
                     link: { color: { fill: ['#a6cee3', '#b2df8a'] } },
                   node: { colors: [ '#a61d4c', '#cab2d6', '#ffff99' ],
                          label: { color: '#871b47' } }}"
                 )
                 ))
WHMan commented 6 years ago

Hi @mages,

Thanks for your superswift reply. Unfortunately, when I run your code, all links remain grey. I've tried to open the plot in different browsers, but that didn't change anything.

I'm using googleVis_0.6.2 under R version 3.3.2.

Cheers

mages commented 6 years ago

Perhaps, something like this:

library(googleVis)
df <- data.frame(From=c(rep("A",3), rep("B", 3)),
                 To=c(rep(c("X", "Y", "Z"),2)),
                 Weight=c(5,7,6,2,9,4))
df <- rbind(cbind(df, group = "One"), 
            cbind(df, group = "Two"))
df_plotdata <- df[, 1:3]

plot( gvisSankey(df_plotdata, 
                 from="From", to="To", 
                 weight="Weight",
                 options = list(
                   sankey = "{
                     link: { color:  ['#a6cee3', '#b2df8a']  ,
                             colorMode: 'gradient' },
                   node: { colors: [ '#a61d4c', '#cab2d6', '#ffff99' ],
                          label: { color: '#871b47' } }
                   }"
                 )
                 ))
WHMan commented 6 years ago

Hi @mages,

Thanks again for the effort you put in answering my question. Unfortunately, your last solution did not produce the graph I had in mind. I'm sorry if I was not clear in my first post, but this (very ugly) figure depicts what I wished to plot:

capture

I tried manipulating several configuration options as described in the link you provided. I have the feeling that the options of sankey.link.colorMode make it impossible to plot the above graph as neither source, target or gradient are the options that I want. Also, the option none can't be combined with an array in sankey.link.color.fill. A workaround could be splitting up each node (e.g. splitting node A into A_One and A_Two) and manipulating the sankey.node.nodePadding. However, it seems like the latter only accepts a single value.

I'll try do dive into the options more. Thanks again!

Bustami commented 6 years ago

Hey @WHMan, please tell me any new or improvement about that issue. I've searching a solution or alternative for split colors, without success.

WHMan commented 6 years ago

Ultimately, I did not manage to visualize it the way I had in mind. So I can't help you any further, @Bustami....