Cargamos librerias a utilizar

library(tidyverse)
library(sf)
library(ggmap)
library(lubridate)

Preparación de datasets

Haremos un análisis del uso de bicicletas de aquiler en Londres Usaremos un dataset obtenido del portal de datos de la ciudad de Londres https://data.london.gov.uk/

london_bikes <- read.csv("bikes.csv",stringsAsFactors=TRUE)
dim(london_bikes)
## [1] 459114     14
head(london_bikes)
##   tripduration                starttime                 stoptime
## 1          914 2021-01-01 00:00:04.5900 2021-01-01 00:15:19.1680
## 2         1085 2021-01-01 00:00:21.8030 2021-01-01 00:18:27.4640
## 3          946 2021-01-01 00:00:26.0090 2021-01-01 00:16:12.0900
## 4          355 2021-01-01 00:00:30.9210 2021-01-01 00:06:26.6000
## 5          511 2021-01-01 00:01:11.2270 2021-01-01 00:09:43.1950
## 6         1004 2021-01-01 00:01:52.3100 2021-01-01 00:18:36.4180
##   start.station.id                                          start.station.name
## 1               91            One Kendall Square at Hampshire St / Portland St
## 2              370                                  Dartmouth St at Newbury St
## 3               46 Christian Science Plaza - Massachusetts Ave at Westland Ave
## 4              178                             MIT Pacific St at Purrington St
## 5              386                     Sennott Park Broadway at Norfolk Street
## 6              105           Lower Cambridgeport at Magazine St / Riverside Rd
##   start.station.latitude start.station.longitude end.station.id
## 1               42.36628               -71.09169            370
## 2               42.35096               -71.07783            169
## 3               42.34367               -71.08582             21
## 4               42.35957               -71.10129            107
## 5               42.36861               -71.09930            413
## 6               42.35722               -71.11387             55
##                          end.station.name end.station.latitude
## 1              Dartmouth St at Newbury St             42.35096
## 2 Edwards Playground - Main St at Eden St             42.37897
## 3  Prudential Center - 101 Huntington Ave             42.34652
## 4                      Ames St at Main St             42.36250
## 5 Kennedy-Longfellow School 158 Spring St             42.36955
## 6        Boylston St at Massachusetts Ave             42.34741
##   end.station.longitude bikeid   usertype postal.code
## 1             -71.07783   5316   Customer       02139
## 2             -71.06861   4917 Subscriber       02116
## 3             -71.08066   2881   Customer       02115
## 4             -71.08822   4792 Subscriber       02139
## 5             -71.08579   6062 Subscriber       02139
## 6             -71.08678   4371 Subscriber       02139
summary(london_bikes)
##   tripduration                        starttime     
##  Min.   :     61   2021-02-25 13:13:19.3730:     2  
##  1st Qu.:    428   2021-03-10 16:06:26.1900:     2  
##  Median :    737   2021-03-11 18:03:49.4190:     2  
##  Mean   :   1548   2021-03-12 00:53:28.0240:     2  
##  3rd Qu.:   1277   2021-03-24 14:52:34.7600:     2  
##  Max.   :2999796   2021-03-25 21:52:08.0320:     2  
##                    (Other)                 :459102  
##                      stoptime      start.station.id
##  2021-03-09 22:44:19.8040:     2   Min.   :  1.0   
##  2021-03-18 18:58:24.3660:     2   1st Qu.: 58.0   
##  2021-03-20 20:38:31.5010:     2   Median :110.0   
##  2021-03-26 01:06:22.0700:     2   Mean   :172.1   
##  2021-03-30 18:24:00.7390:     2   3rd Qu.:328.0   
##  2021-04-05 21:24:00.2220:     2   Max.   :510.0   
##  (Other)                 :459102                   
##                                                    start.station.name
##  MIT at Mass Ave / Amherst St                               :  9715  
##  Central Square at Mass Ave / Essex St                      :  8677  
##  Charles Circle - Charles St at Cambridge St                :  7506  
##  Christian Science Plaza - Massachusetts Ave at Westland Ave:  7157  
##  MIT Pacific St at Purrington St                            :  5920  
##  MIT Vassar St                                              :  5898  
##  (Other)                                                    :414241  
##  start.station.latitude start.station.longitude end.station.id 
##  Min.   :42.27          Min.   :-71.23          Min.   :  1.0  
##  1st Qu.:42.35          1st Qu.:-71.11          1st Qu.: 58.0  
##  Median :42.36          Median :-71.09          Median :107.0  
##  Mean   :42.36          Mean   :-71.09          Mean   :171.1  
##  3rd Qu.:42.37          3rd Qu.:-71.07          3rd Qu.:328.0  
##  Max.   :42.42          Max.   :-71.01          Max.   :510.0  
##                                                                
##                                                     end.station.name 
##  MIT at Mass Ave / Amherst St                               : 10305  
##  Central Square at Mass Ave / Essex St                      :  9005  
##  Charles Circle - Charles St at Cambridge St                :  7602  
##  Christian Science Plaza - Massachusetts Ave at Westland Ave:  7121  
##  Boylston St at Massachusetts Ave                           :  5994  
##  Ames St at Main St                                         :  5844  
##  (Other)                                                    :413243  
##  end.station.latitude end.station.longitude     bikeid           usertype     
##  Min.   :42.27        Min.   :-71.23        Min.   : 218   Customer  :112612  
##  1st Qu.:42.35        1st Qu.:-71.11        1st Qu.:3378   Subscriber:346502  
##  Median :42.36        Median :-71.09        Median :4759                      
##  Mean   :42.36        Mean   :-71.09        Mean   :4590                      
##  3rd Qu.:42.37        3rd Qu.:-71.07        3rd Qu.:5839                      
##  Max.   :42.42        Max.   :-71.01        Max.   :7029                      
##                                                                               
##   postal.code    
##  02139  : 73145  
##  NULL   : 23797  
##  02215  : 20618  
##  02118  : 19223  
##  02138  : 17462  
##  02141  : 16671  
##  (Other):288198
str(london_bikes)
## 'data.frame':    459114 obs. of  14 variables:
##  $ tripduration           : int  914 1085 946 355 511 1004 571 3355 2112 694 ...
##  $ starttime              : Factor w/ 459098 levels "2021-01-01 00:00:04.5900",..: 1 2 3 4 5 6 7 8 9 10 ...
##  $ stoptime               : Factor w/ 459098 levels "2021-01-01 00:06:26.6000",..: 6 9 8 1 2 10 5 65 27 7 ...
##  $ start.station.id       : int  91 370 46 178 386 105 47 179 43 456 ...
##  $ start.station.name     : Factor w/ 373 levels "160 Arsenal",..: 259 132 102 239 292 218 127 241 283 166 ...
##  $ start.station.latitude : num  42.4 42.4 42.3 42.4 42.4 ...
##  $ start.station.longitude: num  -71.1 -71.1 -71.1 -71.1 -71.1 ...
##  $ end.station.id         : int  370 169 21 107 413 55 81 107 372 456 ...
##  $ end.station.name       : Factor w/ 375 levels "160 Arsenal",..: 133 146 275 23 209 64 102 23 61 168 ...
##  $ end.station.latitude   : num  42.4 42.4 42.3 42.4 42.4 ...
##  $ end.station.longitude  : num  -71.1 -71.1 -71.1 -71.1 -71.1 ...
##  $ bikeid                 : int  5316 4917 2881 4792 6062 4371 3609 5287 2053 4659 ...
##  $ usertype               : Factor w/ 2 levels "Customer","Subscriber": 1 2 1 2 2 2 2 2 1 2 ...
##  $ postal.code            : Factor w/ 5356 levels ""," 02139","+5076",..: 440 360 354 440 440 440 345 440 440 431 ...
names(london_bikes)
##  [1] "tripduration"            "starttime"              
##  [3] "stoptime"                "start.station.id"       
##  [5] "start.station.name"      "start.station.latitude" 
##  [7] "start.station.longitude" "end.station.id"         
##  [9] "end.station.name"        "end.station.latitude"   
## [11] "end.station.longitude"   "bikeid"                 
## [13] "usertype"                "postal.code"

