AdeelK93 / collapsibleTree

Create Interactive Collapsible Tree Diagrams in R using D3.js
https://adeelk93.github.io/collapsibleTree/
158 stars 41 forks source link

Including colors based on a particular node #76

Open Cami1992 opened 5 months ago

Cami1992 commented 5 months ago

Data sample:

data <- data.frame( Outcome = rep(c("Outcome A", "Outcome B", "Outcome C"), each = 6), Output = rep(c("Output 1", "Output 2", "Output 3"), times = 6), Project = paste0("Project ", 1:18), Expenditure = runif(18, 100, 1000) # Random expenditure values )

I want to display particular colors based on Outcome (i.e. in total only three colors). But I just got blanks after running the codes:

1° Attempt: collapsibleTree( data, hierarchy = c("Outcome", "Output", "Project"), width_scale = "sqrt", nodeSize = "Expenditure", circleFill = c("lightblue", "lightgreen", "lightyellow"), # Custom colors tooltipLabels = c("Outcome", "Output", "Project", "Expenditure") )

2° Attempt:

outcome_colors <- c("Outcome A" = "#FF5733", # Color for Outcome1 "Outcome B" = "#33FF57", # Color for Outcome2 "Outcome C" = "#5733FF") # Color for Outcome3

Map colors to outcomes in the data frame

mexico_finance$color <- outcome_colors[mexico_finance$Outcome] mexico_finance$color<-as.factor(mexico_finance$color)

Use the color palette in your collapsibleTree function

collapsibleTree( mexico_finance, hierarchy = c("Outcome", "Output","Project", "Expenditure"), nodeSize = "Expenditure", fill = "color", # Use the outcome variable for color tooltipLabels = c("Outcome", "Output", "Project", "Expenditure"), width_scale = "sqrt" )