Last active
July 17, 2024 09:01
-
-
Save jrosell/b489acb1c5f58c283ddbf15546352099 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
--- | |
title: "robo violencia" | |
output: html_document | |
date: "2024-06-19" | |
editor_options: | |
chunk_output_type: console | |
--- | |
## CCAA | |
```{r} | |
library(jrrosell) | |
library(tidyverse) | |
library(openxlsx) | |
library(rvest) | |
theme_set_roboto_darkblue() | |
robos_intimidacion_2024T1 <- openxlsx::read.xlsx(here::here("robo-violencia/09001.xlsx"), startRow = 9, colNames = FALSE) |> | |
set_names(c("ccaa","valor")) |> | |
as_tibble() |> | |
slice_head(n = 19) |> | |
mutate( | |
ccaa = str_replace_all(ccaa, fixed(" - "), "") |> | |
str_replace_all(fixed("CIUDAD AUTÓNOMA DE "), "") | |
) |> | |
arrange(ccaa) | |
robos_intimidacion_2024T1 | |
``` | |
```{r} | |
safe_html <- safely(read_html, otherwise = NULL) | |
poblacion_2023 <- safe_html("https://datosmacro.expansion.com/demografia/poblacion/espana-comunidades-autonomas") |> | |
pluck("result") |> | |
html_table() |> | |
{\(x) x[[1]]}() |> | |
janitor::clean_names() |> | |
transmute( | |
ccaa = str_replace_all(ccaa, fixed("["), "") |> | |
str_replace_all(fixed("+"), "") |> | |
str_replace_all(fixed("]"), "") |> | |
str_replace_all(fixed("Región de "), "") |> | |
str_replace_all(fixed("Islas "), "") |> | |
str_replace_all(fixed("Comunidad de "), "") |> | |
str_replace_all("^La ", "") |> | |
str_trim(), | |
poblacion = str_replace_all(poblacion, fixed("."), "") |> parse_integer() | |
) |> | |
arrange(ccaa) | |
poblacion_2023 | |
``` | |
```{r} | |
library(sf) | |
library(jrrosell) | |
data(spain_ccaas) | |
robos_intimidacion_por_poblacion <- | |
spain_ccaas |> | |
arrange(nombre) |> | |
bind_cols(robos_intimidacion_2024T1, poblacion_2023) |> | |
select(codigo, nombre, geometry, valor, poblacion) |> | |
mutate( | |
robos_violentos_poblacion = round(valor / (poblacion/1000000)), | |
) | |
``` | |
```{r} | |
ccaa_labels <- robos_intimidacion_por_poblacion %>% st_centroid() | |
ccaa_labels <- do.call(rbind, st_geometry(ccaa_labels)) %>% | |
as_tibble() %>% | |
rename(x = V1) %>% | |
rename(y = V2) %>% | |
cbind(ccaa_labels) | |
ccaa_labels | |
``` | |
```{r} | |
ggplot(robos_intimidacion_por_poblacion) + | |
stat_sf_coordinates() + | |
geom_sf(aes(fill = robos_violentos_poblacion)) + | |
shadowtext::geom_shadowtext(data = ccaa_labels, aes(label = robos_violentos_poblacion, x = x, y = y)) + | |
scale_fill_continuous(trans = "reverse") + | |
labs( | |
x = NULL, y = NULL, | |
title = "Robos violentos por millón de habitantes en 2024T1", | |
caption = "Por @jrosell | Fuentes: Estadisticas de criminalidad MIR 2024T1 y población datosmacro.com 2023" | |
) + | |
theme( | |
legend.position = "none", | |
axis.title.x=element_blank(), | |
axis.text.x=element_blank(), | |
axis.ticks.x=element_blank(), | |
axis.title.y=element_blank(), | |
axis.text.y=element_blank(), | |
axis.ticks.y=element_blank(), | |
grid.major=element_blank(), | |
panel.grid.major = element_blank() | |
) | |
``` | |
## Provincias | |
```{r} | |
library(tidyverse) | |
library(openxlsx) | |
library(rvest) | |
robos_intimidacion_2024T1_prov <- openxlsx::read.xlsx(here::here("robo-violencia/09002.xlsx"), startRow = 8, colNames = FALSE) |> | |
set_names(c("provincia","robos")) |> | |
as_tibble() |> | |
slice_head(n = 52) |> | |
mutate( | |
provincia = str_replace(provincia, "Araba/Á", "Á") |> | |
str_replace("Á", "Á") |> | |
str_replace("á", "á") |> | |
str_replace("ó", "ó") |> | |
str_replace("é", "é") | |
) |> | |
arrange(provincia, .locale = "es") | |
robos_intimidacion_2024T1_prov |> print(n = Inf) | |
``` | |
```{r} | |
poblacion_provincia <- openxlsx::read.xlsx(here::here("robo-violencia/56945.xlsx"), startRow = 10, colNames = FALSE) |> | |
set_names(c("provincia","residentes")) |> | |
as_tibble() |> | |
slice_head(n = 52) |> | |
mutate(provincia = str_replace(provincia, "^...","") |> | |
str_replace("Araba/", "")) |> | |
arrange(provincia, .locale = "es") | |
poblacion_provincia |> print(n = Inf) | |
``` | |
```{r} | |
library(sf) | |
library(jrrosell) | |
data(spain_provinces) | |
robos_intimidacion_por_poblacion <- | |
spain_provinces |> | |
distinct(codigo, nombre) |> | |
mutate(nombre = str_replace(nombre, "Vizcaya","Bizkaia") |> | |
str_replace("Guipúzcoa", "Gipuzkoa")) |> | |
arrange(nombre, .locale = "es") |> | |
bind_cols(robos_intimidacion_2024T1_prov, poblacion_provincia) |> | |
select(codigo, nombre, robos, residentes) |> | |
mutate( | |
robos_violentos_poblacion = round(robos / (residentes/1000000)), | |
) |> | |
right_join(select(spain_provinces, -nombre), by = join_by(codigo)) |> | |
st_as_sf() | |
robos_intimidacion_por_poblacion |> print(n = Inf) | |
``` | |
```{r} | |
provinces_labels <- robos_intimidacion_por_poblacion %>% st_centroid() | |
provinces_labels <- do.call(rbind, st_geometry(provinces_labels)) |> | |
as_tibble() |> | |
rename(x = V1) |> | |
rename(y = V2) |> | |
cbind(provinces_labels) |> | |
summarize( | |
.by = c(codigo, nombre), | |
x = first(x), | |
y = first(y), | |
robos = first(robos), | |
residentes = first(residentes), | |
robos_violentos_poblacion = first(robos_violentos_poblacion) | |
) | |
provinces_labels | |
``` | |
```{r} | |
robos_intimidacion_por_poblacion |> | |
ggplot() + | |
stat_sf_coordinates() + | |
geom_sf(aes(fill = robos_violentos_poblacion)) + | |
shadowtext::geom_shadowtext(data = provinces_labels, aes(label = robos_violentos_poblacion, x = x, y = y)) + | |
scale_fill_continuous() + | |
labs( | |
x = NULL, y = NULL, | |
title = "Robos violentos por millón de habitantes en 2024T1", | |
caption = "Por @jrosell | Fuentes: Estadisticas de criminalidad MIR 2024T1 y residentes 2023 INE" | |
) + | |
theme( | |
legend.position = "none", | |
axis.title.x=element_blank(), | |
axis.text.x=element_blank(), | |
axis.ticks.x=element_blank(), | |
axis.title.y=element_blank(), | |
axis.text.y=element_blank(), | |
axis.ticks.y=element_blank(), | |
panel.grid.major = element_blank() | |
) | |
``` | |
Invertint l'escala de colors | |
```{r} | |
robos_intimidacion_por_poblacion |> | |
ggplot() + | |
stat_sf_coordinates() + | |
geom_sf(aes(fill = robos_violentos_poblacion)) + | |
shadowtext::geom_shadowtext(data = provinces_labels, aes(label = robos_violentos_poblacion, x = x, y = y)) + | |
scale_fill_continuous(trans = 'reverse') + # scale_fill_continuous(type = 'viridis', trans = "reverse") + | |
labs( | |
x = NULL, y = NULL, | |
title = "Robos violentos por millón de habitantes en 2024T1", | |
caption = "Por @jrosell | Fuentes: Estadisticas de criminalidad MIR 2024T1 y residentes 2023 INE" | |
) + | |
theme( | |
legend.position = "none", | |
axis.title.x=element_blank(), | |
axis.text.x=element_blank(), | |
axis.ticks.x=element_blank(), | |
axis.title.y=element_blank(), | |
axis.text.y=element_blank(), | |
axis.ticks.y=element_blank(), | |
panel.grid.major = element_blank() | |
) | |
``` | |
Invertint l'escala de color i fent-la logaritmica | |
```{r} | |
robos_intimidacion_por_poblacion |> | |
mutate(robos_violentos_poblacion_fill = log(robos_violentos_poblacion)) |> | |
ggplot() + | |
stat_sf_coordinates() + | |
geom_sf(aes(fill = robos_violentos_poblacion_fill)) + | |
shadowtext::geom_shadowtext(data = provinces_labels, aes(label = robos_violentos_poblacion, x = x, y = y)) + | |
scale_fill_continuous(trans = 'reverse') + # scale_fill_continuous(type = 'viridis', trans = "reverse") + | |
labs( | |
x = NULL, y = NULL, | |
title = "Robos violentos por millón de habitantes en 2024T1", | |
caption = "Por @jrosell | Fuentes: Estadisticas de criminalidad MIR 2024T1 y residentes 2023 INE" | |
) + | |
theme( | |
legend.position = "none", | |
axis.title.x=element_blank(), | |
axis.text.x=element_blank(), | |
axis.ticks.x=element_blank(), | |
axis.title.y=element_blank(), | |
axis.text.y=element_blank(), | |
axis.ticks.y=element_blank(), | |
panel.grid.major = element_blank() | |
) | |
``` | |
Author
jrosell
commented
Jun 21, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment