'R: Plotly: Treemap: Color only lowest labels

By plotting a treemap, I added a color code (colorway) in order to have all main parents having the same color scheme. I would now recolor only the minor childs, such as

  • all starting with 1 having the same color
  • all starting with 2 having the same color
  • etc.

Any ideas how I could manage this?

  fig <- plot_ly(
    type = "treemap",
    labels = df$label,
    parents = df$parent,
    values = df$value / 1000,
    branchvalues = "total",
    texttemplate = "%{label}: %{value:.1f} TWh",
    textinfo = "label+value+percent parent+percent"
  ) %>%
    layout(treemapcolorway = c("#FF0000", "#00A79F", "#413D3A", "B51F1F", "#007480", "#CAC7C7"))

enter image description here

"","label","parent","value"
"1","PV","Electricity techs",71831.99712672
"7","Geothermal","Electricity techs",6096.96
"12","DEC HP Elec","HLT techs",96989.0528080645
"13","Efficiency","Infr. techs",87.6
"15","Grid","Infr. techs",87.6
"19","1 DEC HP Elec","Cp DEC HP Elec",7517.69235287671
"23","5 DEC HP Elec","Cp DEC HP Elec",717.919232876712
"24","6 DEC HP Elec","Cp DEC HP Elec",694.760547945206
"25","7 DEC HP Elec","Cp DEC HP Elec",717.919232876712
"26","8 DEC HP Elec","Cp DEC HP Elec",717.919232876712
"27","9 DEC HP Elec","Cp DEC HP Elec",1198.57438294521
"28","10 DEC HP Elec","Cp DEC HP Elec",3795.63912287671
"30","12 DEC HP Elec","Cp DEC HP Elec",8237.42640287671
"68","2 Efficiency","Cp Efficiency",6.72
"69","3 Efficiency","Cp Efficiency",7.44
"70","4 Efficiency","Cp Efficiency",7.2
"71","5 Efficiency","Cp Efficiency",7.44
"72","6 Efficiency","Cp Efficiency",7.2
"78","12 Efficiency","Cp Efficiency",7.44
"79","1 Geothermal","Cp Geothermal",517.824
"82","4 Geothermal","Cp Geothermal",501.12
"83","5 Geothermal","Cp Geothermal",517.824
"84","6 Geothermal","Cp Geothermal",501.12
"85","7 Geothermal","Cp Geothermal",7.44
"97","7 Grid","Cp Grid",7.44
"98","8 Grid","Cp Grid",7.44
"99","9 Grid","Cp Grid",7.2
"100","10 Grid","Cp Grid",7.44
"101","11 Grid","Cp Grid",7.2
"102","12 Grid","Cp Grid",7.44
"211","1 PV","Cp PV",1971.599921136
"212","2 PV","Cp PV",3158.399873664
"216","6 PV","Cp PV",5651.99977392
"217","7 PV","Cp PV",6100.799755968
"235","Electricity techs","",146522.22744672
"236","HHT techs","",35925.672
"237","HLT techs","",140743.512721477
"238","Infr. techs","",31337.36116834
"239","Cp DEC HP Elec","DEC HP Elec",42725.97
"243","Cp Efficiency","Efficiency",87.6
"244","Cp Geothermal","Geothermal",5243.3856
"245","Cp Grid","Grid",87.6
"255","Cp PV","PV",49553.99801784


Solution 1:[1]

This works for coloring by number, but I doubt this is what you would stick with. (Although this was asked a long time ago, so it's probably not all that useful now!)

I created a color array to use first. Then I looked for the values in the labels that you wanted to color by. For any label that doesn't include a number, I made it light gray.

library(shades) 
library(plotly)

swatch(gradient(c("#003b70", "#b21e28"), 12)) # check out colors
# make color scale for numbered labels
col <- gradient(c("#003b70", "#b21e28"), 12)  
# rearrange colors for discreteness
set.seed(35)
col <- sample(col, 12)

# place to store colors by label
col.arr <- vector(length = 43, mode = "character")

map(1:12, {
  function(x) {
    str <- paste0("^", x, " ")
    ind <- which(str_detect(df1$label, str), arr.ind = T)
    col.arr[ind] <<- col[x]
  }
})
left <- which(col.arr == "", arr.ind = T)
col.arr[left] <- "lightgray"

(fig <- plot_ly(
  type = "treemap",
  labels = df1$label,
  parents = df1$parent,
  values = df1$value / 1000,
  branchvalues = "total",
  texttemplate = "%{label}: %{value:.1f} TWh",
  textinfo = "label+value+percent parent+percent",
  marker = list(colors = col.arr))) 

enter image description here

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kat