score:1

Starting from your example dataframe df, process the data slightly to add variable year as character:

df <- df %>% 
    mutate(YMD = ymd(year),
           year = as.character(year(YMD)))

... and do the grouping and aggregation during the composition of your plot (no need to cram your workspace with dataframelets):

library(dplyr)

## (preceding plot instructions omitted)
## ...
hc_add_series(data = df %>%
                  group_by(year) %>%
                  summarise(y = mean(value, na.rm = TRUE)) %>%
                  mutate(name = year, drilldown = year) #, ... other args
              ) %>%
hc_drilldown(allowPointDrilldown = TRUE,
             series = list(
                 df %>%
                 group_by(year) %>%
                 summarise(drilldown_series = 
                               list(id = first(year),
                                    data =  list_parse2(
                                        df = .[c('year', 'value')] )
                                    )
                           ) %>%
                 pull(drilldown_series)
             )

If your dataframe has a lot of columns, you might want to stack these, e.g with pivot_longer, i. e. you narrow these columns down to two: one contains the variable name, the second its value:

library(tidyr)

df %>%
    pivot_longer(cols = first_column_to_stack : last_col_to_stack,
                 names_to = "variable",
                 values_to = "value"
                 )

... now you can dynamically filter the long-format dataframe by variable (and year and ...) before feeding it as plot arguments.


Related Query

More Query from same tag