En primer lugar convertiremos en formato de fecha los timestamp de inicio i finalización

london_bikes <- london_bikes %>% 
  mutate(starttime=ymd_hms(starttime),stoptime=ymd_hms(stoptime),)
head(london_bikes)
##   tripduration           starttime            stoptime start.station.id
## 1          914 2021-01-01 00:00:04 2021-01-01 00:15:19               91
## 2         1085 2021-01-01 00:00:21 2021-01-01 00:18:27              370
## 3          946 2021-01-01 00:00:26 2021-01-01 00:16:12               46
## 4          355 2021-01-01 00:00:30 2021-01-01 00:06:26              178
## 5          511 2021-01-01 00:01:11 2021-01-01 00:09:43              386
## 6         1004 2021-01-01 00:01:52 2021-01-01 00:18:36              105
##                                            start.station.name
## 1            One Kendall Square at Hampshire St / Portland St
## 2                                  Dartmouth St at Newbury St
## 3 Christian Science Plaza - Massachusetts Ave at Westland Ave
## 4                             MIT Pacific St at Purrington St
## 5                     Sennott Park Broadway at Norfolk Street
## 6           Lower Cambridgeport at Magazine St / Riverside Rd
##   start.station.latitude start.station.longitude end.station.id
## 1               42.36628               -71.09169            370
## 2               42.35096               -71.07783            169
## 3               42.34367               -71.08582             21
## 4               42.35957               -71.10129            107
## 5               42.36861               -71.09930            413
## 6               42.35722               -71.11387             55
##                          end.station.name end.station.latitude
## 1              Dartmouth St at Newbury St             42.35096
## 2 Edwards Playground - Main St at Eden St             42.37897
## 3  Prudential Center - 101 Huntington Ave             42.34652
## 4                      Ames St at Main St             42.36250
## 5 Kennedy-Longfellow School 158 Spring St             42.36955
## 6        Boylston St at Massachusetts Ave             42.34741
##   end.station.longitude bikeid   usertype postal.code
## 1             -71.07783   5316   Customer       02139
## 2             -71.06861   4917 Subscriber       02116
## 3             -71.08066   2881   Customer       02115
## 4             -71.08822   4792 Subscriber       02139
## 5             -71.08579   6062 Subscriber       02139
## 6             -71.08678   4371 Subscriber       02139
london_bikes2 <- london_bikes %>% 
  mutate(mes=month(starttime, label=TRUE), diasem=wday(starttime, label=TRUE), hora=hour(starttime))
