Skip to content

Instantly share code, notes, and snippets.

@szechno
Created November 15, 2025 21:48
Show Gist options
  • Select an option

  • Save szechno/54737f54566f5c53850f0a5e80785d0e to your computer and use it in GitHub Desktop.

Select an option

Save szechno/54737f54566f5c53850f0a5e80785d0e to your computer and use it in GitHub Desktop.
library(sf)
library(shiny)
library(dplyr)
library(readr)
library(units)
library(mapgl)
library(stringr)
# d |> filter((end >= set_units(2020, "year")) & (end <= set_units(2029, "year"))| is.na(end)) |> View()
#
# https://walker-data.com/mapgl/articles/story-maps.html
#
# Electricity statistics
# Data from the National Statistics publication Digest of UK Energy Statistics (DUKES) produced by the Department for Energy Security & Net Zero (DESNZ).
# The data presented is the historical time series of UK electricity since 1920.
# https://www.gov.uk/government/collections/electricity-statistics
#
# https://www.gov.uk/
d <- read_rds("electricity1920_2020.rds") |>
filter(!is.na(start)) |> # drop
mutate(fuel = str_to_title(fuel)) |>
mutate(S0000 = ifelse(start <= set_units(1919, "year") & (end >= set_units(1910, "year") | is.na(end)),
TRUE, FALSE)) |>
mutate(S1920 = ifelse(start <= set_units(1929, "year") & (end >= set_units(1920, "year") | is.na(end)),
TRUE, FALSE)) |>
mutate(S1930 = ifelse(start <= set_units(1939, "year") & (end >= set_units(1930, "year") | is.na(end)),
TRUE, FALSE)) |>
mutate(S1940 = ifelse(start <= set_units(1949, "year") & (end >= set_units(1940, "year") | is.na(end)),
TRUE, FALSE)) |>
mutate(S1950 = ifelse(start <= set_units(1959, "year") & (end >= set_units(1950, "year") | is.na(end)),
TRUE, FALSE)) |>
mutate(S1960 = ifelse(start <= set_units(1969, "year") & (end >= set_units(1960, "year") | is.na(end)),
TRUE, FALSE)) |>
mutate(S1970 = ifelse(start <= set_units(1979, "year") & (end >= set_units(1970, "year") | is.na(end)),
TRUE, FALSE)) |>
mutate(S1980 = ifelse(start <= set_units(1989, "year") & (end >= set_units(1980, "year") | is.na(end)),
TRUE, FALSE)) |>
mutate(S1990 = ifelse(start <= set_units(1999, "year") & (end >= set_units(1990, "year") | is.na(end)),
TRUE, FALSE)) |>
mutate(S2000 = ifelse(start <= set_units(2009, "year") & (end >= set_units(2000, "year") | is.na(end)),
TRUE, FALSE)) |>
mutate(S2010 = ifelse(start <= set_units(2019, "year") & (end >= set_units(2010, "year") | is.na(end)),
TRUE, FALSE)) |>
mutate(S2020 = ifelse(start <= set_units(2029, "year") & (end >= set_units(2020, "year") | is.na(end)),
TRUE, FALSE))
# Bioenergy Coal Gas Hydro Nuclear Oil Pumped Hydro Solar
# 25 225 68 112 23 51 4 236
# Wind
# 458
zvalues = c("Bioenergy",
"Coal",
"Gas",
"Hydro",
"Nuclear",
"Oil",
"Pumped Hydro",
"Solar",
"Wind")
zstops = c("#AAFF00", #bio
"black", # coal
"#191970",# gas
"#89CFF0", #hydro
"#EE4B2B", # nuclear
"#702963", #oil
"#7393B3", #hydro
"#FFBF00", #solar
"#DFFF00"#wind
)
# ui ----
ui <- fluidPage(
tags$link(href = "https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap", rel="stylesheet"),
story_map(
map_id = "map",
map_type = c("maplibre"),
font_family = "Poppins",
sections = list(
"intro" = story_section(
title = "UK electricity statistics",
content = list(
p("Data from the National Statistics publication Digest of UK Energy ",
"Statistics (DUKES) produced by the Department for Energy Security & ",
"Net Zero (DESNZ).", br(), "The data presented is the historical time ",
"series of UK electricity since 1920.", br(),
a(href="https://www.gov.uk/government/collections/electricity-statistics",
"www.gov.uk/government/collections/electricity-statistics"), br(),
br(), "macka szechno 14 November 2025"
)
),
position = "center"
),
"1920" = story_section(
title = "The 1920s",
content = list(
p("Power generation during the 1920s.")
)
),
"1930" = story_section(
title = "The 1930s",
content = list(
p("Power generation during the 1930s.")
)
),
"1940" = story_section(
title = "The 1940s",
content = list(
p("Power generation during the 1940s.")
)
),
"1950" = story_section(
title = "The 1950s",
content = list(
p("Power generation during the 1950s.")
)
),
"1960" = story_section(
title = "The 1960s",
content = list(
p("Power generation during the 1960s.")
)
),
"1970" = story_section(
title = "The 1970s",
content = list(
p("Power generation during the 1970s.")
)
),
"1980" = story_section(
title = "The 1980s",
content = list(
p("Power generation during the 1980s.")
)
),
"1990" = story_section(
title = "The 1990s",
content = list(
p("Power generation during the 1990s.")
)
),
"2000" = story_section(
title = "The 2000s",
content = list(
p("Power generation during the 2000s.")
)
),
"2010" = story_section(
title = "The 2010s",
content = list(
p("Power generation during the 2010s.")
)
),
"2020" = story_section(
title = "The 2020s",
content = list(
p("Power generation during the 2020s.")
)
)
)
)
)
# server ----
server <- function(input, output, session) {
output$map <- renderMaplibre({
maplibre(style = carto_style("voyager")) |>
fit_bounds(d) |>
add_circle_layer(id = "all",
source = d,
circle_color = match_expr("fuel",
values = zvalues,
stops = zstops,
default = "#cccccc"
),
popup = concat("Start ", get_column("start"),
"<br />End ", get_column("end"))) |>
add_categorical_legend(legend_title = "Fuel type",
values = zvalues,
colors = zstops)
})
on_section("map", "1920", {
maplibre_proxy("map") |>
clear_markers() |>
set_filter("all", filter = list("==", "S1920", TRUE)) |>
fit_bounds(d)
})
on_section("map", "1930", {
maplibre_proxy("map") |>
clear_markers() |>
fit_bounds(d) |>
set_filter("all", filter = list("==", "S1930", TRUE))
})
on_section("map", "1940", {
maplibre_proxy("map") |>
clear_markers() |>
fit_bounds(d) |>
set_filter("all", filter = list("==", "S1940", TRUE))
})
on_section("map", "1950", {
maplibre_proxy("map") |>
clear_markers() |>
fit_bounds(d) |>
set_filter("all", filter = list("==", "S1950", TRUE))
})
on_section("map", "1960", {
maplibre_proxy("map") |>
clear_markers() |>
fit_bounds(d) |>
set_filter("all", filter = list("==", "S1960", TRUE))
})
on_section("map", "1970", {
maplibre_proxy("map") |>
clear_markers() |>
fit_bounds(d) |>
set_filter("all", filter = list("==", "S1970", TRUE))
})
on_section("map", "1980", {
maplibre_proxy("map") |>
clear_markers() |>
fit_bounds(d) |>
set_filter("all", filter = list("==", "S1980", TRUE))
})
on_section("map", "1990", {
maplibre_proxy("map") |>
clear_markers() |>
fit_bounds(d) |>
set_filter("all", filter = list("==", "S1990", TRUE))
})
on_section("map", "2000", {
maplibre_proxy("map") |>
clear_markers() |>
fit_bounds(d) |>
set_filter("all", filter = list("==", "S2000", TRUE))
})
on_section("map", "2010", {
maplibre_proxy("map") |>
clear_markers() |>
fit_bounds(d) |>
set_filter("all", filter = list("==", "S2010", TRUE))
})
on_section("map", "2020", {
maplibre_proxy("map") |>
clear_markers() |>
fit_bounds(d) |>
set_filter("all", filter = list("==", "S2020", TRUE))
})
}
shinyApp(ui, server)
@szechno
Copy link
Author

szechno commented Nov 15, 2025

Code can be optimised by creating functions for replication and by visualising results by megawattage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment