Skip to content

Instantly share code, notes, and snippets.

@paulovillarroel
Created February 23, 2024 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulovillarroel/cb0ab7a819687ecfe9e2577ea7fede55 to your computer and use it in GitHub Desktop.
Save paulovillarroel/cb0ab7a819687ecfe9e2577ea7fede55 to your computer and use it in GitHub Desktop.
Evolution of the number of births in Chile
library(tidyverse)
library(ggdark)
urls <- c(
"https://repositoriodeis.minsal.cl/DatosAbiertos/VITALES/PUBLICADA%20NOV%202022%20MDC/Serie_Nacimientos_1992_2000.zip",
"https://repositoriodeis.minsal.cl/DatosAbiertos/VITALES/PUBLICADA%20NOV%202022%20MDC/Serie_Nacimientos_2001_2019.zip",
"https://repositoriodeis.minsal.cl/DatosAbiertos/VITALES/PUBLICADA%20NOV%202022%20MDC/DA_Nacimientos_2020.zip"
)
file_names <- c("nacimientos1.zip", "nacimientos2.zip", "nacimientos3.zip")
for (i in seq_along(urls)) {
download.file(urls[i], paste0("raw-data/", file_names[i]))
unzip(paste0("raw-data/", file_names[i]), exdir = "raw-data")
}
nacimientos1 <- read_csv2("raw-data/Serie_Nacimientos_1992_2000.csv", locale = locale(encoding = "ISO-8859-1"))
nacimientos2 <- read_csv2("raw-data/Serie_Nacimientos_2001_2019.csv", locale = locale(encoding = "ISO-8859-1"))
nacimientos3 <- read_csv2("raw-data/DA_Nacimientos_2020.csv", locale = locale(encoding = "ISO-8859-1"))
todos_nac <- bind_rows(nacimientos1, nacimientos2, nacimientos3)
todos_nac <- todos_nac |>
filter(GRUPO_ETARIO_MADRE != "NO ESPECIFICADO") |>
mutate(
GRUPO_ETARIO_MADRE = case_when(
GRUPO_ETARIO_MADRE == "MENORES 15 AÑO" ~ "MENORES 15 AÑOS",
TRUE ~ GRUPO_ETARIO_MADRE
),
GRUPO_ETARIO_MADRE = fct_relevel(
GRUPO_ETARIO_MADRE,
"MENORES 15 AÑOS",
"15 A 19 AÑOS",
"20 A 24 AÑOS",
"25 A 29 AÑOS",
"30 A 34 AÑOS",
"35 A 39 AÑOS",
"40 A 44 AÑOS",
"45 A 49 AÑOS",
"50 O MAS AÑOS"
)
)
todos_nac |>
group_by(GRUPO_ETARIO_MADRE, ANO_NAC) |>
summarise(n = n()) |>
ggplot(aes(ANO_NAC, n)) +
geom_area(fill = "#ffafcc") +
geom_line(linewidth = 1, color = "#f72585") +
labs(
title = "Evolución de Nacimientos en Chile (1992 - 2020)",
x = "Año",
y = "Nacimientos",
caption = "Fuente: Datos abiertos DEIS MINSAL"
) +
dark_theme_gray() +
scale_y_continuous(labels = scales::comma, breaks = seq(0, 1000000, 20000)) +
facet_wrap(~GRUPO_ETARIO_MADRE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment