Ingreso

Gasto

---
title: "Día 4 #30díasdegráficos (gráficos con facetas). Ingreso y Gasto por regiones. Perú 2018 (ESP)"
author: "Rubén F. Bustillo"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    source_code: embed
    vertical_layout: fill
    theme: spacelab
---



```{r setup, include=FALSE}


# PACKAGES / LIBRARIES:

library(flexdashboard)
library(foreign)
library(tidyverse)
library(readxl)
library(extrafont)
loadfonts(device = "win")

options(scipen=999)

# descargamos la sumaria de 2018 en formato STATA

sumaria<-read.dta("C:/Users/Usuario/Desktop/Para_paper_peru/DATOS_PERU/ENHAO/2018_ENAHO_sumaria/634-Modulo34/sumaria-2018-12g.dta")

# departamentos por ubigeo

ubigeo_departamentos <- read_excel("C:/Users/Usuario/Desktop/Para_paper_peru/DATOS_PERU/REGIONAL/POR_REGION/UBIGEO/ubigeo_departamentos.xlsx")



sumaria_df <- sumaria %>%
  select(inghog2d, gashog2d, mieperho, factor07, ubigeo) %>%
  mutate(gpcm = gashog2d/(12*mieperho), facpob =factor07*mieperho) %>%
  mutate(ipcm = inghog2d/12*mieperho)


sumaria_df$ubigeo <- substr(sumaria$ubigeo, 1, 2)


# inner join

sumaria_df <- inner_join(sumaria_df, ubigeo_departamentos)


```

Ingreso
====================================================================================

```{r, out.width= "100%", fig.width=10, fig.height=5}

ggplot(sumaria_df, aes(x= Departamento,  y = inghog2d, fill = Geo)) +
  geom_point(position = "jitter", alpha = 0.1, color = "grey30") +
  geom_boxplot() +
  scale_fill_manual(values = c("steelblue1", "limegreen", "orange")) +
  coord_flip() +
  #scale_y_continuous(labels = scales::comma) +
  scale_y_continuous(trans = 'log10', labels = scales::comma) +
  facet_wrap(~Geo, scale = "free_y") +
  labs(title = "Ingreso Neto Total Anual por habitante",
       subtitle = "(Eje de abscisas en escala logarítmica)",
       caption = "Fuente: Encuesta Nacional de Hogares (ENAHO)",
       y = "",
       x = "") +
  theme_minimal()  +
  theme(legend.position = "none",
        plot.title=element_text(size = 14, 
                                family= "Modern No. 20",
                                hjust = 0.5),
        plot.subtitle = element_text(size = 10, 
                                     family = "Modern No. 20",
                                     hjust = 0.5),
        text = element_text(size = 10, 
                            family="Modern No. 20"),
        axis.line.x = element_line(size = 1.1, 
                                   colour = "black"),
        axis.line.y = element_line(size = 1.1, 
                                   colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.text.x=element_text(colour="black", 
                                 size = 8),
        axis.text.y=element_text(colour="black", 
                                 size = 8))

```




Gasto
=======================================================================================

```{r, out.width= "100%", fig.width=10, fig.height=5}

ggplot(sumaria_df, aes(x= Departamento,  y = gashog2d, fill = Geo)) +
  geom_point(position = "jitter", alpha = 0.1, color = "grey30") +
  geom_boxplot() +
  scale_fill_manual(values = c("steelblue1", "limegreen", "orange")) +
  coord_flip() +
  #scale_y_continuous(labels = scales::comma) +
  scale_y_continuous(trans = 'log10', labels = scales::comma) +
  facet_wrap(~Geo, scale = "free_y") +
  labs(title = "Gasto Total Bruto Anual por habitante",
       subtitle = "(Eje de abscisas en escala logarítmica)",
       caption = "Fuente: Encuesta Nacional de Hogares (ENAHO)",
       y = "",
       x = "") +
  theme_minimal()  +
  theme(legend.position = "none",
        plot.title=element_text(size = 14, 
                                family= "Modern No. 20",
                                hjust = 0.5),
        plot.subtitle = element_text(size = 10, 
                                     family = "Modern No. 20",
                                     hjust = 0.5),
        text = element_text(size = 10, 
                            family="Modern No. 20"),
        axis.line.x = element_line(size = 1.1, 
                                   colour = "black"),
        axis.line.y = element_line(size = 1.1, 
                                   colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.text.x=element_text(colour="black", 
                                 size = 8),
        axis.text.y=element_text(colour="black", 
                                 size = 8))

```