I am working on a project where I need to visualize the flow of healthcare providers across different phases of a care pathway using a Sankey diagram in R. Each phase represents a step in the care process, and various providers are involved in each phase.
I would like to ensure that each provider is consistently colored throughout the diagram. That means if a provider appears in multiple phases (both as a source and a target), it should have the same color across all instances.
I have already managed to create a Sankey diagram using networkD3::sankeyNetwork, and I can assign colors, but I haven’t figured out how to make sure that the same provider gets the same color in every phase of the care process.
Here’s a simplified version of the code I am working with:
library(dplyr)
library(readxl)
# Load data
data <- read_excel("healthcare_data.xlsx", sheet = "Sheet1")
# Create nodes and links
providers <- unique(c(data$Phase_Provider, data$Next_Phase_Provider))
nodes <- data.frame(name = providers)
links <- data %>%
mutate(source = match(Phase_Provider, nodes$name) - 1,
target = match(Next_Phase_Provider, nodes$name) - 1) %>%
group_by(source, target) %>%
summarise(value = n()) %>%
ungroup()
# Assigning the same color to all nodes for now
nodes$color <- "#1f77b4"
# Create the Sankey diagram
sankeyNetwork(Links = links, Nodes = nodes, Source = "source", Target = "target",
Value = "value", NodeID = "name", fontSize = 12, nodeWidth = 30)
What I want to achieve:
- Each healthcare provider (identified by Phase_Provider or
Next_Phase_Provider) should have the same color in all phases (nodes)
where they appear. - The color assignment should be based on the
provider’s name, so every instance of the same provider across
different phases will always appear in the same color.
Questions:
- How can I assign colors to nodes based on the provider’s name and ensure that the same provider is colored consistently across different phases?
- Is there a way to map a unique color to each provider using d3.scaleOrdinal() or another method in R?
Any help or guidance on how to achieve this would be greatly appreciated!