Treemap in R for subgroups of data

I am trying to plot treemap for topic and sub-topic level. So that inside each topic block, there needs to be subtopic as per the values. For example, topic "ty" should be a huge block since the sum of value "n" is more. Under this block there should be sub blocks with respective to Sub_topics? Is this possible to achieve?

library(plotly)
library(readxl)
dtd7 <- structure(list(topic = c("sf", "sf", "sf", "fgds", "fgds", "ty",
                         "ty", "ty", "ty", "ty"), Sub_topic = c("a", "b", "c", "a", "b",
                                                                "a", "b", "c", "d", "e"), n = c(3, 1, 5, 3, 1, 6, 4, 6, 3, 1)), row.names = c(NA,
                                                                                                                                              -10L), class = c("tbl_df", "tbl", "data.frame"))

# Book1 <- read_excel("Book1.xlsx")
dtd7 <- as.data.frame(Book1)
p <- plot_ly(
  dtd7,
  labels = ~ topic,
  parents = ~ Sub_topic,
  values = ~ n,
  type = 'treemap'
  # ,hovertemplate = "Ingredient: %{label}<br>Count: %{value}<extra></extra>"
)

p

is your example arbitrary , or does it follow the example of some actual data you have ? because it doesnt make sense for a treemap in its current form from what I see. There are repeats of id's at both parent and label levels.
I would expect maybe repeats in the parent level, as some lower labels might have the same parent . Otherwise I think the treemap becomes hard to define how it should be drawn.

Its just an example . The treemap I am expecting is for a subgroups as well.
Refer Treemap Charts tab in (RPubs - Data visualization: Sunburst and Treemaps)

As displayed in the plot, it has main category and inside that there is subgroups. Similar to this.

Your link doesnt work for me.
But treemaps can be done, here is an example adapted from the plotly website
Treemap Charts | R | Plotly

library(plotly)

dt1 <- data.frame(
subtopic = c("Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"),
topic = c("", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve"),
n= c(10, 14, 12, 10, 2, 6, 6, 1, 4))

p <- plot_ly(
  dt1,
  type='treemap',
  labels=~subtopic,
  parents=~topic,
  values= ~n
)

image

Thanks . It is almost. But my data frame is like this. Under each topic there are subtopic that needs to go under respective block

library(plotly)

dt1 <- data.frame(
  
  topic = c("Eve", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve"),
  subtopic = c("a", "b", "c", "a", "b", "x", "y", "a", "tr"),
  n= c(10, 14, 12, 10, 2, 6, 6, 1, 4))

p <- plot_ly(
  dt1,
  type='treemap',
  labels=~subtopic,
  parents=~topic,
  values= ~n
)

p
dt1 <- data.frame(
  topic = c("", "Eve", "Eve", "", "Seth", "Eve", "Eve", "", "Eve"),
  subtopic = c("Eve", "b", "c", "Seth", "sb", "x", "y", "Awan", "tr"),
  n = c(10, 14, 12, 10, 2, 6, 6, 1, 4)
)

here I made minimal edits to your example, do make it plottable.
Don't make a row where the topic and subtopic is the same name. the treemap is heirarchical so the topic should be blank for for eve when eve's subtopic value is given.

This is brilliant Nir. Thanks a lot. This is what i wanted. :slight_smile:

Hi Nir,
I just notified one thing.


dt1 <- data.frame(
  topic = c("",  "" , "", "Eve", "Eve", "Seth", "Seth", "Eve",  "Eve"),
  subtopic = c("Eve", "Seth", "Awan" , "b", "c",  "sb", "x", "y","tr"),
  n = c(23, 10, 14,  1, 12,  2, 6, 6, 4), new = c(12,21,12,2,1,2,1,3,4),
  color_code = c("#004E42", "#007763", "#00997C","","","","","","")
)

If you look here. Eve is 23, and its sub topic values are sum to 23. But in the plots the entire box is not covering up. I referred the documentation but did not get. Sample code below

library(plotly)



dt1 <- data.frame(
  topic = c("",  "" , "", "Eve", "Eve", "Seth", "Seth", "Eve",  "Eve"),
  subtopic = c("Eve", "Seth", "Awan" , "b", "c",  "sb", "x", "y","tr"),
  n = c(23, 10, 14,  1, 12,  2, 6, 6, 4), new = c(12,21,12,2,1,2,1,3,4),
  color_code = c("#004E42", "#007763", "#00997C","","","","","","")
)

p <- plot_ly(
  dt1,
  type='treemap',
  labels=~subtopic,
  parents=~topic,
  values= ~n,
  # text = paste0(dt1$new),
  marker=list(colors=dt1$color_code)
)



p