stats_noob
stats_noob

Reputation: 5925

Treemaps With Plotly: Blank Screen?

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!

Upvotes: 2

Views: 1239

Answers (1)

Kat
Kat

Reputation: 18724

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

Upvotes: 4

Related Questions