How to efficiently use ggplotly to visualize 3 categories proportion ?

Basically, there are 3 dimensions or catgories in the data.

We do not want to use facets as the real data has many variables.

However, we want to see the proportion of all 3 dimensions in 1 chart.

How to apply plotly to visualize all 3 categories
e.g. showing the proportion of typ within grd when we clicked on typ.

Can sunburstR help us ?

# Dataset creation
df <- data.frame(cls = c(rep("A",4),rep("B",4)),
                 grd = c("A1",rep("A2",3),rep(c("B1","B2"), 2)),
                 typ = c(rep("m",2),rep("o",2),"m","n",rep("p",2)),
                 pnts = c(rep(1:4,2)))
df %>%
  group_by(cls) %>%
  mutate(per1 = sum(pnts),
         per1_pct = per1 / sum(per1)) %>%
  group_by(cls, grd) %>%
  mutate(per2 = sum(pnts),
         per2_pct = per2 / sum(per2)) %>%
  group_by(cls, grd, typ) %>%
  mutate(per3 = sum(pnts),
         per3_pct = per3 / sum(per3)) %>%
  ungroup() -> data

plt <-data %>% 
  pivot_longer(cols = -c(cls:pnts),
               names_to = "per_cat",
               values_to = "percent") %>%
  ggplot(aes(cls,percent,#col = typ,
             fill = grd
             )) +
  geom_bar(stat = "identity") +
  coord_flip() +
  theme_bw()

plotly::ggplotly(plt)

1 Like

How can we represent all the different hierarchies in the table ? i.e.
per_c_i, per_c and per_s_i
Some kind of bar chart/ stacked bar did not help
Also, sun_burst might be too complimented to implement !

Can you suggest us how to represent all these hierarchies in a single chart ?

library(tidyverse)
> 
> df <- data.frame(item = c("apple","orange","apple","banana","apple","apple"),
+                 cust = c(rep("john",2),"papa","john",rep("jerry",2)),
+                 shop = c("shop1","shop2",rep("shop1",2),rep("shop2",2)),
+                 sales = c(12,13,6,8,10,9)
+                 )
> 
> df_m <- df %>% 
+   group_by(cust,item) %>%
+   mutate(per_c_i = 100*sales/sum(sales)) %>%
+   group_by(item) %>%
+   mutate(per_c = 100* per_c_i / sum(per_c_i))
> 
> df_c <- merge(df,df_m, by = c("cust","item","shop","sales")) %>%
+   group_by(shop,item) %>%
+   mutate(per_s_i = 100*per_c/sum(per_c)) 
> 
> df_c
# A tibble: 6 x 7
# Groups:   shop, item [4]
  cust  item   shop  sales per_c_i per_c per_s_i
  <fct> <fct>  <fct> <dbl>   <dbl> <dbl>   <dbl>
1 jerry apple  shop2    10    52.6  17.5    52.6
2 jerry apple  shop2     9    47.4  15.8    47.4
3 john  apple  shop1    12   100    33.3    50  
4 john  banana shop1     8   100   100     100  
5 john  orange shop2    13   100   100     100  
6 papa  apple  shop1     6   100    33.3    50  
> 

# Attempt via ggplot

df_c %>%
  ggplot(aes(x = cust, 
             y = per_s_i, 
             fill = item)) +
  geom_bar(stat = "identity"#, position=position_dodge()
  ) +
  geom_col() + 
  coord_flip()

df_c %>% knitr::kable()

### SunburstR but doesnt render
library(d3r)
library(sunburstR)
df_s <- df_c %>% 
  rename("level1" = "cust",
         "level2" = "item",
         "level3" = "shop")

tree <- d3_nest(df_s[,c(1:2,4)], value_cols = "sales")
tree

sb1 <- sunburst(tree, width="100%", height=400)
sb2 <- sunburst(
  tree,
  legend = FALSE,
  width = "100%",
  height = 400
)

# do side-by-side for comparison
div(
  style="display: flex; align-items:center;",
  div(style="width:50%; border:1px solid #ccc;", sb1),
  div(style="width:50%; border:1px solid #ccc;", sb2)
)

sb3 <- sund2b(tree, width="100%")

div(
  style="display: flex; align-items:center;",
  sb3
)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.