library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.1     ✔ purrr   1.0.1
## ✔ tibble  3.1.8     ✔ dplyr   1.1.0
## ✔ tidyr   1.3.0     ✔ stringr 1.5.0
## ✔ readr   2.1.4     ✔ forcats 1.0.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(tidyr)
library(dplyr)
library(ggplot2)
library(readxl)
dataf <- read_excel("~/Desktop/evidencia_df.xlsx")
conditions <- dataf$NOM_MUN %in% c("Apodaca", "San Nicolás de los Garza", "Santa Catarina", "Pesquería")

final_df <- dataf %>% 
              filter(conditions) %>% 
              select(NOM_MUN, NOM_LOC, AGEB, POBTOT, P_18YMAS, GRAPROES, POCUPADA, PEA, VPH_AUTOM)
pesqueria_df <- final_df %>% 
                filter(NOM_MUN == "Pesquería")
pesqueria_df
## # A tibble: 1,508 × 9
##    NOM_MUN   NOM_LOC          AGEB  POBTOT P_18Y…¹ GRAPR…² POCUP…³ PEA   VPH_A…⁴
##    <chr>     <chr>            <chr>  <dbl> <chr>   <chr>   <chr>   <chr> <chr>  
##  1 Pesquería Total del munic… 0     147624 95312   9.44    73717   75016 13109  
##  2 Pesquería Total de la loc… 0      48069 31674   9.7799… 24217   24667 4360   
##  3 Pesquería Total AGEB urba… 50       836 647     9.02    470     479   121    
##  4 Pesquería Pesquería        50         3 *       *       *       *     *      
##  5 Pesquería Pesquería        50         0 0       0       0       0     0      
##  6 Pesquería Pesquería        50        63 48      8.75    31      32    8      
##  7 Pesquería Pesquería        50         0 0       0       0       0     0      
##  8 Pesquería Pesquería        50        75 60      9.18    51      52    12     
##  9 Pesquería Pesquería        50        63 48      9.26    36      36    12     
## 10 Pesquería Pesquería        50        47 39      8.7100… 28      28    7      
## # … with 1,498 more rows, and abbreviated variable names ¹​P_18YMAS, ²​GRAPROES,
## #   ³​POCUPADA, ⁴​VPH_AUTOM
# GRAFICA 1
graf1 <- dataf %>% filter(NOM_MUN == "Pesquería" & NOM_LOC == "Pesquería") %>% select(NOM_MUN,NOM_LOC,AGEB,PEA) %>% filter(if_all(everything(),~ . != "N/D" ))

promedio <- graf1 %>%
  group_by(AGEB) %>%
  summarize(PromedioPob = mean(as.numeric(PEA))) %>%
  arrange(desc(PromedioPob))
## Warning: There were 7 warnings in `summarize()`.
## The first warning was:
## ℹ In argument: `PromedioPob = mean(as.numeric(PEA))`.
## ℹ In group 1: `AGEB = "007A"`.
## Caused by warning in `mean()`:
## ! NAs introduced by coercion
## ℹ Run ]8;;ide:run:dplyr::last_dplyr_warnings()dplyr::last_dplyr_warnings()]8;; to see the 6 remaining warnings.
head <- head(promedio, 10)

ggplot(head, aes(x = AGEB, y = PromedioPob, fill = "blue")) +
  geom_bar(stat = "identity") +
  labs(title = "Promedio de Población Económicamente Activa por AGEB", subtitle = "Top 10 promedios de AGEB en Pesquería", x = "AGEB", y = "Promedio de Polación") +
  scale_fill_manual(values = "blue")

#Gráfica 2
graf2 <- dataf %>% filter(NOM_MUN == "Pesquería" & NOM_LOC == "Pesquería") %>% select(NOM_MUN,NOM_LOC,PEA,VPH_AUTOM) %>% filter(if_all(everything(),~ . != "N/D" ))

plot(graf2$VPH_AUTOM, graf1$PEA, xlab="Viviendas particulares que disponen de un automóvil o camioneta", ylab = "Población Económicamente Activa", main = "Correlación en entre la PEA y las viviendas con automóviles")
## Warning in xy.coords(x, y, xlabel, ylabel, log): NAs introduced by coercion

## Warning in xy.coords(x, y, xlabel, ylabel, log): NAs introduced by coercion

#Gráfica 3
municipios <- c("Apodaca", "San Nicolás de los Garza", "Santa Catarina", "Pesquería")
municipios_data <- dataf[dataf$NOM_MUN %in% municipios, ]

municipios_mean <- aggregate(POBTOT ~ NOM_MUN, data = municipios_data, FUN = mean)

barplot(municipios_mean$POBTOT, names.arg = municipios_mean$NOM_MUN, xlab = "Municipio", ylab = "Promedio de Población", main = "Población Por Municipio", col = "blue")