jakegross808 / pacn-veg-package

all pacn veg code
Creative Commons Zero v1.0 Universal
1 stars 1 forks source link

sunburst plotly with dropdown or button to change dataset #39

Closed jakegross808 closed 1 month ago

jakegross808 commented 2 years ago

Is it possible to get a plotly 'type=sunburst' to switch between datasets using a button or dropdown menu?

Here is a simple dataset I am using:

list1 <- (dplyr::tibble(
  labels = c("Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"),
  parents = c("", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve"),
  values = c(65, 14, 12, 10, 2, 6, 6, 4, 4),
))

nest1 <- list1 %>%
  tidyr::nest(data = everything()) %>%
  dplyr::mutate(name = "bible")

list2 <- (dplyr::tibble(
  labels = c("Abraham", "Herb", "Homer", "Bart", "Lisa", "Maggie"),
  parents = c("", "Abraham", "Abraham", "Homer", "Homer", "Homer"),
  values = c(100, 25, 75, 25, 25, 25),
))

nest2 <- list2 %>%
  tidyr::nest(data = everything()) %>%
  dplyr::mutate(name = "simpsons")

fam_tree <- dplyr::bind_rows(nest1, nest2)`

Then, by just changing just one number I can make two different plots:

plotly::plot_ly(fam_tree[[1]][[1]], labels = ~labels, parents = ~parents, values = ~values, type = 'sunburst', branchvalues = 'total') 
plotly::plot_ly(fam_tree[[1]][[2]], labels = ~labels, parents = ~parents, values = ~values, type = 'sunburst', branchvalues = 'total')

But I can't find any good online resources showing how to make the dropdown or button approach work for a sunburst plot?

wright13 commented 2 years ago

I went down a few different paths with this, and there's not a ton we can do to change the dataset shown while still being able to see the full dataset (all groups) in a single plot without it being broken down by group. If we add an additional "grouping" level to the sunburst plot though, we can use the sunburst plot or a crosstalk dropdown to look at one or more groups at a time. I added this next to the original plot, since the original plot is still needed to see the overall numbers (not broken down by group). It's not a perfect solution, and worth continuing to pursue other options (maybe a different type of plot next to the overall sunburst, or vice versa?), but hopefully it's helpful!