head(london_bikes2)
##   tripduration           starttime            stoptime start.station.id
## 1          914 2021-01-01 00:00:04 2021-01-01 00:15:19               91
## 2         1085 2021-01-01 00:00:21 2021-01-01 00:18:27              370
## 3          946 2021-01-01 00:00:26 2021-01-01 00:16:12               46
## 4          355 2021-01-01 00:00:30 2021-01-01 00:06:26              178
## 5          511 2021-01-01 00:01:11 2021-01-01 00:09:43              386
## 6         1004 2021-01-01 00:01:52 2021-01-01 00:18:36              105
##                                            start.station.name
## 1            One Kendall Square at Hampshire St / Portland St
## 2                                  Dartmouth St at Newbury St
## 3 Christian Science Plaza - Massachusetts Ave at Westland Ave
## 4                             MIT Pacific St at Purrington St
## 5                     Sennott Park Broadway at Norfolk Street
## 6           Lower Cambridgeport at Magazine St / Riverside Rd
##   start.station.latitude start.station.longitude end.station.id
## 1               42.36628               -71.09169            370
## 2               42.35096               -71.07783            169
## 3               42.34367               -71.08582             21
## 4               42.35957               -71.10129            107
## 5               42.36861               -71.09930            413
## 6               42.35722               -71.11387             55
##                          end.station.name end.station.latitude
## 1              Dartmouth St at Newbury St             42.35096
## 2 Edwards Playground - Main St at Eden St             42.37897
## 3  Prudential Center - 101 Huntington Ave             42.34652
## 4                      Ames St at Main St             42.36250
## 5 Kennedy-Longfellow School 158 Spring St             42.36955
## 6        Boylston St at Massachusetts Ave             42.34741
##   end.station.longitude bikeid   usertype postal.code mes diasem hora
## 1             -71.07783   5316   Customer       02139 ene vie\\.    0
## 2             -71.06861   4917 Subscriber       02116 ene vie\\.    0
## 3             -71.08066   2881   Customer       02115 ene vie\\.    0
## 4             -71.08822   4792 Subscriber       02139 ene vie\\.    0
## 5             -71.08579   6062 Subscriber       02139 ene vie\\.    0
## 6             -71.08678   4371 Subscriber       02139 ene vie\\.    0

Primeras representaciones de los datos

Representamos en un gráfico los cuatro meses que se incluyen en el dataset para ver muy en general como están distribuidas las cantidades

ggplot(london_bikes2 %>% 
  group_by(mes) %>% 
  summarise(cantidad=n()))+
  geom_bar(aes(x=mes, weight=cantidad))+
  labs(title="Uso de bicicletas públicas - Londres", subtitle="Uso diario (1er cuatrimestre 2020)", x="mes", y="cantidad")

Como vemos las cantidades de viajes están fuertemente condicionadas por las restricciones en pandemia. Hay que tener en cuenta que durante el 2021, en febrero, se dieron las mayores restricciones a la circulación en el Reino Unido, las mayores liberaciones se dieron a partir de Mayo asi que si incluyera ese més deberíamo apreciar un mayor nivel de uso que en abril.

Podemos ampliar el análisis desagregando el día de la semana para ver si se pueden reconocer patrones de uso

ggplot(london_bikes2 %>% 
  group_by(mes, diasem) %>% 
  summarise(cantidad=n()))+
  geom_bar(aes(x=mes, weight=cantidad, fill=diasem), position="dodge")+
  labs(title="Uso de bicicletas públicas - Londres", subtitle="Uso diario (1er cuatrimestre 2020)", x="mes", y="cantidad")
## `summarise()` has grouped output by 'mes'. You can override using the `.groups` argument.

No se aprecian demasiados patrones, salvo una mayor predominancia de los sábados a medida que se se producián mayor número de movimientos mensuales (seguramente asociado a las restricciones de movilidad)

ggplot(london_bikes2 %>%
         group_by(diasem, hora) %>%
         summarise(cantidad=n()))+
  geom_line(aes(x=hora, y=cantidad, color=diasem))+
  labs(title="Uso de bicicletas públicas - Londres", subtitle="Distribución horaria x dia de la semana", x="hora", y="cantidad")+
  scale_colour_manual(values=c("darkgreen", "blue","darkorange","darkcyan","darkviolet","darkred","green"))
## `summarise()` has grouped output by 'diasem'. You can override using the `.groups` argument.

Se pueden notar dos patrones diferentes, uno para los dias hábiles y otro para los fines de semana, siendo el sabado el de mayor uso. Los días habiles aparecen dos picos en lugar de uno, el primero más bajo que el segundo. Este comportamiento parece razonable, sin embargo sorprenden los horarios a los cuales se dan los picos. No parece estar asociado a horarios laborales. El segundo pico coincide con el horario de cierre de los Pubs!!!

Facetamos para ver si estos patrones se repiten o incrementan en función de cada mes.

ggplot(london_bikes2 %>%
         group_by(mes, diasem, hora) %>%
         summarise(cantidad=n()))+
  geom_line(aes(x=hora, y=cantidad, color=diasem))+
  facet_wrap(~mes)+
  scale_colour_manual(values=c("darkgreen", "blue","darkorange","darkcyan","darkviolet","darkred","green"))
## `summarise()` has grouped output by 'mes', 'diasem'. You can override using the `.groups` argument.

Como era de esperar, y por la diferencia de uso debido a la pandemia, el gáfico de Abril es muy similar al del total. Los otros meses, con su diferencia de escala siguen patrones similares aunque las cantidades de los fines de semana se reducen comparativamente con las de los días hábiles.

Acá hago un stop porque tengo dudas acerca de como seguir, si bien el dataset tiene información geográfica, esta corresponde a las estaciones y dificilmente se pueda encontrar algún patrón geográfico simplemente ploteando los puntos correspondientes a cada estación. Estoy evaluando los siguientes análisis

1- plotear las estaciones haciendo depender el tamaño de los puntos en función de la cantidad de viajes

2- asociar los viajes de cada estación con los distritos en los cuales están ubicadas, luego representar en el mapa los distritos coloreados respectos de la densidad de viajes por unidad de habitante

3- otro/s que me sugieran