Patrick Meyfroidt
Patrick Meyfroidt

Reputation:

Colors randomly assigned in R riverplot Sankey graph

I found the R riverplot package very handy to make Sankey/Minard charts. The output chart is great, including the position of the nodes and the width of the edges.

But, I have a problem with the colors. I assigned colors through a "col" column in the nodes, but the output colors do not match at all what I indicated. I tried experimentally to remove all colors, assign a single color at a time, then add a second, and so on, but I've been unable to find any logic in the erroneous assignments. It just seems completely random, even adding colors that I are not part of the specified list.

For my ease of handling, I loaded the nodes and edges as two separate files.

Below is my reproducible example:

#######################
R CODE: 
#######################
library(riverplot)

ruk_sankey_edges <- read.table("/.../ruk_sankey_edges.csv", header = TRUE, na.strings = "''", sep = ";",  dec=".")

ruk_sankey_nodes <- read.table("/.../ruk_sankey_nodes.csv", header = TRUE, na.strings = "''", sep = ";",  dec=".")

nodes <- ruk_sankey_nodes

edges <- ruk_sankey_edges

colnames( nodes ) <- c( "ID", "x", "y", "col")

colnames( edges ) <- c( "ID", "N1", "N2", "Value")

river <- makeRiver( nodes, edges, node_labels = NULL, node_xpos = nodes$x, node_ypos = nodes$y)

style <- list(col = nodes$col )

riverplot(river, lty = 0, default_style = style, srt = 0, 
node_margin = 0.1, nodewidth = 1, plot_area = 0.8, nsteps = 50,
add_mid_points = NULL, yscale = "auto")

#######################
AND THE DATA FILES:

#######################
ruk_sankey_nodes.csv : 

ID;X;Y;col

A1;5;70;gray

A2;10;90;red

A3;10;65;gray

A4;20;85;gray

A5;30;105;green

A6;30;95;cyan

A7;30;85;mangenta

A8;30;75;yellow

A9;20;45;gray

A10;30;60;blue

A11;30;40;black

#######################
ruk_sankey_edges.csv : 

ID;ID1;ID2;Value

E1;A1;A3;39159

E2;A1;A2;8200

E3;A4;A8;2942

E4;A4;A7;1608

E5;A4;A6;3039

E6;A4;A5;3897

E7;A3;A9;27673

E8;A3;A4;11486

E9;A9;A11;22235

E10;A9;A10;5438

#######################

Does anyone have a suggestion? Or is able to obtain the indicated colors?

Thank you very much,

Patrick

Upvotes: 1

Views: 937

Answers (2)

Schnabeltier
Schnabeltier

Reputation: 188

alternatively use

stringsAsFactors= FALSE

in your 'nodes' data frame. This is proposed in the Reference Manual: see the examples on page 7 for 'makeriver()' styles.

Upvotes: 2

lawyeR
lawyeR

Reputation: 7654

In your nodes data frame (as in nodes.df below), convert the col variable, which is probably a factor, to character. riverplot() gets confused!

nodes.df$col <- as.character(nodes.df$col)

Upvotes: 3

Related Questions