Skip to content

Instantly share code, notes, and snippets.

@USMortality
Last active March 10, 2025 14:53
Show Gist options
  • Save USMortality/06e6ecfe51f2704546a4db6aa9528b6e to your computer and use it in GitHub Desktop.
Save USMortality/06e6ecfe51f2704546a4db6aa9528b6e to your computer and use it in GitHub Desktop.
GDP (PPP) vs Avg Temp [World]
library(readr)
library(rvest)
library(dplyr)
library(ggpubr)
sf <- 2
width <- 1440 * sf
height <- 960 * sf
options(vsc.dev.args = list(width = width, height = height, res = 72 * sf))
# GDP Data
gdp <- read_html(
"https://en.wikipedia.org/wiki/List_of_countries_by_GDP_(PPP)_per_capita"
) %>%
html_nodes("table") %>%
.[[2]] %>%
html_table() %>%
select(1, 2) %>%
setNames(c("country", "gdp")) %>%
mutate(
gdp = as.numeric(gsub(",", "", gdp)),
country = gsub(" \\*", "", country)
) %>%
filter(!is.na(gdp))
# Temperature Data
temp <- read_html(
paste0(
"https://en.wikipedia.org/wiki/",
"List_of_countries_by_average_yearly_temperature"
)
) %>%
html_node("table") %>%
html_table(fill = TRUE) %>%
select(2, 3) %>%
setNames(c("country", "temp")) %>%
mutate(temp = as.numeric(sub("^(\\d+\\.\\d+).*", "\\1", temp)))
# Merge and Plot
chart <- inner_join(gdp, temp) %>%
ggscatter(
x = "temp",
y = "gdp",
label = "country",
add = "reg.line",
add.params = list(color = "blue", fill = "lightgray"),
conf.int = TRUE
) +
stat_cor(color = "red", method = "pearson", label.x = 0.4, label.y = -3) +
labs(
title = "GDP per capita (PPP) vs Average Temperature",
subtitle = "Source: Wikipedia · Made by @USMortality",
x = "Average Temperature (C)",
y = "GDP per Capita (current intl. dollar)"
) +
scale_y_continuous(labels = scales::dollar)
ggplot2::ggsave(
filename = "chart1.png", plot = chart, width = width, height = height,
units = "px", dpi = 72 * sf, device = grDevices::png, type = c("cairo")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment