Assign gradient colors based on a column to nodes of collapsibletree in r

210 Views Asked by At

I am trying to create a collapsibletree object using the library(collapsibleTree) and library(dplyr) packages. Following this example, I tried to assign colors to my tree but I am not getting anything.

A few rows of the dataframe are provided below. the Failure column is categorical and has three options of c("Bar fracture", "Bar pullout", "Coupler failure"). I have created a new column named Color and assigned a value of 1 to each row that indicated the Failure type is Bar fracture and 0 to each row that says otherwise (the two other categories).

> dput(Mostafa[1:27,])
structure(list(`Coupler Type` = c("Headed Bar Coupler", "Headed Bar Coupler", 
"Headed Bar Coupler", "Headed Bar Coupler", "Headed Bar Coupler", 
"Headed Bar Coupler", "Headed Bar Coupler", "Headed Bar Coupler", 
"Headed Bar Coupler", "Grouted Sleeve Coupler", "Grouted Sleeve Coupler", 
"Grouted Sleeve Coupler", "Grouted Sleeve Coupler", "Grouted Sleeve Coupler", 
"Grouted Sleeve Coupler", "Grouted Sleeve Coupler", "Grouted Sleeve Coupler", 
"Grouted Sleeve Coupler", "Grouted Sleeve Coupler", "Grouted Sleeve Coupler",
"Grouted Sleeve Coupler", "Grouted Sleeve Coupler", "Grouted Sleeve Coupler", 
"Grouted Sleeve Coupler", "Grouted Sleeve Coupler", "Grouted Sleeve Coupler", 
"Grouted Sleeve Coupler"), `Coupler Manufacturer` = c("Headed Reinforcement Corp.", 
"Headed Reinforcement Corp.", "Headed Reinforcement Corp.", "Headed Reinforcement Corp.", 
"Headed Reinforcement Corp.", "Headed Reinforcement Corp.", "Headed Reinforcement Corp.", 
"Headed Reinforcement Corp.", "Headed Reinforcement Corp.", "Dayton Superior", 
"Dayton Superior", "Dayton Superior", "Dayton Superior", "Dayton Superior", 
"Dayton Superior", "Dayton Superior", "Dayton Superior", "Dayton Superior", 
"Splice Sleeve North America", "Splice Sleeve North America", 
"Splice Sleeve North America", "Splice Sleeve North America", 
"Splice Sleeve North America", "Splice Sleeve North America", 
"Splice Sleeve North America", "Splice Sleeve North America", 
"Splice Sleeve North America"), `Coupler Model` = c("Xtender® 500/510 Standard Coupler", 
"Xtender® 500/510 Standard Coupler", "Xtender® 500/510 Standard Coupler", 
"Xtender® 500/510 Standard Coupler", "Xtender® 500/510 Standard Coupler", 
"Xtender® 500/510 Standard Coupler", "Xtender® 500/510 Standard Coupler", 
"Xtender® 500/510 Standard Coupler", "Xtender® 500/510 Standard Coupler", 
"D410 Sleeve-Lock® Grout Sleeve", "D410 Sleeve-Lock® Grout Sleeve", 
"D410 Sleeve-Lock® Grout Sleeve", "D410 Sleeve-Lock® Grout Sleeve", 
"D410 Sleeve-Lock® Grout Sleeve", "D410 Sleeve-Lock® Grout Sleeve", 
"D410 Sleeve-Lock® Grout Sleeve", "D410 Sleeve-Lock® Grout Sleeve", 
"D410 Sleeve-Lock® Grout Sleeve", "NMB", "NMB", "NMB", "NMB", 
"NMB", "NMB", "NMB", "NMB", "NMB"), `Bar Size` = c("No. 5", "No. 5", 
"No. 5", "No. 8", "No. 8", "No. 8", "No. 11", "No. 11", "No. 11", 
"No. 5", "No. 5", "No. 5", "No. 8", "No. 8", "No. 8", "No. 11", 
"No. 11", "No. 11", "No. 5", "No. 5", "No. 5", "No. 8", "No. 8", 
"No. 8", "No. 11", "No. 11", "No. 11"), Failure = c("Bar Fracture", 
"Bar Fracture", "Bar Fracture", "Bar Fracture", "Bar Fracture", 
"Bar Fracture", "Bar Fracture", "Bar Fracture", "Bar Fracture", 
"Bar Fracture", "Bar Pullout", "Bar Pullout", "Bar Fracture", 
"Bar Fracture", "Bar Fracture", "Bar Fracture", "Bar Fracture", 
"Bar Fracture", "Bar Pullout", "Bar Pullout", "Bar Pullout", 
"Bar Fracture", "Bar Fracture", "Bar Fracture", "Bar Fracture", 
"Bar Fracture", "Bar Fracture"), Color = c(1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1
)), row.names = c(NA, -27L), class = c("tbl_df", "tbl", "data.frame"
))

I would like to have the nodes colored depending on the summarized number in the Color column. The current code is:

  collapsibleTreeSummary(
      Mostafa,
      hierarchy = c("Coupler Type", "Coupler Manufacturer", "Bar Size", "Failure"),
      root = "Mechanical Couplers",
      width = 800,
      attribute = "Color",
      zoomable = FALSE,
      collapsed = FALSE
      )

The results I get is :

enter image description here

As you can see, the colors are only assigned to the Failure level and are not carried forward.

For example, for the first level Headed Bar Coupler that has all the Failure types as Bar fracture all the nodes falling under level Headed Bar Coupler should be green (same color since all the Failures were of the wanted type. But for example for first level Grouted Sleeve Coupler, there are some rows with zero. Thus, I want to assign red to the Failure nodes that indicate value 0. All the levels in between should be assigned colors by the gradient.

Desired output is shown below:

enter image description here

0

There are 0 best solutions below