簡體   English   中英

為什么這種顏色在 Sankey R 圖中不正確?

[英]Why is this color incorrect in Sankey R plot?

下面的代碼生成一個 Sankey 圖。 但是,正如您在圖 1 中看到的那樣,“No Change”框是橙色的(“#eb6841”),而不是黃色的(“Yellow”)。 這似乎與代碼不符,我在代碼中指定了黃色作為我想要的顏色。 有誰知道為什么它是橙色而不是黃色的? 看起來左邊的框顏色並不總是與線條顏色相同,這也是錯誤的。 任何幫助表示贊賞。 謝謝。

代碼:

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

圖1:

在此處輸入圖像描述

組名中不能有空格。 它只會考慮第一個空格之前的文本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM