R: How to create a Drilldown Highchart using loops

70 Views Asked by At

when doing a job I have found a problem that I don't know how to solve. I have a data frame that has 2 columns:

  • date
  • value

And it has a total of 1303 rows.

For each date there are 12 values (1 for each month), except in the last year that only has 7

The work I have to do would be to create a 'drilldown' style chart using the 'highcharter' library. The problem is that I don't know how to do it efficiently. The solution that comes to my mind is not very efficient, below I show my solution so you can see what I mean.

dataframe

# Load packages
library(tidyverse)
library(highcharter)
library(lubridate)
# Load dataset
df <- read.csv('example.csv')
# Prepare df to use
dfDD <- tibble(name = year(df$date),
               y = round(df$value, digits = 2),
               drilldown = name)
# Create a data frame to use in 'drilldown' (for each year)
df1913 <- df %>%
  filter(year(date) == 1913) %>%
  data.frame()

df1914 <- df %>%
  filter(year(date) == 1914) %>%
  data.frame()
# Create a drilldown chart using Highcharter library
highchart() %>%
  hc_chart(type = "column") %>%
  hc_title(text = "Example Drilldown") %>%
  hc_xAxis(type = "category") %>%
  hc_legend(enabled = FALSE) %>%
  hc_plotOptions(series = list(boderWidth = 2,
                               dataLabels = list(enabled = TRUE))) %>%
  hc_add_series(data = dfDD,
                name = "Mean",
                colorByPoint = TRUE) %>%
  hc_drilldown(allowPointDrilldown = TRUE,
               series = list(list(id = 1913,
                                  data = list_parse2(df1913)),
                             list(id = 1914,
                                  data = list_parse2(df1914))))

Seeing my solution for the first time, I realized that in order to complete the graph I would have to create a subset of values for each year. Having realized that I tried to find a more efficient solution using a 'for loop' but so far I can't get it to work.

Is there a more efficient way to create this graph using a 'loop'!?

If it can be done in another way than using loops, I would also like to know.

Thank you for reading my question and I hope I explained myself well.

1

There are 1 best solutions below

1
stefan On

Using split and purrr::imap you could split your data by years and loop over the resulting list to convert your data to the nested list object required by hc_drilldown. Note: It's important to make the id a numeric and to pass a unnamed list.

library(tidyverse)
library(highcharter)
library(lubridate)

series <- split(df, year(df$date)) %>% 
  purrr::imap(function(x, y) list(id = as.numeric(y), data = list_parse2(x)))
# Unname list
names(series) <- NULL

highchart() %>%
  hc_chart(type = "column") %>%
  hc_title(text = "Example Drilldown") %>%
  hc_xAxis(type = "category") %>%
  hc_legend(enabled = FALSE) %>%
  hc_plotOptions(series = list(boderWidth = 2,
                               dataLabels = list(enabled = TRUE))) %>%
  hc_add_series(data = dfDD,
                name = "Mean",
                colorByPoint = TRUE) %>%
  hc_drilldown(allowPointDrilldown = TRUE,
               series = series)

enter image description here