Closed Jorge-hercas closed 2 years ago
I answered this on StackOverflow here: https://stackoverflow.com/questions/69316891/how-to-fix-incorrect-color-for-nodes-in-networkd3-sankeynetwork
Basically, the node labels used to connect the colors cannot have a space in them, so alternatively one can create a new column in the nodes data frame and specify that with the NodeGroup
argument...
for example...
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)
Hi and thanks for reading me Im workin with a Sankey plot with the networkD3 package and im trying to customize the colors for each node, but im getting an entire column of "source" with the same color and I want to get diferent colors with an specific palette. Anyone know's how I can fix it? The code im using is the following: