library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.3     v purrr   0.3.4
## v tibble  3.1.0     v dplyr   1.0.5
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(sf)
## Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(rgdal)
## Warning: package 'rgdal' was built under R version 4.0.5
## Loading required package: sp
## Warning: package 'sp' was built under R version 4.0.5
## rgdal: version: 1.5-23, (SVN revision 1121)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
## Path to GDAL shared files: C:/Users/MP-PC-20030368/Documents/R/win-library/4.0/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: C:/Users/MP-PC-20030368/Documents/R/win-library/4.0/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-5
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.
## Overwritten PROJ_LIB was C:/Users/MP-PC-20030368/Documents/R/win-library/4.0/rgdal/proj
barrios <- st_read("barrios.geojson", stringsAsFactors = TRUE)
## Reading layer `barrios_badata' from data source `C:\Users\MP-PC-20030368\Desktop\TP 1 DATOS II\barrios.geojson' using driver `GeoJSON'
## Simple feature collection with 48 features and 4 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -58.53152 ymin: -34.70529 xmax: -58.33515 ymax: -34.52649
## Geodetic CRS:  WGS 84
paradas <- st_read("paradas-de-colectivo.geojson", stringsAsFactors = TRUE)
## Reading layer `paradas-de-colectivo' from data source `C:\Users\MP-PC-20030368\Desktop\TP 1 DATOS II\paradas-de-colectivo.geojson' using driver `GeoJSON'
## Simple feature collection with 42463 features and 12 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -59.82785 ymin: -35.18268 xmax: -57.73035 ymax: -34.0424
## Geodetic CRS:  WGS 84
head(paradas)
## Simple feature collection with 6 features and 12 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -58.37372 ymin: -34.64781 xmax: -58.36976 ymax: -34.62566
## Geodetic CRS:  WGS 84
##   stop_id stop_code                       stop_name  stop_lat  stop_lon
## 1     201       201                    1520 DEFENSA -34.62566 -58.37104
## 2    2072      2072 722 REGIMIENTO DE PATRICIOS AV. -34.63674 -58.36977
## 3    2079      2079 324 REGIMIENTO DE PATRICIOS AV. -34.63294 -58.37033
## 4    2084      2084  16 REGIMIENTO DE PATRICIOS AV. -34.62995 -58.37060
## 5   20350     20350   1776 MONTES DE OCA MANUEL AV. -34.64781 -58.37285
## 6   20358     20358   1358 MONTES DE OCA MANUEL AV. -34.64328 -58.37372
##   direction_id route_id agency_id route_short_name route_long_name
## 1            1      108        26              22A       JNAMBA022
## 2            1      108        26              22A       JNAMBA022
## 3            1      108        26              22A       JNAMBA022
## 4            1      108        26              22A       JNAMBA022
## 5            1      108        26              22A       JNAMBA022
## 6            1      108        26              22A       JNAMBA022
##                              route_desc route_type                    geometry
## 1 Retiro - Cno. Centenario y Lavalleja           3 POINT (-58.37104 -34.62566)
## 2 Retiro - Cno. Centenario y Lavalleja           3 POINT (-58.36977 -34.63674)
## 3 Retiro - Cno. Centenario y Lavalleja           3 POINT (-58.37033 -34.63294)
## 4 Retiro - Cno. Centenario y Lavalleja           3  POINT (-58.3706 -34.62995)
## 5 Retiro - Cno. Centenario y Lavalleja           3 POINT (-58.37285 -34.64781)
## 6 Retiro - Cno. Centenario y Lavalleja           3 POINT (-58.37372 -34.64328)
ggplot()+
  geom_sf(data=barrios,fill="gray75",color="white")

ggplot()+
  geom_sf(data=barrios,fill="gray75",color="white")+
  geom_sf(data=paradas)

paradas_por_barrio <- st_join(paradas,barrios)
## although coordinates are longitude/latitude, st_intersects assumes that they are planar
## although coordinates are longitude/latitude, st_intersects assumes that they are planar
head(paradas_por_barrio)
## Simple feature collection with 6 features and 16 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -58.37372 ymin: -34.64781 xmax: -58.36976 ymax: -34.62566
## Geodetic CRS:  WGS 84
##   stop_id stop_code                       stop_name  stop_lat  stop_lon
## 1     201       201                    1520 DEFENSA -34.62566 -58.37104
## 2    2072      2072 722 REGIMIENTO DE PATRICIOS AV. -34.63674 -58.36977
## 3    2079      2079 324 REGIMIENTO DE PATRICIOS AV. -34.63294 -58.37033
## 4    2084      2084  16 REGIMIENTO DE PATRICIOS AV. -34.62995 -58.37060
## 5   20350     20350   1776 MONTES DE OCA MANUEL AV. -34.64781 -58.37285
## 6   20358     20358   1358 MONTES DE OCA MANUEL AV. -34.64328 -58.37372
##   direction_id route_id agency_id route_short_name route_long_name
## 1            1      108        26              22A       JNAMBA022
## 2            1      108        26              22A       JNAMBA022
## 3            1      108        26              22A       JNAMBA022
## 4            1      108        26              22A       JNAMBA022
## 5            1      108        26              22A       JNAMBA022
## 6            1      108        26              22A       JNAMBA022
##                              route_desc route_type    barrio comuna perimetro
## 1 Retiro - Cno. Centenario y Lavalleja           3 SAN TELMO      1  5178.033
## 2 Retiro - Cno. Centenario y Lavalleja           3      BOCA      4 20060.004
## 3 Retiro - Cno. Centenario y Lavalleja           3      BOCA      4 20060.004
## 4 Retiro - Cno. Centenario y Lavalleja           3      BOCA      4 20060.004
## 5 Retiro - Cno. Centenario y Lavalleja           3  BARRACAS      4 12789.792
## 6 Retiro - Cno. Centenario y Lavalleja           3  BARRACAS      4 12789.792
##      area                    geometry
## 1 1232270 POINT (-58.37104 -34.62566)
## 2 5036240 POINT (-58.36977 -34.63674)
## 3 5036240 POINT (-58.37033 -34.63294)
## 4 5036240  POINT (-58.3706 -34.62995)
## 5 7953453 POINT (-58.37285 -34.64781)
## 6 7953453 POINT (-58.37372 -34.64328)
ggplot()+
  geom_sf(data=paradas_por_barrio %>% filter(!is.na(barrio)),aes(color=barrio),show.legend = FALSE)+
  labs(title = "Paradas de Colectivo en Buenos Aires", subtitle = "Según Barrio de Ubicación")

cuantia_paradas <- paradas_por_barrio %>%
  st_set_geometry(NULL) %>%
  group_by(barrio) %>%
  summarise(cantidad=n())
head(cuantia_paradas)
## # A tibble: 6 x 2
##   barrio    cantidad
##   <fct>        <int>
## 1 AGRONOMIA       35
## 2 ALMAGRO        265
## 3 BALVANERA      392
## 4 BARRACAS       284
## 5 BELGRANO       248
## 6 BOCA           167
barrios_por_paradas <- left_join(barrios,cuantia_paradas,by="barrio")
head(barrios_por_paradas)
## Simple feature collection with 6 features and 5 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -58.50617 ymin: -34.63064 xmax: -58.41192 ymax: -34.57829
## Geodetic CRS:  WGS 84
##             barrio comuna perimetro    area cantidad
## 1        CHACARITA     15  7724.853 3115707      183
## 2         PATERNAL     15  7087.513 2229829       60
## 3     VILLA CRESPO     15  8131.857 3615978      209
## 4 VILLA DEL PARQUE     11  7705.390 3399596      109
## 5          ALMAGRO      5  8537.901 4050752      265
## 6        CABALLITO      6 10990.964 6851029      367
##                         geometry
## 1 POLYGON ((-58.45282 -34.595...
## 2 POLYGON ((-58.46558 -34.596...
## 3 POLYGON ((-58.42375 -34.597...
## 4 POLYGON ((-58.49461 -34.614...
## 5 POLYGON ((-58.41287 -34.614...
## 6 POLYGON ((-58.43061 -34.607...
ggplot(barrios_por_paradas)+
  geom_bar(aes(x=reorder(barrio, cantidad), weight=cantidad)) +
  labs(title="Barrios por cantidad de paradas de colectivo",
       subtitle="Por paradas individuales",
       x="Barrio",
       y="Cantidad de paradas",
       caption="Fuente: BA Data")+
   coord_flip()

ggplot()+
  geom_sf(data=barrios_por_paradas, aes(fill=cantidad))+
  labs(title="Barrios por cantidad de paradas de colectivo",
       subtitle="Por paradas individuales",
       caption="Fuente: BA Data")

##Conclusiones

Podemos visualizar facilmente, tanto el gráfico de barras como el plano coroplético, aquellos barrios de la Ciudad de Buenos Aires con mayor y menor presencia de paradas de colectivo individuales. Es facil de visualizar que el barrio de Palermo es el de mayor presencia (único barrio con mas de 500), mientras que Puerto Madero es claro colista de la tabla (con menos de 50). Asimismo, se puede observar como la zona centro y este de la ciudad, con la excepción del mencionado Puerto Madero, son las que muestran mayor presencia de paradas, mientras que los barrios del oeste, especialmente aquellos lejos de la General Paz, son los menos conectados. De la misma manera, los barrios del sudoeste ven disminuida la presencia de paradas.