Last active
December 19, 2023 08:53
-
-
Save sientifiko/0564456204b5208be7ec55feab22a5fd to your computer and use it in GitHub Desktop.
Script para la nota 1 sobre guía de desigualdad en medium
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
library(tidyverse) | |
theme_set(theme_bw(base_size = 21)) | |
options(scipen = 999) | |
dat <- read.csv("dataregionesChile.csv") | |
# filtrar el año 2020 | |
y2020 <- dat %>% filter(year == 2020) | |
# generar histograma | |
y2020 %>% | |
ggplot() + | |
aes(pib) + | |
geom_histogram(bins = 30) + | |
scale_x_continuous(labels = scales::dollar, | |
breaks = seq(0, 80000, 5000)) + | |
theme(axis.text.x = element_text(angle = 90, vjust = .5, hjust = 1)) + | |
labs(x="PIBs", y="Veces que se repite cada PIB") | |
# generar densidad de kernel | |
y2020 %>% | |
ggplot() + | |
aes(pib) + | |
geom_density(size = 1.5, aes(color = "Defecto")) + | |
geom_density(bw = 1000, aes(color = "1.000")) + # ACÁ PUEDEN JUGAR CON LA SUAVIZACIÓN, ES EL PARÁMETRO BW | |
scale_x_continuous(labels = scales::dollar, | |
breaks = seq(0, 80000, 5000)) + | |
theme(axis.text.x = element_text(angle = 90, vjust = .5, hjust = 1)) + | |
labs(x="PIBs", y="Densidad", | |
color = "Suavización") | |
# PROPORCIÓN DE SANTIAGO MÁS ANTOFAGASTA RESPECTO A PIB TOTAL | |
(y2020$pib[y2020$region=="RM"] + y2020$pib[y2020$region=="Antofagasta"]) /sum(y2020$pib) | |
# curva lorenz | |
y2020 %>% | |
arrange(pib) %>% | |
mutate(pcumreg = row_number()/15, | |
pcumpib = cumsum(pib/sum(pib))) %>% | |
ggplot() + | |
aes(pcumreg, pcumpib) + | |
geom_line(size = 1.5, color = "red") + | |
geom_line(aes(y=pcumreg)) + | |
scale_x_continuous(labels = scales::percent, | |
breaks = seq(0, 1, .1)) + | |
scale_y_continuous(labels = scales::percent, | |
breaks = seq(0, 1, .1)) + | |
labs(x="Porcentaje acumulado de regiones", | |
y="Porcentaje acumulado de PIB") | |
# curva lorenz por año | |
dat %>% | |
filter(year %in% c(2008, 2014, 2020)) %>% | |
group_by(year) %>% | |
arrange(year, pib) %>% | |
mutate(pcumreg = row_number()/15, | |
pcumpib = cumsum(pib/sum(pib))) %>% | |
ggplot() + | |
aes(pcumreg, pcumpib, | |
color = as.factor(year), group = as.factor(year)) + | |
geom_line(size = .9) + | |
# geom_line(aes(y=pcumreg)) + | |
scale_x_continuous(labels = scales::percent, | |
breaks = seq(0, 1, .1)) + | |
scale_y_continuous(labels = scales::percent, | |
breaks = seq(0, 1, .1)) + | |
theme(legend.position = c(.1, .8)) + | |
labs(x="Porcentaje acumulado de regiones", | |
y="Porcentaje acumulado de PIB", | |
color = "Año") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Muchas gracias por el código. ¿Dónde se puede obtener la base de datos: 'dataregionesChile.csv' ?