rplotlytreemapr-plotlyplotly.js

Change Parent and Labels tile labels in plotly treemap


I currently have long labels for Parents and Child nodes. I have created two adjusted columns so that I can use them to graph the treemap in plotly. However, when I use the new adjusted columns for parents and labels i get no graph. I have looked some of the posts in stackoverflow, but couldn't find anything similar. I have been reading this documentation too, but not sure what I have to use: https://plotly.com/r/reference/#pie-scalegroup

library(plotly)
# Generates the graph

plot_ly(type = 'treemap',
               labels = df$labels,
               values = df$size,
               parents = df$parent ,
               branchvalues = "total")%>% 
  layout(uniformtext = list(minsize = 10))




# Doesn't generate the graph

plot_ly(type = 'treemap',
        labels = df$labels_adjusted,
        values = df$size,
        parents = df$parent_adjusted ,
        branchvalues = "total")%>% 
  layout(uniformtext = list(minsize = 10))

dput output

structure(list(parent = c("", "Total", "Total", "Total", "Total", 
"16-21", "16-21", "16-21", "22-28", "22-28", "22-28", "29-33", 
"29-33", "29-33", "34+", "34+", "34+", "16-21 > 16-21 > 0-21", 
"16-21 > 16-21 > 0-21", "16-21 > 16-21 > 0-21", "16-21 > 16-21 > 22-24", 
"16-21 > 16-21 > 22-24", "16-21 > 16-21 > 22-24", "16-21 > 16-21 > 25", 
"16-21 > 16-21 > 25", "16-21 > 16-21 > 25", "22-28 > 22-28 > 0-21", 
"22-28 > 22-28 > 0-21", "22-28 > 22-28 > 0-21", "22-28 > 22-28 > 22-24", 
"22-28 > 22-28 > 22-24", "22-28 > 22-28 > 22-24", "22-28 > 22-28 > 25", 
"22-28 > 22-28 > 25", "22-28 > 22-28 > 25", "29-33 > 29-33 > 0-21", 
"29-33 > 29-33 > 0-21", "29-33 > 29-33 > 0-21", "29-33 > 29-33 > 22-24", 
"29-33 > 29-33 > 22-24", "29-33 > 29-33 > 22-24", "29-33 > 29-33 > 25", 
"29-33 > 29-33 > 25", "29-33 > 29-33 > 25", "34+ > 34+ > 0-21", 
"34+ > 34+ > 0-21", "34+ > 34+ > 0-21", "34+ > 34+ > 22-24", 
"34+ > 34+ > 22-24", "34+ > 34+ > 22-24", "34+ > 34+ > 25", "34+ > 34+ > 25", 
"34+ > 34+ > 25"), labels = c("Total", "16-21", "22-28", "29-33", 
"34+", "16-21 > 16-21 > 0-21", "16-21 > 16-21 > 22-24", "16-21 > 16-21 > 25", 
"22-28 > 22-28 > 0-21", "22-28 > 22-28 > 22-24", "22-28 > 22-28 > 25", 
"29-33 > 29-33 > 0-21", "29-33 > 29-33 > 22-24", "29-33 > 29-33 > 25", 
"34+ > 34+ > 0-21", "34+ > 34+ > 22-24", "34+ > 34+ > 25", "16-21 > 16-21 > 0-21 > 16-21 > 0-21 > None", 
"16-21 > 16-21 > 0-21 > 16-21 > 0-21 > One", "16-21 > 16-21 > 0-21 > 16-21 > 0-21 > Two or More", 
"16-21 > 16-21 > 22-24 > 16-21 > 22-24 > None", "16-21 > 16-21 > 22-24 > 16-21 > 22-24 > One", 
"16-21 > 16-21 > 22-24 > 16-21 > 22-24 > Two or More", "16-21 > 16-21 > 25 > 16-21 > 25 > None", 
"16-21 > 16-21 > 25 > 16-21 > 25 > One", "16-21 > 16-21 > 25 > 16-21 > 25 > Two or More", 
"22-28 > 22-28 > 0-21 > 22-28 > 0-21 > None", "22-28 > 22-28 > 0-21 > 22-28 > 0-21 > One", 
"22-28 > 22-28 > 0-21 > 22-28 > 0-21 > Two or More", "22-28 > 22-28 > 22-24 > 22-28 > 22-24 > None", 
"22-28 > 22-28 > 22-24 > 22-28 > 22-24 > One", "22-28 > 22-28 > 22-24 > 22-28 > 22-24 > Two or More", 
"22-28 > 22-28 > 25 > 22-28 > 25 > None", "22-28 > 22-28 > 25 > 22-28 > 25 > One", 
"22-28 > 22-28 > 25 > 22-28 > 25 > Two or More", "29-33 > 29-33 > 0-21 > 29-33 > 0-21 > None", 
"29-33 > 29-33 > 0-21 > 29-33 > 0-21 > One", "29-33 > 29-33 > 0-21 > 29-33 > 0-21 > Two or More", 
"29-33 > 29-33 > 22-24 > 29-33 > 22-24 > None", "29-33 > 29-33 > 22-24 > 29-33 > 22-24 > One", 
"29-33 > 29-33 > 22-24 > 29-33 > 22-24 > Two or More", "29-33 > 29-33 > 25 > 29-33 > 25 > None", 
"29-33 > 29-33 > 25 > 29-33 > 25 > One", "29-33 > 29-33 > 25 > 29-33 > 25 > Two or More", 
"34+ > 34+ > 0-21 > 34+ > 0-21 > None", "34+ > 34+ > 0-21 > 34+ > 0-21 > One", 
"34+ > 34+ > 0-21 > 34+ > 0-21 > Two or More", "34+ > 34+ > 22-24 > 34+ > 22-24 > None", 
"34+ > 34+ > 22-24 > 34+ > 22-24 > One", "34+ > 34+ > 22-24 > 34+ > 22-24 > Two or More", 
"34+ > 34+ > 25 > 34+ > 25 > None", "34+ > 34+ > 25 > 34+ > 25 > One", 
"34+ > 34+ > 25 > 34+ > 25 > Two or More"), size = c(58760, 5750, 
14718, 10836, 27456, 1336, 1765, 2649, 4541, 4294, 5883, 3473, 
3059, 4304, 8046, 6707, 12703, 773, 343, 220, 1299, 341, 125, 
2549, 78, 22, 2979, 1065, 497, 3597, 559, 138, 5693, 178, 12, 
2387, 805, 281, 2691, 326, 42, 4196, 97, 11, 6011, 1482, 553, 
6032, 594, 81, 12496, 204, 3), level = c(1, 2, 2, 2, 2, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4), parent_adjusted = c("", "Total", "Total", "Total", 
"Total", "16-21", "16-21", "16-21", "22-28", "22-28", "22-28", 
"29-33", "29-33", "29-33", "34+", "34+", "34+", "0-21", "0-21", 
"0-21", "22-24", "22-24", "22-24", "25", "25", "25", "0-21", 
"0-21", "0-21", "22-24", "22-24", "22-24", "25", "25", "25", 
"0-21", "0-21", "0-21", "22-24", "22-24", "22-24", "25", "25", 
"25", "0-21", "0-21", "0-21", "22-24", "22-24", "22-24", "25", 
"25", "25"), labels_adjusted = c("Total", "16-21", "22-28", "29-33", 
"34+", "0-21", "22-24", "25", "0-21", "22-24", "25", "0-21", 
"22-24", "25", "0-21", "22-24", "25", "None", "One", "Two or More", 
"None", "One", "Two or More", "None", "One", "Two or More", "None", 
"One", "Two or More", "None", "One", "Two or More", "None", "One", 
"Two or More", "None", "One", "Two or More", "None", "One", "Two or More", 
"None", "One", "Two or More", "None", "One", "Two or More", "None", 
"One", "Two or More", "None", "One", "Two or More")), row.names = c(NA, 
-53L), class = c("tbl_df", "tbl", "data.frame"))

Solution

  • When you asked this question, I did try to answer it and couldn't figure out what Plotly was really looking for. However, today someone asked a question that gave me that light bulb moment for your question.

    FINALLY!

    Alright, due to the many dimensions, you need an id field that's unique to each row--- but you really already have this. It's your original labels field. When you adjusted your labels, you didn't need to change the parents—only the labels are shown on the treemap.

    plot_ly(type = 'treemap',
            labels = df$labels_adjusted,
            parents = df$parent,
            ids = df$labels,
            values = df$size,
            branchvalues = "total") 
    

    enter image description here

    You may have noticed that I left off the call for uniform text. If you have size, uniformtext is ignored.

    However, you don't set size by value and use uniformtext this is what you get.

    plot_ly(type = 'treemap',
            labels = df$labels_adjusted,
            parents = df$parent,
            ids = df$labels,
            # values = df$size # ,
            # branchvalues = "total"
            ) %>% 
      layout(uniformtext = list(minsize = 10))
    

    enter image description here