/
biketown.Rmd
71 lines (53 loc) · 1.91 KB
/
biketown.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
---
title: "Playing with Biketown Data"
author: "Ted Laderas"
date: "March 25, 2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
set.seed(111)
library(sf)
library(leaflet)
library(here)
```
Here's how I processed the data.
```{r eval=FALSE}
biketown <- readr::read_csv(here("data-raw", "week10_biketown.csv"))
biketown <- biketown[sample(nrow(biketown), 50000),]
biketown2 <- biketown %>% tidyr::drop_na(StartLatitude, StartLongitude) %>% st_as_sf(coords = c("StartLongitude", "StartLatitude"),crs = 4326)
```
```{r}
data("bike_points", package="dataLiteracyTutorial")
data("port_bike", package="dataLiteracyTutorial")
biketown2 <- biketown2[sample(nrow(biketown2), 1000),]
#inital leaflet
biketown2 %>% st_transform(crs = 4326) %>% # leaflet crs is in 4326
leaflet() %>% addMarkers() %>% addTiles()
```
```{r eval=FALSE}
portland_neighborhoods <- st_read(here("data","Neighborhoods_Regions.shp"), crs = 4326)
neighborhoods_bike <- st_join(biketown2,
portland_neighborhoods,
join=st_within) %>%
as_tibble() %>% group_by(NAME) %>%
summarise(num_rentals = n(), ave_distance=mean(Distance_miles)) %>%
select(neighborhood = NAME, num_rentals, ave_distance)
port_bike <- portland_neighborhoods %>% dplyr::left_join(y=neighborhoods_bike, by=c("NAME"="neighborhood"))
```
```{r}
bins <- c(1, 200, 600, 1000, 2000, 5000, Inf)
pal <- colorBin("YlOrRd", domain = port_bike$num_rentals, bins = bins)
labels <- sprintf(
"<strong>%s</strong><br/>Number Bike Rentals: %g",
port_bike$NAME, port_bike$num_rentals
) %>% lapply(htmltools::HTML)
port_bike %>% leaflet() %>%
addPolygons(fillColor = ~pal(num_rentals), weight = 2,
opacity = 1,
color = "white",
dashArray = "3", label = labels,
fillOpacity = 0.5) %>% addTiles()
#save(port_bike, file = "data/port_bike.rda")
#save(biketown2, file = "data/bike_points.rda")
```