## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
library("rKolada")

## ----echo = FALSE-------------------------------------------------------------
kpis <- rKolada:::kpi_df
munic <- rKolada:::munic

## ----eval = FALSE-------------------------------------------------------------
# kpis <- get_kpi(cache = FALSE)
# munic <- get_municipality(cache = FALSE)

## -----------------------------------------------------------------------------
dplyr::glimpse(kpis)

## -----------------------------------------------------------------------------
# Get a list KPIs matching a search for "bruttoregionprodukt" (Gross regional product)
kpi_res <- kpis |>
  kpi_search("bruttoregionprodukt") |>
  # "K" = data available at municipality level (not just region)
  kpi_search("K", column = "municipality_type") |>
  kpi_minimize(remove_monotonous_data = TRUE)

dplyr::glimpse(kpi_res)

## -----------------------------------------------------------------------------
munic_res <- munic |>
  # type "K" = municipality (kommun), vs "L" for region (län)
  municipality_search("K", column = "type") |>
  # Only keep Stockholm, Gothenburg and Malmö
  municipality_search(c("Stockholm", "Göteborg", "Malmö"))

dplyr::glimpse(munic_res)

## ----echo = TRUE, results='asis'----------------------------------------------
kpi_res |>
  kpi_bind_keywords(n = 4) |>
  kpi_describe(max_n = 1, format = "md", heading_level = 4, sub_heading_level = 5)

## ----echo = FALSE-------------------------------------------------------------
kld_data <- rKolada:::kld_data

## ----eval = FALSE-------------------------------------------------------------
# kld_data <- get_values(
#   kpi = kpi_extract_ids(kpi_res),
#   municipality = municipality_extract_ids(munic_res),
#   period = 1990:2019,
#   simplify = TRUE
# )

## -----------------------------------------------------------------------------
# Visualise results
library("ggplot2")

ggplot(kld_data, aes(x = year, y = value)) +
  # One line per municipality, coloured by name
  geom_line(aes(color = municipality)) +
  # Separate panel per KPI (stacked vertically)
  facet_grid(kpi ~ .) +
  # Thousand separators for readability
  scale_y_continuous(labels = scales::comma) +
  labs(
    title = "Gross Regional Product",
    subtitle = "Yearly development in Sweden's three\nmost populous municipalities",
    x = "Year",
    y = "",
    caption = values_legend(kld_data, kpis)  # Auto-generated KPI legend
  )

