Contexto censo 2020 microdtatos población por AGEB
https://www.inegi.org.mx/programas/ccpv/2020/default.html#microdatos
Lectura de datos Selección de variables Conversión numérica Creación de clave geográfica alfanumérica
# contexto censo 2020 microdatos poblaciòn por AGEB
#
ruta<-"C:/Users/cguer/Documents/Claudia/Midropbox/Investigacion y escritos/karamanis/data/"
infile <- "RESAGEBURB_05CSV20.csv"
censotorageb<- read.csv(paste0(ruta,infile),header=TRUE, sep=",",encoding="UTF-8")
censotorageb <-subset(censotorageb,censotorageb$NOM_LOC %in% c("Total AGEB urbana"))
census_2020 <- censotorageb %>%
select(ENTIDAD,NOM_ENT,MUN,NOM_MUN,LOC,AGEB,POBTOT,POBFEM,POBMAS,POB65_MAS) %>%
mutate(POBFEM = as.numeric(POBFEM),POBMAS=as.numeric(POBMAS),POB65_MAS=as.numeric(POB65_MAS))
# Se convierte en un dato alfanumérico de dos digito como "01", "02",..."32"
codent<-substr(census_2020$ENTIDAD,1,2)
codent<-sprintf("%02d%s", as.numeric(gsub("[^0-9]+", "", codent)), gsub("[0-9]+", "", codent))
codmun<-substr(census_2020$MUN,1,3)
codmun<-sprintf("%03d%s", as.numeric(gsub("[^0-9]+", "", codmun)), gsub("[0-9]+", "", codmun))
codageb<-census_2020$AGEB
codageb<-substr(census_2020$AGEB,1,4)
codageb<-sprintf("%04s",gsub(" ", "0", codageb))
codageb<-gsub(" ", "0", codageb)
codloc<-substr(census_2020$LOC,1,4)
codloc<-sprintf("%04d%s", as.numeric(gsub("[^0-9]+", "", codloc)), gsub("[0-9]+", "", codloc))
cvegeo<-paste0(codent,codmun,codloc,codageb)
census_2020$CVEGEO<-cvegeo
Contexto geográfico de AGEBS
Descompresión y almacenamiento de Shpfile de AGEBS
ruta<-"C:/Users/cguer/Documents/Claudia/Midropbox/Investigacion y escritos/karamanis/data/conjunto_de_datos/"
infile <- "05a.shp"
nomarchi<-as.filename(paste0(ruta,infile))
nomfil<-"05a"
datageo<- read_sf(nomarchi,options = "ENCODING=latin1")
datageo<-st_transform(datageo, CRS("+init=epsg:4326 +proj=longlat
+ +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0"))
#datageo_geom <- st_geometry(datageo)
st_crs(datageo) <-4326
datageo <-subset(datageo,datageo$CVE_MUN %in% c("035"))
ggplot(datageo) +
geom_sf()
Unión de datos censales con datos geográficos a partir de la clave geográfica del AGEB
basemapa<-merge(datageo,census_2020, by="CVEGEO", all.x =TRUE)
st_crs(basemapa) <-4326
#basemapa <-subset(basemapa,basemapa$CVE_MUN %in% c("035"))
ggplot() +
geom_sf(data = basemapa, linewidth = .5, color="black",aes(fill = POBTOT)) +
scale_fill_scico(palette = "oslo", direction = -1) +
labs(
#title = "Torreón Coahuila",
subtitle="Población por AGEB",
#caption = "autora: @guerreroclaudia"
) +
#coord_sf(xlim=c(-103.337,-103.3261),ylim=(c(25.097,25.108))) +
theme_void() +
theme(
legend.position = "bottom",
legend.key.width = unit(3, "line"),
legend.key.height = unit(0.5, "line"),
plot.background = element_rect(fill = NA, color = NA),
plot.title = element_text(size = 30, hjust = 0.5, face = "bold"),
plot.subtitle = element_text(size = 16, hjust = 0.5),
plot.caption = element_text(size = 10, hjust = 0.5, margin = margin(20, 0, 0, 0))
)
Incorporación de la zona municipal de Torreón en el polÃgono urbano Uso de consultas para la lectura filtrada del Shapefile del contexto estatal filtrado municipal
ruta<-"C:/Users/cguer/Documents/Claudia/Midropbox/Investigacion y escritos/karamanis/data/conjunto_de_datos/"
infile <- "05mun.shp"
nomarchi<-as.filename(paste0(ruta,infile))
nomfil<-"05mun"
#a<-"SELECT * FROM \"00a\" WHERE CVE_ENT = '05'"
a<-"SELECT * FROM \"05mun\" WHERE CVE_ENT = '05' AND CVE_MUN='035'"
#a<-"SELECT * FROM \"00mun\" WHERE CVEGEO = '05017' OR CVEGEO = '05033' OR CVEGEO = '05035' OR CVEGEO = '10007' OR CVEGEO = '10012'"
#a<-"SELECT * FROM \"00a\" WHERE CVEGEO = '05035'
datageot<- read_sf(nomarchi,options = "ENCODING=latin1",query=a)
datageot= st_transform(datageot, crs = 4326)
datageo_geom <- st_geometry(datageot)
polygon = st_cast(datageo_geom, "POLYGON")
plot(polygon)
tor<-polygon[2]
library(scico)
ggplot() +
geom_sf(data = tor, linewidth = 1, fill = NA, color = "black") +
geom_sf(data = basemapa, linewidth = .5, color="black",aes(fill = POBTOT)) +
scale_fill_scico(palette = "oslo", direction = -1) +
labs(
#title = "Torreón Coahuila",
subtitle="Población por AGEB",
#caption = "autora: @guerreroclaudia"
) +
#coord_sf(xlim=c(-103.337,-103.3261),ylim=(c(25.097,25.108))) +
theme_void() +
theme(
legend.position = "bottom",
legend.key.width = unit(3, "line"),
legend.key.height = unit(0.5, "line"),
plot.background = element_rect(fill = NA, color = NA),
plot.title = element_text(size = 30, hjust = 0.5, face = "bold"),
plot.subtitle = element_text(size = 16, hjust = 0.5),
plot.caption = element_text(size = 10, hjust = 0.5, margin = margin(20, 0, 0, 0))
)
Incorporación de contexto vial como referencia local de ubicación uso de librerÃa OSMDATA Elección territorial del polÃgono de Torreón urbano (polygon[2])
city = "Torreón Coahuila"
viasosm <- opq(city) %>%
add_osm_feature(key = "highway") %>%
osmdata_sf()
viasosm_sf <- viasosm$osm_lines
st_crs(viasosm_sf) <-4326
recorteviasosm_sf <-viasosm_sf[tor, ]
recorteviasosm_sf <-subset(recorteviasosm_sf,recorteviasosm_sf$highway %in% c("primary","secondary"))
ggplot() +
geom_sf(data = tor, linewidth = 1, fill = NA, color = "black") +
geom_sf(data = basemapa, linewidth = .5, color="black",aes(fill = POBTOT)) +
scale_fill_scico(palette = "oslo", direction = -1) +
geom_sf(data = recorteviasosm_sf, linewidth = .5,fill = NA, color = "orange") +
labs(
#title = "Torreón Coahuila",
subtitle="Población por AGEB",
#caption = "autora: @guerreroclaudia"
) +
#coord_sf(xlim=c(-103.337,-103.3261),ylim=(c(25.097,25.108))) +
theme_void() +
theme(
legend.position = "bottom",
legend.key.width = unit(3, "line"),
legend.key.height = unit(0.5, "line"),
plot.background = element_rect(fill = NA, color = NA),
plot.title = element_text(size = 30, hjust = 0.5, face = "bold"),
plot.subtitle = element_text(size = 16, hjust = 0.5),
plot.caption = element_text(size = 10, hjust = 0.5, margin = margin(20, 0, 0, 0))
)
Cartograma por AGEG población total
Cambio de proyección para la elaboración del cartograma
En un cartograma el tamaño de la región geográfica se deforma proporcionalmente al valor de la variable para transmitir información, sustituyendo el área geográfica.
Identificación proyección en https://sumapa.com/crsxpais.cfm
muni_pop <- basemapa %>%
select(POBTOT, geometry)
muni_pop<-st_transform(muni_pop, CRS("+init=epsg:4326 +proj=longlat
+ +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0"))
st_crs(muni_pop) <-4326
sf::sf_use_s2(FALSE)
#https://sumapa.com/crsxpais.cfm
tor_sf_proj = st_transform(muni_pop, crs = 32664)
tor_cartogram <- cartogram_cont(tor_sf_proj, "POBTOT")
ggplot(tor_cartogram) +
geom_sf()
a<-ggplot() +
geom_sf(data = tor, size = 0.15, fill = NA, color = "black") +
geom_sf(data = tor_cartogram, linewidth = .5, color="black",aes(fill = POBTOT)) +
scale_fill_scico(palette = "oslo", direction = -1, end = 0.975, labels = function(x) format(x, big.mark = " ", trim = TRUE), name = NULL) +
geom_sf(data = recorteviasosm_sf, linewidth = .5,fill = NA, color = "orange") +
labs(
#title = "Torreón Coahuila",
subtitle="Población por AGEB",
#caption = "autora: @guerreroclaudia"
) +
#coord_sf(xlim=c(-103.337,-103.3261),ylim=(c(25.097,25.108))) +
theme_void() +
theme(
legend.position = "bottom",
legend.key.width = unit(3, "line"),
legend.key.height = unit(0.5, "line"),
plot.background = element_rect(fill = NA, color = NA),
plot.title = element_text(size = 30, hjust = 0.5, face = "bold"),
plot.subtitle = element_text(size = 16, hjust = 0.5),
plot.caption = element_text(size = 10, hjust = 0.5, margin = margin(20, 0, 0, 0))
)
a
Comparación de la población de 65 y más por AGEB sin y en Cartograma
Combinación de gráficos (plots)
b<-ggplot() +
geom_sf(data = tor, linewidth = 1, fill = NA, color = "black") +
geom_sf(data = basemapa, linewidth = .5, color="black",aes(fill = POB65_MAS)) +
scale_fill_scico(palette = "lapaz", direction = -1, end = 0.975, labels = function(x) format(x, big.mark = " ", trim = TRUE), name = NULL) +
geom_sf(data = recorteviasosm_sf, linewidth = .2,fill = NA, color = "brown") +
labs(
#title = "Torreón Coahuila",
subtitle="Población de 65 y más por AGEB",
#caption = "autora: @guerreroclaudia"
) +
#coord_sf(xlim=c(-103.337,-103.3261),ylim=(c(25.097,25.108))) +
theme_void() +
theme(
legend.position = "bottom",
legend.key.width = unit(3, "line"),
legend.key.height = unit(0.5, "line"),
plot.background = element_rect(fill = NA, color = NA),
plot.title = element_text(size = 30, hjust = 0.5, face = "bold"),
plot.subtitle = element_text(size = 16, hjust = 0.5),
plot.caption = element_text(size = 10, hjust = 0.5, margin = margin(20, 0, 0, 0))
)
muni_pop <- basemapa %>%
select(POB65_MAS, geometry)
muni_pop<-st_transform(muni_pop, CRS("+init=epsg:4326 +proj=longlat
+ +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0"))
st_crs(muni_pop) <-4326
sf::sf_use_s2(FALSE)
#https://sumapa.com/crsxpais.cfm
tor_sf_proj = st_transform(muni_pop, crs = 32664)
tor_cartogram <- cartogram_cont(tor_sf_proj, "POB65_MAS")
c<-ggplot() +
geom_sf(data = tor, size = 0.15, fill = NA, color = "black") +
geom_sf(data = tor_cartogram, linewidth = .5, color="black",aes(fill = POB65_MAS)) +
scale_fill_scico(palette = "lapaz", direction = -1, end = 0.975, labels = function(x) format(x, big.mark = " ", trim = TRUE), name = NULL) +
geom_sf(data = recorteviasosm_sf, linewidth = .2,fill = NA, color = "brown") +
labs(
#title = "Torreón Coahuila",
subtitle="Cartograma de Población 65 y más por AGEB",
#caption = "autora: @guerreroclaudia"
) +
#coord_sf(xlim=c(-103.337,-103.3261),ylim=(c(25.097,25.108))) +
theme_void() +
theme(
legend.position = "bottom",
legend.key.width = unit(3, "line"),
legend.key.height = unit(0.5, "line"),
plot.background = element_rect(fill = NA, color = NA),
plot.title = element_text(size = 30, hjust = 0.5, face = "bold"),
plot.subtitle = element_text(size = 16, hjust = 0.5),
plot.caption = element_text(size = 10, hjust = 0.5, margin = margin(20, 0, 0, 0))
)
library(ggpubr)
#grid.arrange(b,c,d, ncol=3)
plot<-ggarrange(b,c,
common.legend = FALSE,ncol = 2,nrow=1)
annotate_figure(plot, top = text_grob("Comparación población 65 y más por AGEB sin y en Cartograma ",
color = "black", face = "bold", size = 20),
bottom = text_grob("Fuente: INEGI elaboración CGS",x=1,
size = 8,hjust=1))