Lectura, uso de otras variables como el sexo del jefe perspectiva de género
#https://www.inegi.org.mx/programas/enigh/nc/2024/#microdatos
# Cuadro 2.1
# Ingreso corriente promedio trimestral por hogar en deciles de
# hogares y su coeficiente de GINI
# 2024
## Limpia la pantalla de tablas o basura de un ejercicio anterior
rm(list = ls())
## Carga librerías
## Abre la tabla concentradohogar
ruta<-"C:/Users/cguer/Documents/Claudia/Midropbox/Investigacion y escritos/karamanis/data/"
infile <- "concentradohogar.CSV"
nomarchi<-paste0(ruta,infile)
Conc<-read.csv(nomarchi,stringsAsFactors = FALSE)
#attach(Conc)
## Selecciona las variables de interés
Conc <- Conc [ c('folioviv', 'foliohog', 'ing_cor', 'factor', 'upm','est_dis','sexo_jefe','ubica_geo','gasto_mon','transporte','publico','foraneo','adqui_vehi','mantenim','refaccion','combus','comunica')]
## Se define la columna de los deciles
Numdec<-c('Total', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X')
Numdec<-c('I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X')
ruta<-"C:/Users/cguer/Documents/Claudia/Midropbox/Investigacion y escritos/karamanis/data/"
infile <- "ubicageo.CSV"
nomarchi<-paste0(ruta,infile)
ubicageo<-read.csv(nomarchi,stringsAsFactors = FALSE)
#unique(ubicageo$desc_ent)
Conc<-left_join(Conc, ubicageo, by = "ubica_geo")
#unique(ubicageo$desc_ent)
## Hogares
## Se crea una bandera para numerar los hogares
Conc$Nhog <- 1
#Conc<-filter(Conc,entidad==5)
## Ordena Conc de acuerdo a ing_cor, folioviv, foliohog
Conc<- orderBy (~+ing_cor+folioviv+foliohog, data=Conc)
## Suma todos los factores y guarda el valor en el vector tot_hogares
tot_hogares <- sum(Conc$factor)
## Se divide la suma de factores entre diez para sacar el tamaño del decil
## (se debe de truncar el resultado quitando los decimales)
tam_dec<-trunc(tot_hogares/10)
## Muestra la suma del factor en variable hogar
Conc$tam_dec=tam_dec
## Creación de deciles de hogares
## Se renombra la tabla concentrado a BD1
BD1 <- Conc
## Dentro de la tabla BD1 se crea la variable MAXT y se le asignan los
## valores que tiene el ing_cor.
BD1$MAXT <- BD1$ing_cor
## Se ordena de menor a mayor según la variable MAXT
BD1 <- BD1[with(BD1, order(rank(MAXT))),]
## Se aplica la función cumsum, suma acumulada a la variable factor
BD1$ACUMULA <- cumsum(BD1$factor)
## Entra a un ciclo donde genera los deciles 1 a 10
for(i in 1:9)
{
a1<-BD1[dim(BD1[BD1$ACUMULA<tam_dec*i,])[1]+1,]$factor
BD1<-rbind(BD1[1:(dim(BD1[BD1$ACUMULA<tam_dec*i,])[1]+1),],
BD1[(dim(BD1[BD1$ACUMULA<tam_dec*i,])[1]+1):dim(BD1[1])[1],])
b1<-tam_dec*i-BD1[dim(BD1[BD1$ACUMULA<tam_dec*i,])[1],]$ACUMULA
BD1[(dim(BD1[BD1$ACUMULA<tam_dec*i,])[1]+1),]$factor<-b1
BD1[(dim(BD1[BD1$ACUMULA<tam_dec*i,])[1]+2),]$factor<-(a1-b1)
}
BD1$ACUMULA2<-cumsum(BD1$factor)
BD1$DECIL <- 0
BD1[(BD1$ACUMULA2<=tam_dec),]$DECIL <- 1
for(i in 1:9)
{
BD1[((BD1$ACUMULA2>tam_dec*i)&(BD1$ACUMULA2<=tam_dec*(i+1))),]$DECIL <- (i+1)
}
#BD1[as.numeric(BD1$DECIL) %in% '0',]$DECIL <- 10
## Total de hogares
x <- tapply(BD1$factor,BD1$Nhog,sum)
## Deciles
y <- tapply(BD1$factor,as.numeric(BD1$DECIL),sum)
## Se calcula el promedio de ingreso para el total y para cada uno de los deciles
ing_cormed_t <- tapply(BD1$factor*BD1$ing_cor,BD1$Nhog,sum)/x
ing_cormed_d <- tapply(BD1$factor*BD1$ing_cor,as.numeric(BD1$DECIL),sum)/y
##transporte
gasdecil <- BD1%>%
#group_by(DECIL,sexo_jefe) %>%
group_by(DECIL) %>%
summarise(gasmon = sum(gasto_mon*factor),n=n())
alidecil <- BD1%>%
#group_by(DECIL,sexo_jefe) %>%
group_by(DECIL) %>%
summarise(gasali = sum(transporte*factor))
#alidecil <- BD1%>%
# group_by(DECIL,sexo_jefe) %>%
# summarise(gasali = sum(carnes*factor))
#alitod<-inner_join(gasdecil, alidecil, by = c("sexo_jefe","DECIL"))
alitod<-inner_join(gasdecil, alidecil, by = c("DECIL"))
alitod$promaligtot<-(alitod$gasali/alitod$gasmon)*100
Proporción de gastos en transporte por decil
A<-ggplot(alitod) +
ggdist::geom_dots(alitod , mapping = aes(x = as.numeric(DECIL), y = alitod$promaligtot, fill = alitod$promaligtot), size = .5,color="black") +
ggrepel::geom_text_repel(data = alitod, mapping = aes(x = as.numeric(DECIL), y = alitod$promaligtot,label = str_wrap(round(promaligtot,2), 4)), lineheight = 0.9, seed = 999, segment.size = 0.1, size = 3.5, direction = "x",fontface = "bold") +
scale_x_continuous(breaks = breaks_width(1),limits=c(0,11)) +
scale_y_continuous(labels = scales::label_comma())+
colorspace::scale_fill_binned_sequential(palette= "YlGnBu", rev =TRUE,n.breaks = 15,guide = guide_colorsteps(show.limits = TRUE, title = "% transporte"),labels = scales::label_comma())+
# facet_wrap(vars(sexo_jefe), ncol = 2) +
labs(
title = "% Gasto en transporte por Decil",
#subtitle = str_wrap("1:Masculino 2:Femenino"),
caption = "Fuente: ENIGH 2024 "
) +
theme_minimal() +
theme(
legend.position = "right",
legend.key.height = unit(3, "lines"),
legend.key.width = unit(1, "lines"),
plot.background = element_rect(fill = "grey99", color = NA),
axis.title = element_blank(),
panel.grid.major.y = element_blank(),
axis.text.y = element_text(face = "bold", size = 12, color = "grey10"),
strip.text = element_text(size = 15, face = "bold"),
panel.spacing.y = unit(1.5, "lines"),
plot.margin = margin(10, 10, 10, 10),
plot.title = element_text(size = 18, face = "bold"),
plot.subtitle = element_text(size = 12, margin = margin(0, 0, 10, 0)),
plot.caption = element_text(margin = margin(10, 0, 0, 0))
)
A
library(glue)
map<-"inegi"
pal<-"purples"
if (!dir.exists(glue("images/{map}"))) {
dir.create(glue("images/{map}"))
}
outfile <- str_to_lower(glue("images/{map}/{map}_{pal}trans3.png"))
# Now that everything is assigned, save these objects so we
# can use then in our markup script
ggsave(outfile, width = 300, height = 300, units = "mm", dpi = "retina",limitsize = FALSE)
##transporte
gasdecil <- BD1%>%
group_by(DECIL,sexo_jefe) %>%
# group_by(DECIL) %>%
summarise(gasmon = sum(gasto_mon*factor),n=n())
alidecil <- BD1%>%
group_by(DECIL,sexo_jefe) %>%
# group_by(DECIL) %>%
summarise(gasali = sum(transporte*factor))
alitod<-inner_join(gasdecil, alidecil, by = c("sexo_jefe","DECIL"))
#alitod<-inner_join(gasdecil, alidecil, by = c("DECIL"))
alitod$promaligtot<-(alitod$gasali/alitod$gasmon)*100
alitod1<-alitod |> filter(sexo_jefe==1)
alitod2<-alitod |> filter(sexo_jefe==2)
library(ggnewscale)
B<-ggplot() +
ggdist::geom_dots(alitod1 , mapping = aes(x = as.numeric(DECIL), y = alitod1$promaligtot, fill = alitod1$promaligtot), size = .5,color="black") +
ggrepel::geom_text_repel(data = alitod1, mapping = aes(x = as.numeric(DECIL), y = alitod1$promaligtot,label = str_wrap(round(promaligtot,2), 4)), lineheight = 0.9, seed = 999, segment.size = 0.1, size = 3.5, direction = "x",fontface = "bold") +
scale_x_continuous(breaks = breaks_width(1),limits=c(0,11)) +
scale_y_continuous(labels = scales::label_comma())+
colorspace::scale_fill_binned_sequential(palette= "YlGnBu", rev =TRUE,n.breaks = 15,guide = guide_colorsteps(show.limits = TRUE, title = "jefe hombre"),labels = scales::label_comma())+
new_scale("fill") +
ggdist::geom_dots(alitod2 , mapping = aes(x = as.numeric(DECIL), y = alitod2$promaligtot, fill = alitod2$promaligtot), size = .5,color="black") +
ggrepel::geom_text_repel(data = alitod2, mapping = aes(x = as.numeric(DECIL), y = alitod2$promaligtot,label = str_wrap(round(promaligtot,2), 4)), lineheight = 0.9, seed = 999, segment.size = 0.1, size = 3.5, direction = "x",fontface = "bold") +
scale_x_continuous(breaks = breaks_width(1),limits=c(0,11)) +
scale_y_continuous(labels = scales::label_comma())+
colorspace::scale_fill_binned_sequential(palette= "Oranges", rev =TRUE,n.breaks = 15,guide = guide_colorsteps(show.limits = TRUE, title = "jefe mujer"),labels = scales::label_comma())+
#facet_wrap(vars(sexo_jefe), ncol = 2) +
labs(
title = "% Gasto en transporte por Decil-sexo",
subtitle = str_wrap("1:Masculino 2:Femenino"),
caption = "Fuente: ENIGH 2024 "
) +
theme_minimal() +
theme(
legend.position = "right",
legend.key.height = unit(3, "lines"),
legend.key.width = unit(1, "lines"),
plot.background = element_rect(fill = "grey99", color = NA),
axis.title = element_blank(),
panel.grid.major.y = element_blank(),
axis.text.y = element_text(face = "bold", size = 12, color = "grey10"),
strip.text = element_text(size = 15, face = "bold"),
panel.spacing.y = unit(1.5, "lines"),
plot.margin = margin(10, 10, 10, 10),
plot.title = element_text(size = 18, face = "bold"),
plot.subtitle = element_text(size = 12, margin = margin(0, 0, 10, 0)),
plot.caption = element_text(margin = margin(10, 0, 0, 0))
)
B
outfile <- str_to_lower(glue("images/{map}/{map}_{pal}trans2.png"))
# Now that everything is assigned, save these objects so we
# can use then in our markup script
ggsave(outfile, width = 300, height = 300, units = "mm", dpi = "retina",limitsize = FALSE)
En qué alimento se gasta más de acuerdo al DECIL
## Abre la tabla concentradohogar
ruta<-"C:/Users/cguer/Documents/Claudia/Midropbox/Investigacion y escritos/karamanis/data/"
infile <- "concentradohogar.CSV"
nomarchi<-paste0(ruta,infile)
Conc<-read.csv(nomarchi,stringsAsFactors = FALSE)
#attach(Conc)
## Selecciona las variables de interés
Conc <- Conc [ c('folioviv', 'foliohog', 'ing_cor', 'factor', 'upm','est_dis','sexo_jefe','ubica_geo','gasto_mon','transporte','publico','foraneo','adqui_vehi','mantenim','refaccion','combus','comunica')]
## Se define la columna de los deciles
Numdec<-c('Total', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X')
Numdec<-c('I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X')
ruta<-"C:/Users/cguer/Documents/Claudia/Midropbox/Investigacion y escritos/karamanis/data/"
infile <- "ubicageo.CSV"
nomarchi<-paste0(ruta,infile)
ubicageo<-read.csv(nomarchi,stringsAsFactors = FALSE)
#unique(ubicageo$desc_ent)
Conc<-left_join(Conc, ubicageo, by = "ubica_geo")
#unique(ubicageo$desc_ent)
## Hogares
## Se crea una bandera para numerar los hogares
Conc$Nhog <- 1
#Conc<-filter(Conc,entidad==5)
## Ordena Conc de acuerdo a ing_cor, folioviv, foliohog
Conc<- orderBy (~+ing_cor+folioviv+foliohog, data=Conc)
## Suma todos los factores y guarda el valor en el vector tot_hogares
tot_hogares <- sum(Conc$factor)
## Se divide la suma de factores entre diez para sacar el tamaño del decil
## (se debe de truncar el resultado quitando los decimales)
tam_dec<-trunc(tot_hogares/10)
## Muestra la suma del factor en variable hogar
Conc$tam_dec=tam_dec
BD1 <- Conc
## Creación de deciles de hogares
## Se renombra la tabla concentrado a BD1
## Dentro de la tabla BD1 se crea la variable MAXT y se le asignan los
## valores que tiene el ing_cor.
BD1$MAXT <- BD1$ing_cor
## Se ordena de menor a mayor según la variable MAXT
BD1 <- BD1[with(BD1, order(rank(MAXT))),]
## Se aplica la función cumsum, suma acumulada a la variable factor
BD1$ACUMULA <- cumsum(BD1$factor)
## Entra a un ciclo donde genera los deciles 1 a 10
for(i in 1:9)
{
a1<-BD1[dim(BD1[BD1$ACUMULA<tam_dec*i,])[1]+1,]$factor
BD1<-rbind(BD1[1:(dim(BD1[BD1$ACUMULA<tam_dec*i,])[1]+1),],
BD1[(dim(BD1[BD1$ACUMULA<tam_dec*i,])[1]+1):dim(BD1[1])[1],])
b1<-tam_dec*i-BD1[dim(BD1[BD1$ACUMULA<tam_dec*i,])[1],]$ACUMULA
BD1[(dim(BD1[BD1$ACUMULA<tam_dec*i,])[1]+1),]$factor<-b1
BD1[(dim(BD1[BD1$ACUMULA<tam_dec*i,])[1]+2),]$factor<-(a1-b1)
}
BD1$ACUMULA2<-cumsum(BD1$factor)
BD1$DECIL <- 0
BD1[(BD1$ACUMULA2<=tam_dec),]$DECIL <- 1
for(i in 1:9)
{
BD1[((BD1$ACUMULA2>tam_dec*i)&(BD1$ACUMULA2<=tam_dec*(i+1))),]$DECIL <- (i+1)
}
#BD1[as.numeric(BD1$DECIL) %in% '0',]$DECIL <- 10
## Total de hogares
bd1col<-BD1[, -c(24,25,26,27)] # Selects all columns except 2 and 4
rows_in_df1_only <- setdiff(bd1col, Conc)
bd1col2<-left_join(Conc,BD1,by=c('folioviv','foliohog','ing_cor','factor'))
#BD1[as.numeric(BD1$DECIL) %in% '0',]$DECIL <- 10
## Total de hogares
x <- tapply(BD1$factor,BD1$Nhog,sum)
## Deciles
y <- tapply(BD1$factor,as.numeric(BD1$DECIL),sum)
## Se calcula el promedio de ingreso para el total y para cada uno de los deciles
ing_cormed_t <- tapply(BD1$factor*BD1$ing_cor,BD1$Nhog,sum)/x
ing_cormed_d <- tapply(BD1$factor*BD1$ing_cor,as.numeric(BD1$DECIL),sum)/y
## Selecciona las variables de alimentos
BD1al <- bd1col2[ c('publico.x','foraneo.x','adqui_vehi.x','mantenim.x','refaccion.x','combus.x')]
BD1al$max_col_base <- apply(BD1al, 1, function(x) names(x)[which.max(x)])
merged_BD1_inner <- merge(BD1, BD1al, by = 0)
BD1nvo <- merged_BD1_inner[c('folioviv', 'foliohog', 'ing_cor', 'factor', 'upm', 'est_dis','sexo_jefe','ubica_geo','gasto_mon','transporte','publico','foraneo','adqui_vehi','mantenim','refaccion','combus','DECIL','max_col_base')]
colnames(BD1nvo) <- c('folioviv', 'foliohog', 'ing_cor', 'factor', 'upm', 'est_dis','sexo_jefe','ubica_geo','gasto_mon','transporte','publico','foraneo','adqui_vehi','mantenim','refaccion','combus','DECIL','max_col_base')
BD1nvo$max_col_base <- gsub("\\.x", "", BD1nvo$max_col_base)
df_dplyr <-BD1nvo %>%
rowwise() %>%
mutate(selected_value = cur_data()[[max_col_base]]) %>%
ungroup()
BD1decil2 <- df_dplyr %>%
group_by(DECIL,max_col_base) %>%
summarise(gasrub=sum(selected_value))
BD1decil2 <-na.omit(BD1decil2)
BD1decil3 <- df_dplyr %>%
group_by(DECIL) %>%
summarise(gasali=sum(transporte))
BD1decil3 <-na.omit(BD1decil3)
BD1decil2<-left_join(BD1decil2,BD1decil3,by=c('DECIL'))
BD1decil2$porrubalim<-(BD1decil2$gasrub/BD1decil2$gasali)*100
BD1decil2 <-na.omit(BD1decil2)
BD1decil <- BD1nvo %>%
group_by(DECIL,max_col_base) %>%
summarise(n=n(),na.rm = TRUE)
BD1decil$max_col_base <- gsub("\\.x", "", BD1decil$max_col_base)
library(ggsci)
C<-ggplot(
subset(BD1decil, n>=5),
aes(x=as.numeric(DECIL),y=n,color=max_col_base)) +
# ggdist::geom_dots(BD1decil , mapping = aes(x = as.numeric(DECIL), y = n, fill = max_col_base),binwidth = NA,overflow = 'compress' ) +
geom_point(alpha=0.7) +
geom_smooth(method="loess",alpha=0.1,size=1,span=1)+
scale_x_continuous(breaks = breaks_width(1),limits=c(0,11))+
#scale_color_brewer(palette='Spectral')+
scale_color_ucscgb(name="rubro")+
#scale_color_simpsons()+
scale_y_continuous(labels = scales::label_comma())+
ggrepel::geom_text_repel(data = subset(BD1decil, n>=200) , mapping = aes(x = as.numeric(DECIL), y = n,label = str_wrap(round(n,0), 4)), lineheight = 0.9, seed = 999, segment.size = 0.1, size = 3.5, direction = "y",fontface = "bold")+
labs(
title = "Rubro gasto transporte máximo por decil de ingreso corriente",
subtitle = str_wrap("Conteo de viviendas"),
caption = "Fuente: ENIGH 2024 Gráfica:Claudia Guerrero"
) +
theme_minimal() +
theme(
legend.position = "right",
legend.key.height = unit(3, "lines"),
legend.key.width = unit(2, "lines"),
plot.background = element_rect(fill = "grey99", color = NA),
axis.title = element_blank(),
panel.grid.major.y = element_blank(),
axis.text.y = element_text(face = "bold", size = 12, color = "grey10"),
strip.text = element_text(size = 15, face = "bold"),
panel.spacing.y = unit(1.5, "lines"),
plot.margin = margin(10, 10, 10, 10),
plot.title = element_text(size = 18, face = "bold"),
plot.subtitle = element_text(size = 12, margin = margin(0, 0, 10, 0)),
plot.caption = element_text(margin = margin(10, 0, 0, 0))
)
C
map<-"inegi"
pal<-"purples"
if (!dir.exists(glue("images/{map}"))) {
dir.create(glue("images/{map}"))
}
outfile <- str_to_lower(glue("images/{map}/{map}_{pal}rubt.png"))
# Now that everything is assigned, save these objects so we
# can use then in our markup script
ggsave(outfile, width = 300, height = 300, units = "mm", dpi = "retina",limitsize = FALSE)
B<-ggplot(BD1decil2,
#subset(BD1decil2, n>=5),
aes(x=as.numeric(DECIL),y=porrubalim,color=max_col_base)) +
# ggdist::geom_dots(BD1decil , mapping = aes(x = as.numeric(DECIL), y = n, fill = max_col_base),binwidth = NA,overflow = 'compress' ) +
geom_point(alpha=0.7) +
geom_smooth(method="loess",alpha=0.1,size=1,span=1)+
scale_x_continuous(breaks = breaks_width(1),limits=c(0,11))+
#scale_color_brewer(palette='Spectral')+
scale_color_ucscgb(name="rubro")+
#scale_color_simpsons()+
scale_y_continuous(labels = scales::label_comma())+
ggrepel::geom_text_repel(data = BD1decil2 , mapping = aes(x = as.numeric(DECIL), y = porrubalim,label = str_wrap(round(porrubalim,2), 4)), lineheight = 0.9, seed = 999, segment.size = 0.1, size = 3.5, direction = "y",fontface = "bold")+
labs(
title = " % Rubro gasto transporte máximo por vivienda",
subtitle = str_wrap("por decil de ingreso corriente"),
caption = "Fuente: ENIGH 2024 Gráfica:Claudia Guerrero"
) +
theme_minimal() +
theme(
legend.position = "right",
legend.key.height = unit(3, "lines"),
legend.key.width = unit(2, "lines"),
plot.background = element_rect(fill = "grey99", color = NA),
axis.title = element_blank(),
panel.grid.major.y = element_blank(),
axis.text.y = element_text(face = "bold", size = 12, color = "grey10"),
strip.text = element_text(size = 15, face = "bold"),
panel.spacing.y = unit(1.5, "lines"),
plot.margin = margin(10, 10, 10, 10),
plot.title = element_text(size = 18, face = "bold"),
plot.subtitle = element_text(size = 12, margin = margin(0, 0, 10, 0)),
plot.caption = element_text(margin = margin(10, 0, 0, 0))
)
B
outfile <- str_to_lower(glue("images/{map}/{map}_{pal}porrubtp.png"))
# Now that everything is assigned, save these objects so we
# can use then in our markup script
ggsave(outfile, width = 300, height = 300, units = "mm", dpi = "retina",limitsize = FALSE)
En qué alimento se gasta más de acuerdo al DECIL proporción al ingreso corriente
## Abre la tabla concentradohogar
ruta<-"C:/Users/cguer/Documents/Claudia/Midropbox/Investigacion y escritos/karamanis/data/"
infile <- "concentradohogar.CSV"
nomarchi<-paste0(ruta,infile)
Conc<-read.csv(nomarchi,stringsAsFactors = FALSE)
#attach(Conc)
## Selecciona las variables de interés
Conc <- Conc [ c('folioviv', 'foliohog', 'ing_cor', 'factor', 'upm','est_dis','sexo_jefe','ubica_geo','gasto_mon','transporte','publico','foraneo','adqui_vehi','mantenim','refaccion','combus','comunica')]
## Se define la columna de los deciles
Numdec<-c('Total', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X')
Numdec<-c('I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X')
ruta<-"C:/Users/cguer/Documents/Claudia/Midropbox/Investigacion y escritos/karamanis/data/"
infile <- "ubicageo.CSV"
nomarchi<-paste0(ruta,infile)
ubicageo<-read.csv(nomarchi,stringsAsFactors = FALSE)
#unique(ubicageo$desc_ent)
Conc<-left_join(Conc, ubicageo, by = "ubica_geo")
#unique(ubicageo$desc_ent)
## Hogares
## Se crea una bandera para numerar los hogares
Conc$Nhog <- 1
#Conc<-filter(Conc,entidad==5)
## Ordena Conc de acuerdo a ing_cor, folioviv, foliohog
Conc<- orderBy (~+ing_cor+folioviv+foliohog, data=Conc)
## Suma todos los factores y guarda el valor en el vector tot_hogares
tot_hogares <- sum(Conc$factor)
## Se divide la suma de factores entre diez para sacar el tamaño del decil
## (se debe de truncar el resultado quitando los decimales)
tam_dec<-trunc(tot_hogares/10)
## Muestra la suma del factor en variable hogar
Conc$tam_dec=tam_dec
BD1 <- Conc
## Creación de deciles de hogares
## Se renombra la tabla concentrado a BD1
## Dentro de la tabla BD1 se crea la variable MAXT y se le asignan los
## valores que tiene el ing_cor.
BD1$MAXT <- BD1$ing_cor
## Se ordena de menor a mayor según la variable MAXT
BD1 <- BD1[with(BD1, order(rank(MAXT))),]
## Se aplica la función cumsum, suma acumulada a la variable factor
BD1$ACUMULA <- cumsum(BD1$factor)
## Entra a un ciclo donde genera los deciles 1 a 10
for(i in 1:9)
{
a1<-BD1[dim(BD1[BD1$ACUMULA<tam_dec*i,])[1]+1,]$factor
BD1<-rbind(BD1[1:(dim(BD1[BD1$ACUMULA<tam_dec*i,])[1]+1),],
BD1[(dim(BD1[BD1$ACUMULA<tam_dec*i,])[1]+1):dim(BD1[1])[1],])
b1<-tam_dec*i-BD1[dim(BD1[BD1$ACUMULA<tam_dec*i,])[1],]$ACUMULA
BD1[(dim(BD1[BD1$ACUMULA<tam_dec*i,])[1]+1),]$factor<-b1
BD1[(dim(BD1[BD1$ACUMULA<tam_dec*i,])[1]+2),]$factor<-(a1-b1)
}
BD1$ACUMULA2<-cumsum(BD1$factor)
BD1$DECIL <- 0
BD1[(BD1$ACUMULA2<=tam_dec),]$DECIL <- 1
for(i in 1:9)
{
BD1[((BD1$ACUMULA2>tam_dec*i)&(BD1$ACUMULA2<=tam_dec*(i+1))),]$DECIL <- (i+1)
}
#BD1[as.numeric(BD1$DECIL) %in% '0',]$DECIL <- 10
## Total de hogares
bd1col<-BD1[, -c(24,25,26,27)] # Selects all columns except 2 and 4
rows_in_df1_only <- setdiff(bd1col, Conc)
bd1col2<-left_join(Conc,BD1,by=c('folioviv','foliohog','ing_cor','factor'))
#BD1[as.numeric(BD1$DECIL) %in% '0',]$DECIL <- 10
## Total de hogares
x <- tapply(BD1$factor,BD1$Nhog,sum)
## Deciles
y <- tapply(BD1$factor,as.numeric(BD1$DECIL),sum)
## Se calcula el promedio de ingreso para el total y para cada uno de los deciles
ing_cormed_t <- tapply(BD1$factor*BD1$ing_cor,BD1$Nhog,sum)/x
ing_cormed_d <- tapply(BD1$factor*BD1$ing_cor,as.numeric(BD1$DECIL),sum)/y
## Selecciona las variables de alimentos
BD1al <- bd1col2[ c('publico.x','foraneo.x','adqui_vehi.x','mantenim.x','refaccion.x','combus.x')]
BD1al$max_col_base <- apply(BD1al, 1, function(x) names(x)[which.max(x)])
merged_BD1_inner <- merge(BD1, BD1al, by = 0)
BD1nvo <- merged_BD1_inner[c('folioviv', 'foliohog', 'ing_cor', 'factor', 'upm', 'est_dis','sexo_jefe','ubica_geo','gasto_mon','transporte','publico','foraneo','adqui_vehi','mantenim','refaccion','combus','DECIL','max_col_base')]
colnames(BD1nvo) <- c('folioviv', 'foliohog', 'ing_cor', 'factor', 'upm', 'est_dis','sexo_jefe','ubica_geo','gasto_mon','transporte','publico','foraneo','adqui_vehi','mantenim','refaccion','combus','DECIL','max_col_base')
BD1nvo$max_col_base <- gsub("\\.x", "", BD1nvo$max_col_base)
df_dplyr <-BD1nvo %>%
rowwise() %>%
mutate(selected_value = cur_data()[[max_col_base]]) %>%
ungroup()
BD1decil0 <- df_dplyr %>%
group_by(DECIL) %>%
summarise(ingcor=sum(ing_cor))
BD1decil1 <- df_dplyr %>%
group_by(DECIL) %>%
summarise(gasmon=sum(gasto_mon))
BD1decil2 <- df_dplyr %>%
group_by(DECIL) %>%
summarise(gastrans=sum(transporte))
BD1decil3 <- df_dplyr %>%
group_by(DECIL) %>%
summarise(gaspub=sum(publico))
BD1decil4 <- df_dplyr %>%
group_by(DECIL) %>%
summarise(gasfor=sum(foraneo))
BD1decil5 <- df_dplyr %>%
group_by(DECIL) %>%
summarise(gasadquiv=sum(adqui_vehi))
BD1decil6 <- df_dplyr %>%
group_by(DECIL) %>%
summarise(gasmantenim=sum(mantenim))
BD1decil7 <- df_dplyr %>%
group_by(DECIL) %>%
summarise(gasref=sum(refaccion))
BD1decil8 <- df_dplyr %>%
group_by(DECIL) %>%
summarise(gascomb=sum(combus))
BD1decil<-left_join(BD1decil0,BD1decil1,by=c('DECIL'))
BD1decil<-left_join(BD1decil,BD1decil2,by=c('DECIL'))
BD1decil<-left_join(BD1decil,BD1decil3,by=c('DECIL'))
BD1decil<-left_join(BD1decil,BD1decil4,by=c('DECIL'))
BD1decil<-left_join(BD1decil,BD1decil5,by=c('DECIL'))
BD1decil<-left_join(BD1decil,BD1decil6,by=c('DECIL'))
BD1decil<-left_join(BD1decil,BD1decil7,by=c('DECIL'))
BD1decil<-left_join(BD1decil,BD1decil8,by=c('DECIL'))
BD1decil$Numdec<-c('I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X')
#B
#BD1decil$factor<-BD1decil$ingcor
#titul<-"% Rubro e Ingresos corrientes"
#nomfil<-"gastostransingcor"
#C
#BD1decil$factor<-BD1decil$gasmon
#titul<-"% Rubro y gastos totales"
#nomfil<-"gastostranstotales"
#D
BD1decil$factor<-BD1decil$gastrans
titul<-"% Rubro y gastos transporte"
nomfil<-"gastostranstransporte"
BD1decil$porgastransporte<-(BD1decil$gastrans/BD1decil$factor)*100
BD1decil$porgaspublico<-(BD1decil$gaspub/BD1decil$factor)*100
BD1decil$porgasforaneo<-(BD1decil$gasfor/BD1decil$factor)*100
BD1decil$porgasadquivehiculo<-(BD1decil$gasadquiv/BD1decil$factor)*100
BD1decil$porgasmantenimiento<-(BD1decil$gasmantenim/BD1decil$factor)*100
BD1decil$porgasrefaccion<-(BD1decil$gasref/BD1decil$factor)*100
BD1decil$porgascombustible<-(BD1decil$gascomb/BD1decil$factor)*100
c(
"chr5" = "#FF0000", "chr8" = "#FF9900", "chr9" = "#FFCC00",
"chr12" = "#00FF00", "chr15" = "#6699FF", "chr20" = "#CC33FF",
"chr3" = "#99991E", "chrX" = "#999999", "chr6" = "#FF00CC",
"chr4" = "#CC0000", "chr7" = "#FFCCCC", "chr10" = "#FFFF00",
"chr11" = "#CCFF00", "chr13" = "#358000", "chr14" = "#0000CC",
"chr16" = "#99CCFF", "chr17" = "#00FFFF", "chr18" = "#CCFFFF",
"chr19" = "#9900CC", "chr21" = "#CC99FF", "chr1" = "#996600",
"chr2" = "#666600", "chr22" = "#666666", "chrY" = "#CCCCCC",
"chrUn" = "#79CC3D", "chrM" = "#CCCC99"
)
chr5 chr8 chr9 chr12 chr15 chr20 chr3 chrX
“#FF0000” “#FF9900” “#FFCC00” “#00FF00” “#6699FF” “#CC33FF” “#99991E” “#999999” chr6 chr4 chr7 chr10 chr11 chr13 chr14 chr16 “#FF00CC” “#CC0000” “#FFCCCC” “#FFFF00” “#CCFF00” “#358000” “#0000CC” “#99CCFF” chr17 chr18 chr19 chr21 chr1 chr2 chr22 chrY “#00FFFF” “#CCFFFF” “#9900CC” “#CC99FF” “#996600” “#666600” “#666666” “#CCCCCC” chrUn chrM “#79CC3D” “#CCCC99”
c(
"Matisse" = "#1F77B4", "Flamenco" = "#FF7F0E",
"ForestGreen" = "#2CA02C", "Punch" = "#D62728",
"Wisteria" = "#9467BD", "SpicyMix" = "#8C564B",
"Orchid" = "#E377C2", "Gray" = "#7F7F7F",
"KeyLimePie" = "#BCBD22", "Java" = "#17BECF"
)
Matisse Flamenco ForestGreen Punch Wisteria SpicyMix
“#1F77B4” “#FF7F0E” “#2CA02C” “#D62728” “#9467BD” “#8C564B” Orchid Gray KeyLimePie Java “#E377C2” “#7F7F7F” “#BCBD22” “#17BECF”
library(tidyr)
# Convert to long format
BD1decil_long1 <- BD1decil [, c(1, 2,3,13)] %>%
pivot_longer(cols = starts_with("porgastransporte"), names_to = "gastotipo", values_to = "y_value")
BD1decil_long2 <- BD1decil [, c(1, 2,3,14)] %>%
pivot_longer(cols = starts_with("porgaspublico"), names_to = "gastotipo", values_to = "y_value")
BD1decil_long3 <- BD1decil [, c(1, 2,3,15)] %>%
pivot_longer(cols = starts_with("porgasforaneo"), names_to = "gastotipo", values_to = "y_value")
BD1decil_long4 <- BD1decil [, c(1, 2,3,16)]%>%
pivot_longer(cols = starts_with("porgasadquivehiculo"), names_to = "gastotipo", values_to = "y_value")
BD1decil_long5 <- BD1decil [, c(1, 2,3,17)] %>%
pivot_longer(cols = starts_with("porgasmantenimiento"), names_to = "gastotipo", values_to = "y_value")
BD1decil_long6 <- BD1decil [, c(1, 2,3,18)]%>%
pivot_longer(cols = starts_with("porgasrefaccion"), names_to = "gastotipo", values_to = "y_value")
BD1decil_long7<- BD1decil [, c(1, 2,3,19)] %>%
pivot_longer(cols = starts_with("porgascombustible"), names_to = "gastotipo", values_to = "y_value")
BD1decil_long<- rbind(BD1decil_long1,BD1decil_long2,BD1decil_long3,BD1decil_long4,
BD1decil_long5,BD1decil_long6,BD1decil_long7)
D<-ggplot(BD1decil_long,
aes(x=as.numeric(DECIL),y=y_value,color=gastotipo)) +
geom_line(alpha=0.7) +
geom_smooth(method="loess",alpha=0.1,size=1,span=1)+
scale_x_continuous(breaks = breaks_width(1),limits=c(0,11))+
scale_y_continuous(labels = scales::label_comma())+
ggrepel::geom_text_repel(data = BD1decil_long , mapping = aes(x = as.numeric(DECIL), y = y_value,label = str_wrap(round(y_value,1), 4)), lineheight = 0.9, seed = 999, segment.size = 0.1, size = 3, direction = "y",fontface = "bold")+
scale_color_ucscgb(name="rubro")+
labs(
title =titul,
subtitle = str_wrap("por decil "),
caption = "Fuente: ENIGH 2024 Gráfica:Claudia Guerrero"
) +
theme_minimal() +
theme(
legend.position = "right",
legend.key.height = unit(3, "lines"),
legend.key.width = unit(2, "lines"),
plot.background = element_rect(fill = "grey99", color = NA),
axis.title = element_blank(),
panel.grid.major.y = element_blank(),
axis.text.y = element_text(face = "bold", size = 12, color = "grey10"),
strip.text = element_text(size = 15, face = "bold"),
panel.spacing.y = unit(1.5, "lines"),
plot.margin = margin(10, 10, 10, 10),
plot.title = element_text(size = 12, face = "bold"),
plot.subtitle = element_text(size = 12, margin = margin(0, 0, 10, 0)),
plot.caption = element_text(margin = margin(10, 0, 0, 0))
)
D
outfile <- str_to_lower(glue("images/{map}/{map}_{pal}",nomfil,".png"))
# Now that everything is assigned, save these objects so we
# can use then in our markup script
ggsave(outfile, width = 300, height = 300, units = "mm", dpi = "retina",limitsize = FALSE)
nomfil<-"gastosprporcion"
library(ggpubr)
ggarrange(B,C,D, ncol=3,common.legend = TRUE)
outfile <- str_to_lower(glue("images/{map}/{map}_{pal}",nomfil,".png"))
# Now that everything is assigned, save these objects so we
# can use then in our markup script
ggsave(outfile, width = 300, height = 300, units = "mm", dpi = "retina",limitsize = FALSE)