library(tidyverse) # dplyr, tidyr, ggplot2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.3 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readxl) # leer Excel
library(scales) # formato de ejes
##
## Attaching package: 'scales'
##
## The following object is masked from 'package:purrr':
##
## discard
##
## The following object is masked from 'package:readr':
##
## col_factor
library(GGally) # matriz de dispersión
library(ggplot2)
library(ggrepel)
library(dplyr)
data <- read_excel("RETO_Dinámica_DB.xlsx", sheet=2)
paleta <- c(
"#061A40",
"#0353A4",
"#0077B6",
"#5B4B9E",
"#B7A9E0",
"#9AAFD1"
)
datos_graf1 <- data %>%
select(Estado, PIB_peso_nacional, PIB_peso_regional) %>%
pivot_longer(
cols = c(PIB_peso_nacional, PIB_peso_regional),
names_to = "Indicador",
values_to = "Porcentaje"
) %>%
mutate(
Indicador = recode(
Indicador,
PIB_peso_nacional = "PIB nacional",
PIB_peso_regional = "PIB regional"
)
)
ggplot(datos_graf1,
aes(x = reorder(Estado, Porcentaje),
y = Porcentaje,
fill = Indicador)) +
geom_col(position = position_dodge(width = 0.7),
width = 0.65) +
coord_flip() +
scale_fill_manual(values = c(
"PIB nacional" = "#0353A4",
"PIB regional" = "#5B4B9E"
)) +
scale_y_continuous(labels = label_percent(scale = 1)) +
labs(
title = "Participación del PIB por estado",
subtitle = "Peso respecto al PIB nacional y al PIB regional",
x = "",
y = "Porcentaje",
fill = ""
) +
theme_minimal(base_size = 13) +
theme(
plot.title = element_text(face = "bold", size = 18, color = "#061A40"),
plot.subtitle = element_text(size = 12),
legend.position = "top",
legend.text = element_text(size = 11),
panel.grid.major.y = element_blank(),
panel.grid.minor = element_blank(),
axis.text = element_text(color = "#061A40")
)
ggplot(data,
aes(x = PIB_peso_nacional,
y = PIB_peso_regional)) +
geom_point(
size = 3.5,
shape = 21,
fill = "#0353A4",
color = "white",
stroke = 0.6
) +
geom_text_repel(
aes(label = Estado),
size = 3.6,
color = "#061A40",
family = "sans",
box.padding = 0.45,
point.padding = 0.3,
max.overlaps = Inf,
segment.color = "grey60",
segment.size = 0.3
) +
scale_x_continuous(labels = label_percent(scale = 100)) +
scale_y_continuous(labels = label_percent(scale = 100)) +
labs(
x = "Participación en el PIB nacional (%)",
y = "Participación en el PIB regional (%)"
) +
theme_minimal(base_size = 13) +
theme(
panel.grid.minor = element_blank(),
panel.grid.major = element_line(color = "grey90", linewidth = 0.3),
axis.title = element_text(color = "#061A40"),
axis.text = element_text(color = "#333333"),
axis.ticks = element_blank(),
plot.margin = margin(15, 20, 15, 15)
)
# Gráfica 2 demografÃa
ggplot(data,
aes(x = reorder(Estado, poblacion),
y = poblacion)) +
geom_col(fill = "#0353A4", width = 0.7) +
geom_text(
aes(label = comma(poblacion)),
hjust = -0.15,
size = 3.6,
color = "#061A40",
) +
coord_flip(clip = "off") +
scale_y_continuous(
labels = label_comma(),
expand = expansion(mult = c(0, .18))
) +
labs(
x = "",
y = "Habitantes"
) +
theme_minimal(base_size = 13) +
theme(
panel.grid.major.y = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major.x = element_line(color = "grey90", linewidth = 0.3),
axis.text.y = element_text(color = "#061A40", size = 11),
axis.text.x = element_text(color = "#333333"),
axis.title.x = element_text(color = "#061A40"),
axis.ticks = element_blank(),
plot.margin = margin(15, 30, 15, 15)
)
# Gráfico 3 PIB per capita
promedio_nacional <- mean(data$PIB_pcc, na.rm = TRUE)
ggplot(data,
aes(x = reorder(Estado, PIB_pcc), y = PIB_pcc)) +
geom_col(fill = "#0353A4", width = 0.7) +
geom_text(
aes(label = comma(PIB_pcc, prefix = "$")),
hjust = -0.1,
size = 3.3,
color = "#061A40"
) +
coord_flip(clip = "off") +
scale_y_continuous(
labels = label_comma(prefix = "$"),
expand = expansion(mult = c(0, .18))
) +
labs(
x = "",
y = "PIB per cápita"
) +
theme_minimal(base_size = 13) +
theme(
panel.grid.major.y = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major.x = element_line(color = "grey90", linewidth = 0.3),
axis.text.y = element_text(color = "#061A40", size = 11),
axis.text.x = element_text(color = "#333333"),
axis.title.x = element_text(color = "#061A40"),
axis.ticks = element_blank(),
plot.margin = margin(15, 30, 15, 15)
)
data_stack <- data %>%
mutate(
poblacion_15mas = PEA / PEA_part,
PEA_inactiva = poblacion_15mas - PEA,
PEA_inactiva_part = 1 - PEA_part
) %>%
select(Estado, PEA_part, PEA_inactiva_part, PEA, PEA_inactiva) %>%
pivot_longer(
cols = c(PEA_part, PEA_inactiva_part),
names_to = "categoria", values_to = "valor"
) %>%
mutate(
absoluto = if_else(categoria == "PEA_part", PEA, PEA_inactiva)
)
ggplot(data_stack,
aes(x = reorder(Estado, valor), y = valor, fill = categoria)) +
geom_col(width = 0.7) +
geom_text(
aes(label = paste0(label_percent(accuracy = 0.1)(valor),
"\n(", comma(absoluto, accuracy = 1), ")")),
position = position_stack(vjust = 0.5),
size = 2.9,
lineheight = 0.85,
color = "white"
) +
coord_flip() +
scale_y_continuous(labels = label_percent()) +
scale_fill_manual(
values = c("PEA_part" = "#0353A4", "PEA_inactiva_part" = "#9AAFD1"),
labels = c("PEA_part" = "Participación económica (PEA)", "PEA_inactiva_part" = "Inactiva")
) +
labs(x = "", y = "", fill = "") +
theme_minimal(base_size = 13) +
theme(
panel.grid = element_blank(),
axis.text.y = element_text(color = "#061A40"),
legend.position = "top"
)
data_stack_formal <- data %>%
mutate(
PEA_informal_part = 1 - PEA_formal, # complemento en proporción 0-1
informal = PEA - formal # número absoluto de informales
) %>%
select(Estado, PEA_formal, PEA_informal_part, formal, informal) %>%
pivot_longer(
cols = c(PEA_formal, PEA_informal_part),
names_to = "categoria", values_to = "valor"
) %>%
mutate(
absoluto = if_else(categoria == "PEA_formal", formal, informal)
)
ggplot(data_stack_formal,
aes(x = reorder(Estado, valor), y = valor, fill = categoria)) +
geom_col(width = 0.7) +
geom_text(
aes(label = paste0(label_percent(accuracy = 0.1)(valor),
"\n(", comma(absoluto, accuracy = 1), ")")),
position = position_stack(vjust = 0.5),
size = 2.9,
lineheight = 0.85,
color = "white"
) +
coord_flip() +
scale_y_continuous(labels = label_percent()) +
scale_fill_manual(
values = c("PEA_formal" = "#0353A4", "PEA_informal_part" = "#9AAFD1"),
labels = c("PEA_formal" = "Empleo formal", "PEA_informal_part" = "Empleo informal")
) +
labs(x = "", y = "", fill = "") +
theme_minimal(base_size = 13) +
theme(
panel.grid = element_blank(),
axis.text.y = element_text(color = "#061A40"),
legend.position = "top"
)
datos_vab <- data %>%
select(
Estado,
VAB_primario_region,
VAB_secundario_region,
VAB_terciario_region
) %>%
pivot_longer(
cols = starts_with("VAB_"),
names_to = "Sector",
values_to = "Aporte"
) %>%
mutate(
Sector = recode(
Sector,
VAB_primario_region = "Primario",
VAB_secundario_region = "Secundario",
VAB_terciario_region = "Terciario"
)
)
ggplot(datos_vab,
aes(x = Sector,
y = reorder(Estado, Aporte),
fill = Aporte)) +
geom_tile(color = "white", linewidth = 1) +
geom_text(
aes(label = label_percent(accuracy = 1)(Aporte)),
color = "#061A40",
size = 4
)+
scale_fill_gradient(
low = "#9AAFD1",
high = "#0353A4",
labels = label_percent()
)+
labs(
x = "",
y = "",
fill = "Aporte (%)"
) +
theme_minimal() +
theme(
panel.grid = element_blank(),
axis.text.x = element_text(face = "bold", color = "#061A40"),
axis.text.y = element_text(color = "#061A40"),
plot.title = element_text(face = "bold", color = "#061A40")
)
names(data)
## [1] "Estado"
## [2] "PIB"
## [3] "PIB_peso_nacional"
## [4] "PIB_peso_regional"
## [5] "poblacion"
## [6] "PIB_pcc"
## [7] "PEA"
## [8] "PEA_part"
## [9] "formal"
## [10] "PEA_formal"
## [11] "VAB_total"
## [12] "VAB_primario"
## [13] "VAB_primario_region"
## [14] "VAB_secundario"
## [15] "VAB_secundario_region"
## [16] "VAB_terciario"
## [17] "VAB_terciario_region"
## [18] "VAB_primario_nacional"
## [19] "VAB_secundario_nacional"
## [20] "VAB_terciario_nacional"
## [21] "VAB_comercio_por_mayor"
## [22] "VAB_cpmay_regional"
## [23] "VAB_cpmay_nacional"
## [24] "VAB_comercio_por_menor"
## [25] "VAB_cpmen_regional"
## [26] "VAB_cpmen_nacional"
## [27] "VAB_servicios_inmobiliarios"
## [28] "VAB_si_regional"
## [29] "VAB_si_nacional"
## [30] "VAB_transporte_correos_almacenamientos"
## [31] "VAB_tca_regional"
## [32] "VAB_tca_nacional"
## [33] "VAB_serv_financieros"
## [34] "VAB_sf_regional"
## [35] "VAB_sf_nacional"
## [36] "VAB_manufactura"
## [37] "VAB_man_regional"
## [38] "VAB_man_nacional"
## [39] "VAB_agricultura"
## [40] "VAB_agr_regional"
## [41] "VAB_agr_nacional"
## [42] "VAB_energia"
## [43] "VAB_ener_regional"
## [44] "VAB_ener_nacional"
## [45] "VAB_mineria"
## [46] "VAB_min_regional"
## [47] "VAB_min_nacional"
## [48] "VAB_turismo"
## [49] "VAB_tur_regional"
## [50] "VAB_tur_nacional"
datos_subsectores <- data %>%
select(
Estado,
VAB_cpmay_regional,
VAB_cpmen_regional,
VAB_si_regional,
VAB_tca_regional,
VAB_sf_regional,
VAB_man_regional,
VAB_agr_regional,
VAB_ener_regional,
VAB_min_regional,
VAB_tur_regional
) %>%
pivot_longer(
cols = -Estado,
names_to = "Subsector",
values_to = "Aporte"
) %>%
mutate(
Subsector = recode(
Subsector,
VAB_cpmay_regional = "Comercio mayor",
VAB_cpmen_regional = "Comercio menor",
VAB_si_regional = "Servicios inmobiliarios",
VAB_tca_regional = "Transporte",
VAB_sf_regional = "Servicios financieros",
VAB_man_regional = "Manufactura",
VAB_agr_regional = "Agricultura",
VAB_ener_regional = "EnergÃa",
VAB_min_regional = "MinerÃa",
VAB_tur_regional = "Turismo"
)
)
ggplot(datos_subsectores,
aes(x = Subsector,
y = Estado,
fill = Aporte)) +
geom_tile(color = "white", linewidth = 1) +
geom_text(
aes(label = label_percent(accuracy = 1)(Aporte)),
color = "#061A40",
size = 3.5
) +
scale_fill_gradient(
low = "#9AAFD1",
high = "#0353A4",
labels = label_percent()
) +
labs(
x = "",
y = "",
fill = "Aporte"
) +
theme_minimal() +
theme(
panel.grid = element_blank(),
axis.text.x = element_text(
angle = 45,
hjust = 1,
face = "bold",
color = "#061A40"
),
axis.text.y = element_text(color = "#061A40"),
plot.title = element_text(face = "bold", color = "#061A40"),
legend.position = "right"
)
datos_subsectores_nac <- data %>%
select(
Estado,
VAB_cpmay_nacional,
VAB_cpmen_nacional,
VAB_si_nacional,
VAB_tca_nacional,
VAB_sf_nacional,
VAB_man_nacional,
VAB_agr_nacional,
VAB_ener_nacional,
VAB_min_nacional,
VAB_tur_nacional
) %>%
pivot_longer(
cols = -Estado,
names_to = "Subsector",
values_to = "Aporte"
) %>%
mutate(
Subsector = recode(
Subsector,
VAB_cpmay_nacional = "Comercio mayor",
VAB_cpmen_nacional = "Comercio menor",
VAB_si_nacional = "Servicios inmobiliarios",
VAB_tca_nacional = "Transporte",
VAB_sf_nacional = "Servicios financieros",
VAB_man_nacional = "Manufactura",
VAB_agr_nacional = "Agricultura",
VAB_ener_nacional = "EnergÃa",
VAB_min_nacional = "MinerÃa",
VAB_tur_nacional = "Turismo"
)
)
ggplot(datos_subsectores_nac,
aes(x = Subsector,
y = Estado,
fill = Aporte)) +
geom_tile(color = "white", linewidth = 1) +
geom_text(
aes(label = label_percent(accuracy = 0.1)(Aporte)),
color = "#061A40",
size = 3.5
) +
scale_fill_gradient(
low = "#9AAFD1",
high = "#0353A4",
labels = label_percent()
) +
labs(
x = "",
y = "",
fill = "Aporte"
) +
theme_minimal() +
theme(
panel.grid = element_blank(),
axis.text.x = element_text(
angle = 45,
hjust = 1,
face = "bold",
color = "#061A40"
),
axis.text.y = element_text(color = "#061A40"),
plot.title = element_text(face = "bold", color = "#061A40"),
legend.position = "right"
)