#install.packages(tidyverse)
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.2.1
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.9
## ✔ tidyr 1.2.0 ✔ stringr 1.4.0
## ✔ readr 2.1.2 ✔ forcats 0.5.1
## Warning: package 'ggplot2' was built under R version 4.2.1
## Warning: package 'tibble' was built under R version 4.2.1
## Warning: package 'tidyr' was built under R version 4.2.1
## Warning: package 'readr' was built under R version 4.2.1
## Warning: package 'purrr' was built under R version 4.2.1
## Warning: package 'dplyr' was built under R version 4.2.1
## Warning: package 'forcats' was built under R version 4.2.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
#install.packages(sf)
library(sf)
## Warning: package 'sf' was built under R version 4.2.1
## Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE
#install.packages("leaflet")
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.2.1
#install.packages("tmaptools")
library(tmaptools)
## Warning: package 'tmaptools' was built under R version 4.2.1
#install.packages("osrm")
library(osrm)
## Warning: package 'osrm' was built under R version 4.2.1
## Data: (c) OpenStreetMap contributors, ODbL 1.0 - http://www.openstreetmap.org/copyright
## Routing: OSRM - http://project-osrm.org/
## sp support will be dropped in the next major release, please use sf objects instead.
library(knitr) #para tablas
library(kableExtra) #para tablas
## Warning: package 'kableExtra' was built under R version 4.2.1
##
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
##
## group_rows
Cargamos el dataset de Barrios obtenido de BA Data: https://data.buenosaires.gob.ar/dataset/barrios
barrios <- st_read("data/barrios.geojson", stringsAsFactors = TRUE)
## Reading layer `barrios' from data source `C:\UTDT-Trabajo3\data\barrios.geojson' using driver `GeoJSON'
## Simple feature collection with 48 features and 5 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -58.53152 ymin: -34.70529 xmax: -58.33515 ymax: -34.52649
## Geodetic CRS: WGS 84
Cargamos el dataset con los Domicilios de las reparticiones
#domicilios <- read.csv("data/Domicilios_geocodif.csv", stringsAsFactors = TRUE)
domicilios <- st_read("data/Reparticiones_geo.geojson", stringsAsFactors = TRUE)
## Reading layer `Reparticiones_geo' from data source
## `C:\UTDT-Trabajo3\data\Reparticiones_geo.geojson' using driver `GeoJSON'
## Simple feature collection with 1053 features and 5 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: -58.52846 ymin: -34.6919 xmax: -58.3549 ymax: -34.5351
## Geodetic CRS: WGS 84
// Excluimos para éste anáilisis los 0, list() // domicilios que no tienen coordenadas cargadas
#domicilios <- domicilios %>%
# filter(!is.na(geometry))
ggplot(domicilios) +
geom_sf(data=barrios, aes(fill=BARRIO), color="black", show.legend=FALSE, alpha=0.25)+
scale_fill_discrete()+
geom_sf(data=domicilios,aes(color=-CANTIDAD,size=CANTIDAD), alpha=.5)+
scale_color_distiller(palette="Dark2")+
labs(title="Reparticiones del Gobierno de la Ciudad de Buenos Aires",
color="Q Empleados",
size="Q Empleados",
caption="Fuente: DGPLYCO")
ggplot()+
geom_sf(data=barrios, color="black", show.legend=FALSE, alpha=0.25)+
scale_fill_discrete()+
geom_sf(data= domicilios %>% filter(CANTIDAD > 1500)
,aes(size=CANTIDAD), color="RED")+
labs(title="Reparticiones del Gobierno de la Ciudad de Buenos Aires",
subtitle = paste(count(filter(domicilios,CANTIDAD > 1500)),"reparticiones con más de 1500 empleados"),
#color="Q Empleados",
size="Q Empleados",
caption="Fuente: DGPLYCO")
las podemos ver en un mapa interactivo
top_domicilios<-domicilios %>%
filter(CANTIDAD > 1500) %>%
mutate(lon=unlist(map(geometry,1)),
lat=unlist(map(geometry,2))
)
leaflet(top_domicilios) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addCircleMarkers(~lon, ~lat,
#color = "white",
popup = ~paste(domicilio,"<br>",
CANTIDAD,"Empleados"))
leaflet() %>%
addTiles() %>%
addPolygons(data=barrios,weight = 1,
label = ~paste(BARRIO,"- Comuna ",COMUNA),
fillColor = "red") %>%
addMarkers(data=domicilios %>% filter(CANTIDAD > 1500),
popup = ~paste(domicilio,"<br>",
CANTIDAD,"Empleados"))
veamos la estructura de los 2 datasets
str(domicilios)
## Classes 'sf' and 'data.frame': 1053 obs. of 6 variables:
## $ ORD : Factor w/ 1053 levels "1","10","100",..: 1 309 396 488 586 684 778 871 959 2 ...
## $ CALLE : Factor w/ 532 levels "11 DE SEPTIEMBRE DE 1888",..: 82 504 81 223 5 185 190 89 472 169 ...
## $ NRO : Factor w/ 903 levels "1","10","100",..: 283 760 321 868 130 890 78 548 323 180 ...
## $ CANTIDAD : num 87 2387 1074 563 80 ...
## $ domicilio: Factor w/ 1053 levels "11 DE SEPTIEMBRE DE 1888 3451 , CABA, Argentina",..: 159 996 157 429 6 364 372 174 930 339 ...
## $ geometry :sfc_POINT of length 1053; first list element: 'XY' num -58.4 -34.6
## - attr(*, "sf_column")= chr "geometry"
## - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA
## ..- attr(*, "names")= chr [1:5] "ORD" "CALLE" "NRO" "CANTIDAD" ...
str(barrios)
## Classes 'sf' and 'data.frame': 48 obs. of 6 variables:
## $ BARRIO : Factor w/ 48 levels "AGRONOMIA","ALMAGRO",..: 9 26 36 37 2 8 46 18 44 13 ...
## $ COMUNA : num 15 15 15 11 5 6 11 10 10 7 ...
## $ PERIMETRO: num 7725 7088 8132 7705 8538 ...
## $ AREA : num 3115707 2229829 3615978 3399596 4050752 ...
## $ OBJETO : Factor w/ 1 level "BARRIO": 1 1 1 1 1 1 1 1 1 1 ...
## $ geometry :sfc_MULTIPOLYGON of length 48; first list element: List of 1
## ..$ :List of 1
## .. ..$ : num [1:142, 1:2] -58.5 -58.5 -58.5 -58.5 -58.5 ...
## ..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
## - attr(*, "sf_column")= chr "geometry"
## - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA
## ..- attr(*, "names")= chr [1:5] "BARRIO" "COMUNA" "PERIMETRO" "AREA" ...
podemos generar un dataset uniéndolos por los datos espaciales
domicilios2 <- st_join(domicilios,barrios)
summary(domicilios2)
## ORD CALLE NRO CANTIDAD
## 1 : 1 RIVADAVIA AV. : 13 1052 : 4 Min. : 1.0
## 10 : 1 CASEROS AV. : 11 1502 : 4 1st Qu.: 32.0
## 100 : 1 CORDOBA AV. : 11 3050 : 4 Median : 66.0
## 1000 : 1 SAN JUAN AV. : 9 3100 : 4 Mean : 193.5
## 1001 : 1 VARELA AV. : 9 780 : 4 3rd Qu.: 136.0
## 1003 : 1 INDEPENDENCIA AV.: 8 (Other):1031 Max. :4689.0
## (Other):1047 (Other) :992 NA's : 2
## domicilio BARRIO
## 11 DE SEPTIEMBRE DE 1888 3451 , CABA, Argentina: 1 PALERMO : 64
## 14 DE JULIO 546 , CABA, Argentina : 1 BALVANERA : 57
## 15 DE NOVIEMBRE DE 1889 1749 , CABA, Argentina : 1 CABALLITO : 53
## 2 DE ABRIL DE 1982 6850 , CABA, Argentina : 1 FLORES : 51
## 24 DE NOVIEMBRE 113 , CABA, Argentina : 1 VILLA LUGANO: 44
## 24 DE NOVIEMBRE 1536 , CABA, Argentina : 1 BARRACAS : 43
## (Other) :1047 (Other) :741
## COMUNA PERIMETRO AREA OBJETO
## Min. : 1.000 Min. : 4531 Min. : 1232270 BARRIO:1053
## 1st Qu.: 4.000 1st Qu.: 7725 1st Qu.: 3115707
## Median : 7.000 Median :10166 Median : 4512711
## Mean : 7.195 Mean :11489 Mean : 5590567
## 3rd Qu.:11.000 3rd Qu.:15057 3rd Qu.: 7398179
## Max. :15.000 Max. :22127 Max. :15845871
##
## geometry
## POINT :1053
## epsg:4326 : 0
## +proj=long...: 0
##
##
##
##
Vemos que se incorporaron los campos BARRIO COMUNA PERIMETRO AREA
Utilicemos el campo BARRIO para colorear las reparticiones
ggplot()+
geom_sf(data=barrios )+
scale_fill_steps()+
geom_sf(data=domicilios2, aes(color=BARRIO), show.legend = FALSE )+
labs(title = "Reparticiones GCABA") +
theme_dark()
Agrupamos para tener la información por Barrio
Info_x_barrio <- domicilios2 %>%
group_by(BARRIO) %>%
summarise(cant_repas = n(),
empleados = sum(CANTIDAD)) %>%
st_set_geometry(NULL) # Eliminamos la geometría para porde luego hacer JOIN
head(Info_x_barrio)
## # A tibble: 6 × 3
## BARRIO cant_repas empleados
## <fct> <int> <dbl>
## 1 AGRONOMIA 2 180
## 2 ALMAGRO 42 10846
## 3 BALVANERA 57 16263
## 4 BARRACAS 43 13174
## 5 BELGRANO 33 6228
## 6 BOCA 33 8874
barrios <- left_join(barrios,Info_x_barrio,by="BARRIO")
hacemos un mapa corocléptico de cantidad de reparticiones por barrio
ggplot()+
geom_sf(data=barrios, aes(fill=cant_repas)) +
labs(title="Reparticiones del Gobierno de la Ciudad de Buenos Aires",
subtitle = "Reparticiones por Barrio",
fill="Cant. de Reparticiones",
caption="Fuente: DGPLYCO")
y otro mapa corocléptico de cantidad de empleados por barrio
ggplot()+
geom_sf(data=barrios, aes(fill=empleados)) +
coord_sf(datum = NA) +
labs(title="Reparticiones del Gobierno de la Ciudad de Buenos Aires",
subtitle = "Empleados por Barrio",
fill="Cant. de Empleados",
caption="Fuente: DGPLYCO"
)
vemos los barrios con mayor cantidad de reparticiones
ggplot()+
geom_sf(data=barrios, color="black", show.legend=FALSE, alpha=0.25) +
geom_sf(data=arrange(barrios,-cant_repas) %>% head(5)
,aes(fill=cant_repas))+
coord_sf(datum = NA) +
geom_sf_text(data=arrange(barrios,-cant_repas) %>% head(5)
,aes(label = BARRIO), size=3, colour = "RED") +
labs(title="Reparticiones del Gobierno de la Ciudad de Buenos Aires",
subtitle = "Top 5 de Empleados por Barrio",
fill="Cant. de Reparticiones",
caption="Fuente: DGPLYCO",
x="",
y="")
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data
Podemos ver los datos en forma tabular
kable(arrange(barrios,-cant_repas) %>%
head(5) %>%
st_set_geometry(NULL) %>%
select(BARRIO,cant_repas,empleados),
col.names = c("Barrio", "Reparticiones", "Nro. Empleados")
) %>%
kable_styling(font_size = 15)
| Barrio | Reparticiones | Nro. Empleados |
|---|---|---|
| PALERMO | 64 | 10041 |
| BALVANERA | 57 | 16263 |
| CABALLITO | 53 | 10276 |
| FLORES | 51 | 8129 |
| VILLA LUGANO | 44 | 5632 |
Generamos mapas segmentando por barrio según la cantidad de empleados
ggplot()+
geom_sf(data=barrios, color="black", aes(fill=cant_repas),show.legend=FALSE, alpha=0.25) +
facet_wrap(~cut(barrios$cant_repas,breaks = 3,c("bajo", "medio", "alto"))) +
coord_sf(datum = NA) +
labs(title="Reparticiones del Gobierno de la Ciudad de Buenos Aires",
subtitle = "Cantidad de Empleados ")