Tarea 5: Analizando y Visualizando Flujos de Viajes Urbanos

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## 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(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(leaflet)
library(osrm)
## Data: (c) OpenStreetMap contributors, ODbL 1.0 - http://www.openstreetmap.org/copyright
## Routing: OSRM - http://project-osrm.org/
recorridos_ecobici <- read.csv("recorridos-realizados-2020.csv")
dim(recorridos_ecobici)
## [1] 2002945      15
summary(recorridos_ecobici)
##  duracion_recorrido id_estacion_origen fecha_origen_recorrido
##  Min.   :      0    Min.   :  2.0      Length:2002945        
##  1st Qu.:    709    1st Qu.: 76.0      Class :character      
##  Median :   1142    Median :164.0      Mode  :character      
##  Mean   :   1463    Mean   :169.2                            
##  3rd Qu.:   1664    3rd Qu.:239.0                            
##  Max.   :3190094    Max.   :449.0                            
##  nombre_estacion_origen fecha_destino_recorrido id_estacion_destino
##  Length:2002945         Length:2002945          Min.   :  2.0      
##  Class :character       Class :character        1st Qu.: 76.0      
##  Mode  :character       Mode  :character        Median :165.0      
##                                                 Mean   :170.2      
##                                                 3rd Qu.:239.0      
##                                                 Max.   :449.0      
##  nombre_estacion_destino   id_usuario     direccion_estacion_origen
##  Length:2002945          Min.   :    37   Length:2002945           
##  Class :character        1st Qu.:164597   Class :character         
##  Mode  :character        Median :458410   Mode  :character         
##                          Mean   :417679                            
##                          3rd Qu.:673113                            
##                          Max.   :735946                            
##  long_estacion_origen lat_estacion_origen direccion_estacion_destino
##  Min.   :-58.49       Min.   :-34.65      Length:2002945            
##  1st Qu.:-58.44       1st Qu.:-34.61      Class :character          
##  Median :-58.42       Median :-34.60      Mode  :character          
##  Mean   :-58.42       Mean   :-34.60                                
##  3rd Qu.:-58.40       3rd Qu.:-34.58                                
##  Max.   :-58.36       Max.   :-34.54                                
##  long_estacion_destino lat_estacion_destino    periodo    
##  Min.   :-58.49        Min.   :-34.65       Min.   :2020  
##  1st Qu.:-58.44        1st Qu.:-34.61       1st Qu.:2020  
##  Median :-58.42        Median :-34.60       Median :2020  
##  Mean   :-58.42        Mean   :-34.60       Mean   :2020  
##  3rd Qu.:-58.39        3rd Qu.:-34.58       3rd Qu.:2020  
##  Max.   :-58.36        Max.   :-34.54       Max.   :2020
bbox_caba <- make_bbox(recorridos_ecobici$long_estacion_destino, recorridos_ecobici$lat_estacion_destino)
mapa_caba <- get_stamenmap(bbox_caba, zoom=12)
## Source : http://tile.stamen.com/terrain/12/1382/2467.png
## Source : http://tile.stamen.com/terrain/12/1383/2467.png
## Source : http://tile.stamen.com/terrain/12/1384/2467.png
## Source : http://tile.stamen.com/terrain/12/1382/2468.png
## Source : http://tile.stamen.com/terrain/12/1383/2468.png
## Source : http://tile.stamen.com/terrain/12/1384/2468.png
ggmap(mapa_caba)

ggmap(mapa_caba)+
  geom_point(data= recorridos_ecobici, aes(x=long_estacion_destino, y=lat_estacion_destino), inherit.aes = FALSE)+
  labs(title = "Ecobici Buenos Aires",
       subtitle = "Estaciones de bicicletas")

viajes <- recorridos_ecobici %>%
  group_by(nombre_estacion_origen, long_estacion_origen, lat_estacion_origen, nombre_estacion_destino,  long_estacion_destino,lat_estacion_destino) %>%
  summarise(cant_viajes = n ())
## `summarise()` has grouped output by 'nombre_estacion_origen', 'long_estacion_origen', 'lat_estacion_origen', 'nombre_estacion_destino', 'long_estacion_destino'. You can override using the `.groups` argument.
viajes
## # A tibble: 36,184 x 7
## # Groups:   nombre_estacion_origen, long_estacion_origen, lat_estacion_origen,
## #   nombre_estacion_destino, long_estacion_destino [36,184]
##    nombre_estacion_ori~ long_estacion_ori~ lat_estacion_ori~ nombre_estacion_de~
##    <chr>                             <dbl>             <dbl> <chr>              
##  1 001 - FACULTAD DE D~              -58.4             -34.6 001 - FACULTAD DE ~
##  2 001 - FACULTAD DE D~              -58.4             -34.6 002 - Retiro I     
##  3 001 - FACULTAD DE D~              -58.4             -34.6 003 - ADUANA       
##  4 001 - FACULTAD DE D~              -58.4             -34.6 004 - Plaza Roma   
##  5 001 - FACULTAD DE D~              -58.4             -34.6 005 - Plaza Italia 
##  6 001 - FACULTAD DE D~              -58.4             -34.6 006 - Parque Lezama
##  7 001 - FACULTAD DE D~              -58.4             -34.6 007 - OBELISCO     
##  8 001 - FACULTAD DE D~              -58.4             -34.6 008 - Congreso     
##  9 001 - FACULTAD DE D~              -58.4             -34.6 009 - Parque Las H~
## 10 001 - FACULTAD DE D~              -58.4             -34.6 012 - Plaza Vicent~
## # ... with 36,174 more rows, and 3 more variables: long_estacion_destino <dbl>,
## #   lat_estacion_destino <dbl>, cant_viajes <int>
top_10 <- viajes %>%
  filter(nombre_estacion_origen != nombre_estacion_destino) %>%
  arrange(desc(cant_viajes)) %>%
  head(10)
top_10
## # A tibble: 10 x 7
## # Groups:   nombre_estacion_origen, long_estacion_origen, lat_estacion_origen,
## #   nombre_estacion_destino, long_estacion_destino [10]
##    nombre_estacion_orig~ long_estacion_or~ lat_estacion_or~ nombre_estacion_des~
##    <chr>                             <dbl>            <dbl> <chr>               
##  1 255 - BARRANCAS DE B~             -58.4            -34.6 292 - PLAZA BOLIVIA 
##  2 014 - Pacifico                    -58.4            -34.6 160 - Godoy Cruz y ~
##  3 292 - PLAZA BOLIVIA               -58.4            -34.6 255 - BARRANCAS DE ~
##  4 399 - GARCIA DEL RIO              -58.5            -34.6 257 - Plaza Francis~
##  5 292 - PLAZA BOLIVIA               -58.4            -34.6 160 - Godoy Cruz y ~
##  6 391 - Plaza Repúbli~             -58.4            -34.6 292 - PLAZA BOLIVIA 
##  7 257 - Plaza Francisc~             -58.5            -34.6 399 - GARCIA DEL RIO
##  8 014 - Pacifico                    -58.4            -34.6 391 - Plaza Repúbl~
##  9 160 - Godoy Cruz y L~             -58.4            -34.6 292 - PLAZA BOLIVIA 
## 10 292 - PLAZA BOLIVIA               -58.4            -34.6 391 - Plaza Repúbl~
## # ... with 3 more variables: long_estacion_destino <dbl>,
## #   lat_estacion_destino <dbl>, cant_viajes <int>
ggplot()+
  geom_tile(data = top_10,
            aes(x = as.factor(nombre_estacion_origen),
                y = as.factor(nombre_estacion_destino),
                fill = cant_viajes)) +
  scale_fill_distiller(palette = "RdYlGn")+
  labs(title = "Matriz recorridos - Ecobici",
       subtitle = "Top 10 recorridos",
       x= "Estacion de Origen",
       y= "Estacion de destino",
       fill="Viajes")

El recorrido más utilizado es el comprendido entre las estaciones “Barrancas de Belgrano” como estación de origen y “Plaza Bolivia” como estación de destino.

En el dataset “Viajes” aparecen 34 registros de rutas con más recorridos que los filtados dentro del dataset de “Top_10”, pero son viajes de origen y finalización en la misma estación.

Ruteo

viaje1 <- top_10%>%
  ungroup() %>%
  filter(cant_viajes==max(cant_viajes))
ruteo_viaje1 <- osrmRoute(src = c(viaje1$nombre_estacion_origen, viaje1$long_estacion_origen, viaje1$lat_estacion_origen),
                          dst = c(viaje1$nombre_estacion_destino, viaje1$long_estacion_destino, viaje1$lat_estacion_destino),
                          returnclass = "sf",
                          overview = "full",
                          osrm.profile = "bike")
ruteo_viaje1
## Simple feature collection with 1 feature and 4 fields
## Geometry type: LINESTRING
## Dimension:     XY
## Bounding box:  xmin: -58.44831 ymin: -34.56354 xmax: -58.43614 ymax: -34.5598
## Geodetic CRS:  WGS 84
##                                                                         src
## 255 - BARRANCAS DE BELGRANO_292 - PLAZA BOLIVIA 255 - BARRANCAS DE BELGRANO
##                                                                 dst duration
## 255 - BARRANCAS DE BELGRANO_292 - PLAZA BOLIVIA 292 - PLAZA BOLIVIA 5.756667
##                                                 distance
## 255 - BARRANCAS DE BELGRANO_292 - PLAZA BOLIVIA   1.2402
##                                                                       geometry
## 255 - BARRANCAS DE BELGRANO_292 - PLAZA BOLIVIA LINESTRING (-58.44831 -34.5...
ggmap(mapa_caba)+
  geom_point(data=recorridos_ecobici, aes(x=long_estacion_destino, y=lat_estacion_destino), inherit.aes = FALSE)+
  geom_sf(data=ruteo_viaje1, color="red", size=1.5, inherit.aes = FALSE)+
  labs(title="Recorrido más Realizado en Bicicleta",
       subtitle="Ecobici - Buenos Aires")+
  theme_void()
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.

leaflet(ruteo_viaje1) %>% 
    addTiles() %>% 
    addPolylines(color = "red",
                 label = paste("Distancia:", ruteo_viaje1$distance, "|",
                               "Duración:", ruteo_viaje1$duration))

El recorrido más utilizado es el que une la estación “Barrancas de Belgrano” de la línea del tren Mitre con la estación “Lisandro de la Torre” de la misma línea de tren.