Skip to content

Instantly share code, notes, and snippets.

@chalg
Last active August 26, 2023 02:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chalg/c324905929c9961b0447048ab986e5bf to your computer and use it in GitHub Desktop.
Save chalg/c324905929c9961b0447048ab986e5bf to your computer and use it in GitHub Desktop.
BP Statistical Review of World Energy visualisation in R
library(tidyverse)
library(scales)
# Read in data and clean (column) names
# Source: https://www.bp.com/en/global/corporate/energy-economics/statistical-review-of-world-energy/downloads.html
# Below assumes you save the CSV in a folder called DATA
bp_wide <- read_csv("DATA/bp-stats-review-2022-consolidated-dataset-panel-format.csv",
name_repair = ~ janitor::make_clean_names(., case = "snake"))
bp_wide %>% glimpse()
bp_wide <- bp_wide %>% mutate(country = recode(country, "US" = "United States"))
# Add consumption variables in TWhs by multiplying be constant.
# This step is not necessary (data can be viewed in EJ) just a personal preference.
bp_wide <- bp_wide %>%
mutate(
hydrocons_twh = hydro_ej * 277.78,
nuclearcons_twh = nuclear_ej * 277.78,
solarcons_twh = solar_ej * 277.78,
windcons_twh = wind_ej * 277.78,
gascons_twh = gascons_ej * 277.78,
coalcons_twh = coalcons_ej * 277.78,
oilcons_twh = oilcons_ej * 277.78,
biogeocons_twh = biogeo_ej * 277.78)
# Setup custom colours - create your own or use a predefined palette.
my_colours <- c("Hydro" = "#56B4E9",
"Nuclear" = "#009E73",
"Solar" = "gold2",
"Wind" = "#CC79A7",
"Gas" = "sandybrown",
"Coal" = "#8B5A2B",
"Oil" = "#36648B",
"Geo, Bio, Oth" = "olivedrab4")
# Check colours look reasonable
scales::show_col(my_colours)
# Set theme
theme_set(theme_minimal())
# Review - easier with glimpse() when there are many columns
bp_wide %>% glimpse()
cons_lng <- bp_wide %>%
# Remove existing aggregated values to avoid double-counting
filter(!grepl(country, pattern = "Total")) %>%
select(country, year,
Hydro = hydrocons_twh,
Nuclear = nuclearcons_twh,
Solar = solarcons_twh,
Wind = windcons_twh,
Gas = gascons_twh,
Coal = coalcons_twh,
Oil = oilcons_twh,
"Geo, Bio, Oth" = biogeocons_twh) %>%
pivot_longer(
cols = !contains(c("country", "year")),
names_to = "technology",
values_to = "value",
# The below will drop rows that contain only NAs in the value_to column
# which is what we want.
values_drop_na = TRUE)
# Create two aggregated tibbles with consumption & percentage variables
# Create global tibble
cons_lng_global <- cons_lng %>%
filter(year >= 1965) %>%
group_by(year, technology) %>%
summarise(annual_cons = sum(value)) %>%
mutate(percentage = annual_cons / sum(annual_cons),
tech_ord = factor(technology, levels=c("Coal", "Oil", "Gas",
"Geo, Bio, Oth", "Hydro",
"Nuclear", "Solar", "Wind"))) %>%
ungroup()
# Create country level tibble
cons_lng_country <- cons_lng %>%
filter(year >= 1965) %>%
group_by(year, country, technology) %>%
summarise(annual_cons = sum(value)) %>%
mutate(percentage = annual_cons / sum(annual_cons),
tech_ord = factor(technology, levels=c("Coal", "Oil", "Gas",
"Geo, Bio, Oth", "Hydro",
"Nuclear", "Solar", "Wind"))) %>%
ungroup()
# Review
glimpse(cons_lng_global)
glimpse(cons_lng_country)
# Area global plot global
cons_lng_global %>%
ggplot(aes(x = year, y = annual_cons, fill = tech_ord)) +
geom_area(alpha = 0.6 , size = 0.3, colour = "white") +
scale_fill_manual(values = my_colours) +
scale_x_continuous(breaks = seq(1965, 2021, 2), expand = c(0,0)) +
scale_y_continuous(labels = scales::comma, expand = c(0,0)) +
labs(title = "Primary Energy Consumption by Technology",
y = "Primary Energy Consumption (TWh)", x = "Year", fill = "Technology",
caption = "Source: BP Statistical Review of World Energy June 2022") +
theme(plot.title = element_text(size = 12, face = "bold"),
axis.title = element_text(size = 11), legend.position = "right",
axis.text.x = element_text(angle = 45, size = 8, hjust = 1),
plot.caption = element_text(size = 8, color = "gray50", face = "italic"),
plot.background = element_rect(fill = 'antiquewhite', colour = 'antiquewhite'),
panel.background = element_rect(fill = 'snow'),
legend.background = element_rect(fill = 'antiquewhite', colour = 'antiquewhite'),
legend.title = element_text(size = 9))
ggsave("energy_consumption_by_tech_global.png", path = "PLOTS", width = 10, height = 7)
# Area plot global percentage
cons_lng_global %>%
ggplot(aes(x = year, y = percentage, fill = tech_ord)) +
geom_area(alpha = 0.6 , size = 0.3, colour = "white") +
cale_fill_manual(values = my_colours) +
scale_x_continuous(breaks = seq(1965, 2021, 2), expand = c(0,0)) +
scale_y_continuous(labels = scales::percent, breaks = seq(0, 1, 0.1), expand = c(0,0)) +
labs(title = "Primary Energy Consumption by Technology",
y = "Energy Consumption (%)", x = "Year", fill = "Technology",
caption = "Source: BP Statistical Review of World Energy June 2022") +
theme(plot.title = element_text(size = 12, face = "bold"),
axis.title = element_text(size = 11), legend.position = "right",
axis.text.x = element_text(angle = 45, size = 8, hjust = 1),
plot.caption = element_text(size = 8, color = "gray50", face = "italic"),
plot.background = element_rect(fill = 'antiquewhite', colour = 'antiquewhite'),
panel.background = element_rect(fill = 'snow'),
legend.background = element_rect(fill = 'antiquewhite', colour = 'antiquewhite'),
legend.title = element_text(size = 9))
ggsave("energy_consumption_perc_by_tech_global.png", path = "PLOTS", width = 9, height = 6)
# Facet 4 countries of interest
# Area plot using geom_area
cons_lng_country %>%
filter(country %in% c("Italy", "Spain", "Belgium", "Germany")) %>%
ggplot(aes(x = year, y = annual_cons, fill = tech_ord)) +
geom_area(alpha = 0.6 , size = 0.3, colour = "white") +
scale_fill_manual(values = my_colours) +
scale_x_continuous(breaks = seq(1965, 2021, 2), expand = c(0,0)) +
scale_y_continuous(labels = scales::comma, expand = c(0,0)) +
facet_wrap(~ country, scales = "free_y", ncol = 2) +
labs(title = "Primary Energy Consumption by Technology",
# subtitle = "United Kingdom",
y = "Energy Consumption (TWh)", x = "Year", fill = "Technology",
caption = "Source: BP Statistical Review of World Energy June 2022") +
theme(plot.title = element_text(size = 12, face = "bold"),
axis.title = element_text(size = 10), legend.position = "right",
axis.text.x = element_text(angle = 45, size = 8, hjust = 1),
strip.text.x = element_text(size = 10, colour = "darkgreen", face = "bold"),
plot.caption = element_text(size = 8, color = "gray50", face = "italic"),
plot.background = element_rect(fill = 'antiquewhite', colour = 'antiquewhite'),
panel.background = element_rect(fill = 'snow'),
legend.background = element_rect(fill = 'antiquewhite', colour = 'antiquewhite'),
legend.title = element_text(size = 9), legend.text = element_text(size = 9))
ggsave("energy_consumption_by_tech_facet.png", path = "PLOTS", width = 14, height = 8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment