Skip to content

Instantly share code, notes, and snippets.

@paulovillarroel
Created February 17, 2024 22:26
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 paulovillarroel/a5910398e2b901882c93df8947cff4a8 to your computer and use it in GitHub Desktop.
Save paulovillarroel/a5910398e2b901882c93df8947cff4a8 to your computer and use it in GitHub Desktop.
Code showing the evolution of Chile's life expectancy (source OECD)
library(tidyverse)
url <- "https://sdmx.oecd.org/public/rest/data/OECD.ELS.HD,DSD_HEALTH_STAT@DF_LE,1.0/all?dimensionAtObservation=AllDimensions&format=csvfilewithlabels"
data <- read_csv(url)
first_last_values <- data |>
filter(
`Reference area` == "Chile",
Measure == "Life expectancy",
AGE == "Y0",
SEX != "_T"
) |>
group_by(SEX) |>
summarise(
first_value = first(OBS_VALUE),
last_value = last(OBS_VALUE),
first_year = first(TIME_PERIOD),
last_year = last(TIME_PERIOD)
)
plot <- data |>
filter(
`Reference area` == "Chile",
Measure == "Life expectancy",
AGE == "Y0",
SEX != "_T"
) |>
ggplot(aes(TIME_PERIOD, OBS_VALUE, color = SEX)) +
geom_line(linewidth = 1) +
geom_point(data = first_last_values, aes(x = first_year, y = first_value), fill = "#42047e", color = "#42047e", size = 3, shape = 21) +
geom_text(data = first_last_values, aes(x = first_year, y = first_value, label = first_value), vjust = -1.5, hjust = 0) +
geom_point(data = first_last_values, aes(x = last_year, y = last_value), fill = "#42047e", color = "#42047e", size = 3, shape = 21) +
geom_text(data = first_last_values, aes(x = last_year, y = last_value, label = last_value), vjust = 1.8, hjust = 0.5) +
labs(
title = "Life expectancy in Chile",
x = "Year",
y = "Life expectancy",
caption = "Source: OECD"
) +
theme_minimal() +
theme(legend.position = "none") +
scale_color_manual(values = c("#d90368", "#009ffd")) +
scale_x_continuous(breaks = seq(1960, 2020, 10)) +
scale_y_continuous(breaks = seq(50, 90, 5))
plot + annotate("text", x = 2028, y = 78, label = "Male", color = "#009ffd") +
annotate("text", x = 2028, y = 84, label = "Female", color = "#d90368")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment