library(tidyverse)
## ── Attaching packages ──────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.1     ✔ purrr   0.3.2
## ✔ tibble  2.1.3     ✔ dplyr   0.8.3
## ✔ tidyr   0.8.3     ✔ stringr 1.4.0
## ✔ readr   1.3.1     ✔ forcats 0.4.0
## ── Conflicts ─────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
viajes <- read.csv("201501-hubway-tripdata.csv")
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.
bbox <- make_bbox (viajes$end.station.longitude, viajes$end.station.latitude)

mapa_base <- get_stamenmap(bbox=bbox,
                           maptype = "toner-lite",
                           zoom=11)
## Source : http://tile.stamen.com/toner-lite/11/619/757.png
ggmap(mapa_base)

ggmap(mapa_base) +
    geom_point(data = viajes, aes(end.station.longitude, end.station.latitude), color = "limegreen")

ggmap(mapa_base) +
    geom_point(data = viajes, aes(end.station.longitude, end.station.latitude, color =end.station.id)) +
    scale_color_distiller(type = "div")

conteo <- viajes %>% 
    group_by(start.station.id, start.station.longitude, start.station.latitude, end.station.id,
             end.station.longitude, end.station.latitude) %>% 
    summarise(cantidad=n())
library(ggplot2)
ggplot() + 
    geom_tile(data = conteo, aes(x =start.station.id, y= end.station.id, fill=cantidad)) + 
scale_fill_distiller(palette = "Spectral")

unique(conteo$"start.station.id")
##  [1]  67  68  70  72  73  74  75  76  80  84  87  88  89  90  91  95  96
## [18]  97 104 105 107 108 110 115 116 117 118 140 141 142 143 145
ggplot() + 
    geom_tile(data = viajes, 
              aes(x = as.factor(start.station.id),
                  y = as.factor(end.station.id),
                  fill = bikeid)) +
    scale_fill_distiller(palette = "Spectral")

top10 <- conteo %>% 
    ungroup() %>% 
    filter(start.station.id != end.station.id) %>% 
    top_n(10)
## Selecting by cantidad
top10
## # A tibble: 10 x 7
##    start.station.id start.station.l… start.station.l… end.station.id
##               <int>            <dbl>            <dbl>          <int>
##  1               67            -71.1             42.4             68
##  2               67            -71.1             42.4            107
##  3               68            -71.1             42.4             88
##  4               75            -71.1             42.4             67
##  5               80            -71.1             42.4             68
##  6               88            -71.1             42.4             76
##  7               91            -71.1             42.4            107
##  8              107            -71.1             42.4             67
##  9              107            -71.1             42.4             91
## 10              116            -71.1             42.4             76
## # … with 3 more variables: end.station.longitude <dbl>,
## #   end.station.latitude <dbl>, cantidad <int>
ggplot() + 
    geom_tile(data = top10, 
              aes(x = as.factor(start.station.id),
                  y = as.factor(end.station.id),
                  fill = cantidad)) +
    scale_fill_distiller(palette = "Spectral")

library(osrm)
## Data: (c) OpenStreetMap contributors, ODbL 1.0 - http://www.openstreetmap.org/copyright
## Routing: OSRM - http://project-osrm.org/
ruteo <- function(o_nombre, o_x, o_y, d_nombre, d_x, d_y) {
  
  ruta <- osrmRoute(src = c(o_nombre, o_x, o_y),
                    dst = c(d_nombre, d_x, d_y), 
                    returnclass = "sf",
                    overview = "full")
  
  cbind(ORIGEN = o_nombre, DESTINO = d_nombre, ruta)
  
}
ruteo_boston <- list(top10$start.station.id, top10$start.station.longitude,top10$start.station.latitude,
                   top10$end.station.id, top10$end.station.longitude,top10$end.station.latitude)

ruteo_boston_ <- pmap(ruteo_boston, ruteo) %>% 
  reduce(rbind)
library(leaflet)
leaflet(ruteo_boston_) %>%
  addTiles() %>%
  addProviderTiles(providers$CartoDB.DarkMatter) %>%
  addPolylines(color = ~colorNumeric("BrBG", ruteo_boston_$distance)(distance),
              label = ruteo_boston_$distance %>%
              lapply(htmltools::HTML)) %>%
  addLegend("bottomright", pal = colorNumeric("BrBG", ruteo_boston_$distance), values = ~distance,
            title = "Distancia",
            opacity = 0.55)