0

I have a sunburst diagram, that - after some help - works quite well. However, since the margin of the data is quite big, there is a problem with the visibility of some elements. A small example:

library(plotly)
library(dplyr)
df2 <- data.frame(KtoID = c(66,661,6610,6611,6612,6613,6614,6615,6616,6617,6618,6619,6651,6691),
                  label=c("A","B","Ba","Bc","Bd","Be","Bf","Bg","Bh","Bi","Bk","Bl","C","D"),
                  value_2020=c(208800494,3669502,NA,NA,14,NA,NA,940704,91560,2614368,22856,NA,163422732,41708260),
                  value_2019=c(202946326,14278132,NA,42,50,NA,NA,703978,129434,13417048,27580,NA,153535498,35132696),
                  parent_ID= c(NA,66,661,661,661,661,661,661,661,661,661,661,66,66))

y_axis_var_names <- c('value_2020', 'value_2019')

create_buttons2 <- function(df2, y_axis_var_names) {
  lapply(y_axis_var_names,
         FUN = function(var_name, df2) {
           button <- list(
             method = 'restyle',
             args = list('values', list(df2[, var_name])),
             label = gsub("value_","",sprintf('Year %s',var_name))
           )},
         df2)}

fig <- plot_ly(ids=df2$KtoID,
               labels = df2$label,
               parents = df2$parent_ID,
               values = df2$value_2020,
               type = 'sunburst',
               maxdepth=2,
               insidetextorientation="horizontal",
               branchvalues = 'total')
fig <- fig %>% layout(separators = ', ',                                     
                      autosize = T, 
                      hoverlabel=list(bgcolor='#ffffff',
                                      bordercolor = '#dcdcdc',
                                      align = "left",
                                      font = list(color="#000000"),
                                      size=12),
                      updatemenus = list(
                        list(
                          y = 0.95,
                          x = 0.7,
                          xanchor = "left",
                          font= list(size = 12,
                                     color = "#000000"),
                          bordercolor="#a3a3a3",
                          borderwidth = 1.5,
                          buttons = create_buttons2(df2, y_axis_var_names))
                            ))

fig

When clicking on Element B the diagram zooms in. Now, when you carefully point the mouse on the small "invisible" Element on the right side, the tooltip for element Bd (value = 14) is shown (element "Bc" is missing because it contains no value for 2020). When you use the button to select the "Year 2019", you now can only find the element "Bc" (value = 42) (here) but not "Bd" (value = 50), although both elements are connected to values. I assume the reason is the small values for these elements. (When you inspect the html code, you can the "missing" element.)

Since I cannot change the data, I would like to add a kind of legend that updates analogous to the selected (element). So for example, when clicked on element B the Legend should change from listing the children of the root element to a list, containing the B elements. Maybe it is also possible to provide different colors.

I know you can somehow use plotly_click and plotly_sunburstclick events (discussed here).

But I don't know how to achive what I described. Do I need shiny for that?

RAU
  • 3
  • 2

0 Answers0