Last active
July 26, 2024 02:22
-
-
Save jrosell/8c238cb1776ff877612eb98bf1c6017b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if(!"rlang" %in% installed.packages()){ | |
if(!interactive()) { stop("The package \"rlang\" is required.") } | |
cat("The package \"rlang\" is required.\n✖ Would you like to install it?\n\n1: Yes\n2: No\n\nSelection:") | |
if (readLines(n = 1) == "1"){ | |
install.packages("rlang") | |
} | |
} | |
rlang::check_installed( | |
"jrrosell (>= 0.0.0.9006)", | |
action = \(pkg,...) { | |
if("jrrosell" %in% installed.packages()){ remove.packages("jrrosell") } | |
pak::pak("jrosell/jrrosell") | |
} | |
) | |
rlang::check_installed("tidyverse") | |
invisible(!dir.exists("data") && dir.create("data")) | |
library(tidyverse) | |
jrrosell::theme_set_roboto_darkblue() | |
# Pots descarregar les dades del banc de dades al directori data manualment, | |
# o executar la seguent funció | |
download_banco_datos <- \(x) { | |
rlang::check_installed("selenider") | |
library(selenider) | |
sess <- selenider_session( | |
"chromote", | |
timeout = 10, | |
options = chromote_options(headless = FALSE) | |
) | |
sess$driver$Browser$setDownloadBehavior( | |
behavior = "allow", | |
downloadPath = "data" | |
) | |
open_url("http://buscadorcdi.gob.es/Cifra/es/buscador/resultados/Ultimos-Distribucion-Territorial") | |
try({ | |
elem_click(s(".introjs-skipbutton"), timeout = 10) | |
}) | |
try({ | |
elem_click(s(".guardarBusqueda li:nth-child(3) img"), timeout = 20) | |
}) | |
Sys.sleep(10) | |
selenider::close_session() | |
} | |
comunidades <- c( | |
"PAÍS VASCO", | |
"CATALUÑA", | |
"GALICIA", | |
"ANDALUCÍA", | |
"PRINCIPADO DE ASTURIAS", | |
"CANTABRIA", | |
"LA RIOJA", | |
"REGION DE MURCIA", | |
"COMUNITAT VALENCIANA", | |
"ARAGÓN", | |
"CASTILLA-LA MANCHA", | |
"CANARIAS", | |
"COMUNIDAD FORAL DE NAVARRA", | |
"EXTREMADURA", | |
"ILLES BALEARS", | |
"COMUNIDAD DE MADRID", | |
"CASTILLA Y LEON", | |
"CEUTA", | |
"MELILLA" | |
) | |
dat <- list.files("data") |> | |
{\(x) paste0("data/", x[str_detect(x, "Informe_Cifra")]) }() |> | |
read_csv2( | |
show_col_types = FALSE, | |
locale = readr::locale( | |
encoding = "latin1", | |
decimal_mark = ",", | |
grouping_mark = "." | |
), | |
skip = 1, | |
col_names = c( | |
"cod_administracion", "administracion", | |
"jerarquia_administracion", "submateria", | |
"materia", "cod_variable", | |
"variable", "nombre_corto_variable", | |
"unidad", "escala", | |
"ano", "trimestre", | |
"mes", "observaciones", | |
"valor", "variable_referencia", | |
"base_submateria_referencia", "unidad_variable_referencia", | |
"escala_variable_referencia", "valor_variable_referencia", | |
"ano_variable_referencia", "trimestre_variable_referencia", | |
"mes_variable_referencia", "observaciones_variable_referencia" | |
), | |
col_select = 1:24 | |
) |> | |
janitor::clean_names() |> | |
separate_wider_position( | |
cod_variable, c(cod_comunidad = 2), too_many = "debug" | |
) |> | |
select(-cod_comunidad, -cod_variable, -cod_variable_width, -cod_variable_ok) |> | |
rename(cod_variable = cod_variable_remainder) |> | |
filter(cod_variable %in% c("AGE_OR","OOAA_OR","ESTI_IR","EMP_IR")) |> | |
filter(str_detect(variable, paste(comunidades, collapse = "|"))) |> | |
separate_wider_delim( | |
variable, delim = ":", names = c("comunidad", "variable_resto") | |
) |> | |
select(-variable_resto) |> | |
mutate( | |
nombre_corto_variable = nombre_corto_variable |> | |
str_replace(paste0(" en ", comunidad), "") |> | |
str_replace(" en MURCIA", "") |> | |
str_replace(" en ASTURIAS", "") |> | |
str_replace(" en C. VALENCIANA", "") |> | |
str_replace(" en NAVARRA", "") |> | |
str_replace(" en C.LA MANCHA", "") |> | |
str_replace(" en MADRID", "") | |
) |> | |
select(comunidad, nombre_corto_variable, ano, valor) |> | |
filter(ano >= 2015) |> | |
arrange(comunidad, ano) | |
df_ccaa <- dat |> | |
group_by(comunidad, ano) |> | |
mutate(total = sum(valor)) |> | |
ungroup() |> | |
pivot_longer(valor:total) |> | |
mutate( | |
name = if_else( | |
name != "total", nombre_corto_variable, "Total ejecutado" | |
) | |
)|> | |
select(-nombre_corto_variable) |> | |
unique() | |
p1 <- ggplot(df_ccaa, aes(ano, value, colour = name)) + | |
geom_line(data = df_ccaa |> filter(name != "Total ejecutado")) + | |
geom_line(data = df_ccaa |> filter(name == "Total ejecutado"), size = 2) + | |
facet_wrap("comunidad") + | |
scale_y_continuous( | |
labels = scales::unit_format(unit = "M €", scale = 1e-6, sep = "") | |
) + | |
labs( | |
title = "Distribución territorial de la inversión del Sector Público Estatal", | |
subtitle = "Total ejecutado = Inversión real EMPRESAS + Inversión real ESTIMATIVOS + Oblig. reconocidas de la AGE + Oblig. reconocidas de ORGANISMOS", | |
caption = "Fuente: buscadorcdi.gob.es/Cifra/es/buscador/resultados/Ultimos-Distribucion-Territorial | Autor: @jrosell", | |
y = "", x = "", colour = "" | |
) | |
print(p1) | |
p2 <- dat |> | |
rename(value = valor) |> | |
filter(comunidad %in% c("COMUNIDAD DE MADRID", "CATALUÑA")) |> | |
ggplot(aes(ano, value)) + | |
geom_col(aes(fill = nombre_corto_variable)) + | |
geom_text( | |
nudge_y = 200000000, | |
aes(label = scales::number(value, suffix = "M €", scale = 1e-6, sep = "")), | |
data = df_ccaa |> filter(comunidad %in% c("COMUNIDAD DE MADRID", "CATALUÑA"), name == "Total ejecutado") | |
) + | |
scale_x_continuous(breaks = 2009:2021, trans = "reverse") + | |
scale_y_continuous( | |
labels = scales::unit_format(unit = "M €", scale = 1e-6, sep = "") | |
) + | |
facet_wrap("comunidad") + | |
coord_flip() + | |
labs( | |
title = "Distribución territorial de la inversión del Sector Público Estatal en Cataluña y Comunidad de Madrid", | |
subtitle = "Total ejecutado = Inversión real EMPRESAS + Inversión real ESTIMATIVOS + Oblig. reconocidas de la AGE + Oblig. reconocidas de ORGANISMOS", | |
caption = "Fuente: buscadorcdi.gob.es/Cifra/es/buscador/resultados/Ultimos-Distribucion-Territorial | Autor: @jrosell", | |
y = "", x = "", fill = "" | |
) | |
print(p2) | |
df_catalunya_madrid <- df_ccaa |> | |
filter(comunidad %in% c("COMUNIDAD DE MADRID", "CATALUÑA"), name == "Total ejecutado") |> | |
group_by(comunidad, name) |> | |
summarize(value = sum(value)) | |
p3 <- dat |> | |
rename(value = valor) |> | |
filter(comunidad %in% c("COMUNIDAD DE MADRID", "CATALUÑA")) |> | |
group_by(comunidad, nombre_corto_variable) |> | |
summarize(value = sum(value)) |> | |
ungroup() |> | |
ggplot(aes(comunidad, value)) + | |
geom_col(aes(fill = nombre_corto_variable)) + | |
geom_text( | |
nudge_y = 500000000, | |
aes(label = scales::number(value, suffix = "M €", scale = 1e-6, sep = "")), | |
data = df_catalunya_madrid | |
) + | |
scale_y_continuous( | |
labels = scales::unit_format(unit = "M €", scale = 1e-6, sep = "") | |
) + | |
coord_flip() + | |
labs( | |
title = "Distribución territorial de la inversión del Sector Público Estatal en Cataluña y Comunidad de Madrid de 2015-2021", | |
subtitle = "Total ejecutado = Inversión real EMPRESAS + Inversión real ESTIMATIVOS + Oblig. reconocidas de la AGE + Oblig. reconocidas de ORGANISMOS", | |
caption = "Fuente: buscadorcdi.gob.es/Cifra/es/buscador/resultados/Ultimos-Distribucion-Territorial | Autor: @jrosell", | |
y = "", x = "", fill = "" | |
) | |
print(p3) | |
Author
jrosell
commented
Jul 26, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment