Skip to content

Instantly share code, notes, and snippets.

@matteodefelice
Created September 18, 2024 18:07
Show Gist options
  • Save matteodefelice/139e058c2fff676652cda69d3cc3d426 to your computer and use it in GitHub Desktop.
Save matteodefelice/139e058c2fff676652cda69d3cc3d426 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(countrycode)
library(scico)
library(patchwork)
library(ggtext)
mapdata <- map_data("world") |>
mutate(iso3_code = countrycode(region, "country.name", "iso3c"))
edgar_data <- readxl::read_excel(
"data/EDGAR_AR5_GHG_1970_2022/EDGAR_AR5_GHG_1970_2022.xlsx",
sheet = "TOTALS BY COUNTRY", skip = 9
) |>
mutate(
delta_abs = Y_2022 - Y_2021,
delta_rel = delta_abs / Y_2021,
label_abs = round(delta_abs / 1e3)
)
toplot <- mapdata |>
left_join(
edgar_data |> select(Country_code_A3, delta_abs, delta_rel),
by = c("iso3_code" = "Country_code_A3")
) |>
filter(!long > 180)
g <- ggplot(toplot) +
geom_polygon(aes(x = long, y = lat, group = group, fill = delta_rel),
color = "grey90", linewidth = 0.2
) +
theme_void() +
scale_fill_scico(
na.value = "grey77", palette = "vik",
midpoint = 0, direction = 1,
labels = scales::percent_format(),
name = "Change (%)",
breaks = seq(-1, 1, .05)
) +
ggalt::coord_proj(
"+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs",
ylim = c(-60, 80)
) +
expand_limits(x = toplot$long, y = toplot$lat) +
theme(
legend.key.height = unit(1, "cm")
)
# https://github.com/milos-agathon/Inset-graph-within-map-in-R
# https://github.com/milos-agathon/neet_2019
sel_barplot <- edgar_data |>
filter(!(Country_code_A3 %in% c("AIR"))) |>
filter(abs(delta_abs) > 10000)
g1 <- ggplot(
sel_barplot,
aes(x = reorder(Name, delta_abs), y = delta_abs, fill = delta_rel)
) +
geom_bar(stat = "identity") +
geom_text(
data = sel_barplot |>
filter(label_abs > -50),
aes(label = label_abs),
position = position_stack(vjust = .5),
hjust = 0.5,
size = 2.75,
color = "grey10",
family = "georg"
) +
geom_text(
data = sel_barplot |>
filter(label_abs <= -50),
aes(label = label_abs),
position = position_stack(vjust = .5),
hjust = 0.5,
size = 2.75,
color = "grey90",
family = "georg"
) +
scale_fill_scico(
na.value = "grey77", palette = "vik",
midpoint = 0, direction = 1,
labels = scales::percent_format(),
name = "Change (%)"
) +
theme(
panel.background = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
panel.border = element_blank(),
text = element_text(family = "georg"),
strip.text = element_text(size = 12),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.line = element_line(colour = NA),
axis.line.x = element_blank(),
axis.line.y = element_blank(),
axis.text.y = element_text(margin = unit(c(3, 0, 0, 0), "mm"), colour = "grey10", size = 8, hjust = 0),
axis.text.x = element_blank(),
axis.ticks = element_blank(),
legend.title = element_text(),
plot.title = element_text(size = 8, color = "grey20", hjust = .5),
legend.key = element_rect(fill = NA),
legend.position = "none", legend.direction = "horizontal"
) +
xlab("") +
ylab("") +
coord_flip() +
labs(caption = "Change in megatonnes\n(only countries with significant changes)") +
theme(
plot.caption = element_text(
hjust = 0, face = "italic"
),
plot.caption.position = "plot"
)
layout <- c(
area(t = 1.1, l = 1, b = 5, r = 1.5),
area(t = 1.1, l = 1.5, b = 5, r = 5)
)
p <- g1 + g + plot_layout(design = layout) +
plot_annotation(
title = "<b>Global emissions: changes between 2022 and 2021</b><br>
<span style = 'font-size:10pt'>CO2-equivalent emissions (GWP-100 AR5) from JRC EDGAR v8.0
</span>",
caption = "Emissions Database for Global Atmospheric Research (EDGAR)\n(https://edgar.jrc.ec.europa.eu/dataset_ghg80)\nvisualisation by @matteodefelice",
theme = theme(
plot.title = element_textbox_simple(
size = 18,
lineheight = 1,
padding = margin(5.5, 5.5, 5.5, 5.5),
margin = margin(0, 0, 5.5, 0),
)
)
)
ggsave("main.png", width = 11, height = 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment