简体   繁体   English

在R riverplot Sankey图中随机分配的颜色

[英]Colors randomly assigned in R riverplot Sankey graph

I found the R riverplot package very handy to make Sankey/Minard charts. 我发现R riverplot软件包非常适合制作Sankey / Minard图表。 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. 我通过节点中的“ col”列分配颜色,但是输出颜色与我所指示的完全不匹配。 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 帕特里克

In your nodes data frame (as in nodes.df below), convert the col variable, which is probably a factor, to character. 在节点数据框中(如下面的nodes.df所示),将col变量(可能是一个因素)转换为字符。 riverplot () gets confused! riverplot ()感到困惑!

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

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. 这是在参考手册中提出的:有关“ makeriver()”样式的信息,请参见第7页的示例。

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

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