简体   繁体   中英

Why is this color incorrect in Sankey R plot?

The code below generates a Sankey diagram. However, as you can see in Figure 1, the "No Change" box is orange ("#eb6841"), not yellow ("Yellow"). This does not appear to correspond to the code, where I specified yellow as the colour I wanted. Does anyone know why it is orange and not yellow? It also looks like the box colour on the left is not always the same as the line colour, which is also wrong. Any help appreciated. Thanks.

Code:

library(networkD3)

## create a dataframe with 12 nodes
nodes = data.frame("name" = c("Demographics",
                               "Inequality",
                               "Productivity",
                               "Urbanisation",
                               "Balance of Payments",
                               "Consumption",
                               "Debt",
                               "FDI",
                               "Trade Deals",
                               "Currency",
                               "Positive",
                               "No Change",
                               "Negative"))

## create edges with weights
links = as.data.frame(matrix(c(0, 12, 2,
                                1, 11, 2,
                                2, 10, 2,
                                3, 10, 2,
                                4, 11, 2,
                                5, 10, 2,
                                6, 12, 2,
                                7, 11, 2,
                                8, 10, 2,
                                9, 11, 2
                               ), byrow = TRUE, ncol = 3))

## set column names for links
names(links) = c("source", "target", "value")

## add edge types for coloring purpose

links$group <- c("group_1",
                 "group_1",
                 "group_2",
                 "group_2",
                 "group_3",
                 "group_3",
                 "group_4",
                 "group_4",
                 "group_5",
                 "group_5")

## Create custom color list using d3 for each node
node_color <- 'd3.scaleOrdinal() .domain(["Demographics",
                               "Inequality",
                               "Productivity",
                               "Urbanisation",
                               "Balance of Payments",
                               "Consumption",
                               "Debt",
                               "FDI",
                               "Trade Deals",
                               "Currency",
                               "Positive",
                               "No Change",
                               "Negative",
                              "group_1",
                               "group_2",
                               "group_3",
                               "group_4",
                 "group_5"]) .range(["#edc951", "#edc951", "#eb6841", "#eb6841", "#cc2a36", "#cc2a36", "#4f372d", "#4f372d", "#00a0b0", "#00a0b0", "Green", "Yellow", "Red", "#edc951", "#eb6841", "#cc2a36", "#4f372d", "#00a0b0"])'

## Draw Sankey Diagram

p = sankeyNetwork(Links = links, Nodes = nodes,
 Source = "source", Target = "target",
 Value = "value", NodeID = "name",
 fontSize = 14, nodeWidth = 40,
 colourScale = node_color,
 LinkGroup = "group")

p

Figure 1:

在此处输入图像描述

The group names can not have spaces in them. It will only consider the text before the first space.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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