【问题标题】:how to fix incorrect color for nodes in networkD3 (sankeyNetwork)?如何修复networkD3(sankeyNetwork)中节点的不正确颜色?
【发布时间】:2021-09-24 14:41:17
【问题描述】:

您好,感谢您阅读我 我正在使用带有 networkD3 包的 Sankey 图,并尝试自定义每个节点的颜色,但我得到了一整列具有相同颜色的“源”,我想用特定的调色板获得不同的颜色。任何人都知道我该如何解决它?我使用的代码如下:

library(networkD3)
data_long <-data.frame(
  stringsAsFactors = FALSE,
  source = c("Objetivo 1",
             "Objetivo 1","Objetivo 2","Objetivo 3",
             "Objetivo 4","Objetivo 1","Objetivo 2",
             "Objetivo 3","Objetivo 4",
             "Objetivo 1","Objetivo 2","Objetivo 3",
             "Objetivo 4","Objetivo 1",
             "Objetivo 2","Objetivo 3","Objetivo 4",
             "Objetivo 1","Objetivo 4",
             "Objetivo 4"),
  target = c("ODS 12: Producción y consumo responsables",
             "ODS 17: Alianzas para lograr los objetivos",
             "ODS 10: Reducción de las desigualdades",
             "ODS 10: Reducción de las desigualdades",
             "ODS 17: Alianzas para lograr los objetivos","ODS 3: Salud y bienestar",
             "ODS 12: Producción y consumo responsables",
             "ODS 12: Producción y consumo responsables",
             "ODS 9: Industria, innovación e infraestructura",
             "ODS 8: Trabajo decente y crecimiento económico",
             "ODS 8: Trabajo decente y crecimiento económico",
             "ODS 8: Trabajo decente y crecimiento económico",
             "ODS 8: Trabajo decente y crecimiento económico",
             "ODS 9: Industria, innovación e infraestructura",
             "ODS 9: Industria, innovación e infraestructura",
             "ODS 9: Industria, innovación e infraestructura",
             "ODS 9: Industria, innovación e infraestructura",
             "ODS 5: Igualdad de género",
             "ODS 10: Reducción de las desigualdades",
             "ODS 12: Producción y consumo responsables"),
  value = c(2L,1L,3L,1L,2L,1L,
            3L,1L,1L,19L,1L,11L,13L,15L,
            8L,4L,4L,1L,1L,1L)
)


my_color <- 'd3.scaleOrdinal() .domain(["Objetivo 1", "Objetivo 2","Objetivo 3", "Objetivo 4","ODS 3: Salud y bienestar", "ODS 5: Igualdad de género", "ODS 9: Industria, innovación e infraestructura", "ODS 10: Reducción de las desigualdades", "ODS 12: Producción y consumo responsables", "ODS 17: Alianzas para lograr los objetivos"]) 
.range(["#12A09A", "#1E5C4F" , "#941B80", "#F19100", "#4C9F38", "#FF3A21", "#FD6925", "#DD1367", "#BF8B2E", "purple", "#001B8C"])'

nodes <- data.frame(name=c(as.character(data_long$source), as.character(data_long$target)) %>% unique())


data_long$IDsource=match(data_long$source, nodes$name)-1 
data_long$IDtarget=match(data_long$target, nodes$name)-1

sankeyNetwork(Links = data_long, Nodes = nodes,
              Source = "IDsource", Target = "IDtarget",
              Value = "value", NodeID = "name", 
              sinksRight=FALSE, colourScale=my_color, 
              nodeWidth=40, fontSize=13, nodePadding=20)

【问题讨论】:

    标签: r d3.js htmlwidgets networkd3


    【解决方案1】:

    除非明确指定,否则节点组由节点名称推断,并且节点组名称中不能有空格。

    您可以像这样向节点数据框添加节点组列

    nodes$node_group <- gsub(" ", "_", nodes$name)
    

    然后将该列指定为具有sankeyNetwork() 参数NodeGroup = "node_group" 的节点组

    大家一起...

    library(networkD3)
    library(dplyr)
    
    data_long <-data.frame(
      stringsAsFactors = FALSE,
      source = c("Objetivo 1",
                 "Objetivo 1","Objetivo 2","Objetivo 3",
                 "Objetivo 4","Objetivo 1","Objetivo 2",
                 "Objetivo 3","Objetivo 4",
                 "Objetivo 1","Objetivo 2","Objetivo 3",
                 "Objetivo 4","Objetivo 1",
                 "Objetivo 2","Objetivo 3","Objetivo 4",
                 "Objetivo 1","Objetivo 4",
                 "Objetivo 4"),
      target = c("ODS 12: Producción y consumo responsables",
                 "ODS 17: Alianzas para lograr los objetivos",
                 "ODS 10: Reducción de las desigualdades",
                 "ODS 10: Reducción de las desigualdades",
                 "ODS 17: Alianzas para lograr los objetivos","ODS 3: Salud y bienestar",
                 "ODS 12: Producción y consumo responsables",
                 "ODS 12: Producción y consumo responsables",
                 "ODS 9: Industria, innovación e infraestructura",
                 "ODS 8: Trabajo decente y crecimiento económico",
                 "ODS 8: Trabajo decente y crecimiento económico",
                 "ODS 8: Trabajo decente y crecimiento económico",
                 "ODS 8: Trabajo decente y crecimiento económico",
                 "ODS 9: Industria, innovación e infraestructura",
                 "ODS 9: Industria, innovación e infraestructura",
                 "ODS 9: Industria, innovación e infraestructura",
                 "ODS 9: Industria, innovación e infraestructura",
                 "ODS 5: Igualdad de género",
                 "ODS 10: Reducción de las desigualdades",
                 "ODS 12: Producción y consumo responsables"),
      value = c(2L,1L,3L,1L,2L,1L,
                3L,1L,1L,19L,1L,11L,13L,15L,
                8L,4L,4L,1L,1L,1L)
    )
    
    
    my_color <- 'd3.scaleOrdinal().domain(["Objetivo 1", "Objetivo 2","Objetivo 3", "Objetivo 4","ODS 3: Salud y bienestar", "ODS 5: Igualdad de género", "ODS 9: Industria, innovación e infraestructura", "ODS 10: Reducción de las desigualdades", "ODS 12: Producción y consumo responsables", "ODS 17: Alianzas para lograr los objetivos"]) 
    .range(["#12A09A", "#1E5C4F" , "#941B80", "#F19100", "#4C9F38", "#FF3A21", "#FD6925", "#DD1367", "#BF8B2E", "purple", "#001B8C"])'
    
    nodes <- data.frame(name=c(as.character(data_long$source), as.character(data_long$target)) %>% unique())
    
    
    data_long$IDsource=match(data_long$source, nodes$name)-1 
    data_long$IDtarget=match(data_long$target, nodes$name)-1
    
    nodes$node_group <- gsub(" ", "_", nodes$name)
    
    
    sankeyNetwork(Links = data_long, Nodes = nodes,
                  Source = "IDsource", Target = "IDtarget",
                  Value = "value", NodeID = "name", NodeGroup = "node_group",
                  sinksRight=FALSE, colourScale=my_color, 
                  nodeWidth=40, fontSize=13, nodePadding=20)
    

    【讨论】:

      猜你喜欢
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 2018-12-11
      • 1970-01-01
      相关资源
      最近更新 更多