2

I am working with the R programming language.

I am trying to follow the answers provided over here: Treemap plot with plotly in R

I made my own dataset for this problem:

library(treemap)
library(plotly)
library(dplyr)
library(stringr)
########################

var1 <- c("A", "B", "C", "D", "E")
var1<- sample(var1, 1000, replace=TRUE, prob=c(0.2, 0.2, 0.2, 0.2, 0.2))
var1<- as.factor(var1)

var2 <- c("abc", "bcd", "egf", "hiu", "lkj", "oiu", "piu", "xsr", "zze", "tre")
var2<- sample(var2, 1000, replace=TRUE, prob=c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1))
var2<- as.factor(var2)

my_data = data.frame(var1, var2)

my_data = data.frame(my_data %>% group_by(var1, var2) %>% summarise(counts = n()))

Then, I tried to adapt the answers provided in this link (Treemap plot with plotly in R) to recreate these visualizations for my data:

# visualization 1
(g4EMP1 <- plot_ly(
  data = my_data,
  type = "treemap",
  labels = ~var2 %>% stringr::str_wrap(width = 15),
  branchvalues = "total",
  values = ~counts,
  parents = ~var1)) 

# visualization 2

(g4EMP2 <- plot_ly(
  data = my_data,
  type = "treemap",
  labels = ~var2%>% stringr::str_wrap(width = 15),
  #values = ~size,
  parents = ~var1)) %>% 
  layout(uniformtext = list(minsize = 10))

However, when I try to view these plots - all I see is a blank screen?

Can someone please show me what I am doing wrong and what can I do to fix this?

Thanks!

stats_noob
  • 5,401
  • 4
  • 27
  • 83

1 Answers1

2

I haven't figured out the left and proper lateral limits of how Plotly works out treemaps. I did stumble upon a way that works, though.

Since your treemap is "many" dimensional, you'll need a unique ID field for each row. I'm not sure what dimension size constitutes this necessity by Plotly standards. The easiest way is to use the child and parent names.

This column needs to be the first column in the data frame. (I have NO idea why.)

(my_data <- my_data %>% # create id labels for each row
    mutate(ids = ifelse(var1 == "", var2, 
                        paste0(var2, "-", var1))) %>% 
    select(ids, everything()))

You also have to add the parents to items with the total for the parent. In the parent columns, you'll enter empty strings (because the parent doesn't have a parent).

(par_info <- my_data %>% group_by(var1) %>%  # group by parent
    summarise(counts = sum(counts)) %>%  # parent total
    rename(var2 = var1) %>%            # parent labels for the item field
    mutate(var1 = "", ids = var2) %>%  # add missing fields for my_data
    select(names(my_data)))            # put cols in same order as my_data

Now smash your original data with the content in par_info, and you're finally ready to plot.

my_data2 <- rbind(my_data, par_info)

plot_ly(
  data = my_data2, branchvalues = "total",
  type = "treemap", labels = ~var2,
  parents = ~var1, values = ~counts, ids = ~ids)

enter image description here

Kat
  • 15,669
  • 3
  • 18
  • 51
  • 1
    @ Kat: Thank you so much! I hope in the future, people who are stuck on this problem will come across this answer you have posted as it appears to exactly solve this general problem of making 'treemaps" in R! – stats_noob Oct 23 '22 at 21:30
  • 1
    Very nice workaround! – Jilber Urbina Dec 09 '22 at 19:52
  • @ kat: i have been working on a leaflet problem in R ... can you please take a look at this? https://stackoverflow.com/questions/76142813/legend-disappearing-on-geographic-map-of-north-carolina thank you so much! – stats_noob May 02 '23 at 23:56
  • It looks like you've already worked out the answers to your questions. Am I missing something? (Looks great, BTW!) – Kat May 03 '23 at 15:30