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