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
)