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)