authors: Peter Carbonetto, Gao Wang
In this last analysis, we use the Divvy trip data to examine biking trends in Chicago over the course of one year.
We begin by loading a few packages, as well as some additional R functions implemented for this project. The repr
package is used to adjust the dimensions of the figures in the Jupyter notebook.
library(repr)
library(data.table)
library(ggplot2)
source("../code/functions.R")
First, we read in the Divvy trip and station data from the CSV files.
divvy <- read.divvy.data()
To analyze city-wide departures for each day of the year, we create a "day of year" column.
divvy$trips <-
transform(divvy$trips,
start.dayofyear = factor(as.numeric(format(divvy$trips$starttime,"%j")),
1:366))
We also convert the "start week" column to a factor to make it easier to compile trip statistics for each week in the year.
divvy$trips <- transform(divvy$trips,start.week = factor(start.week,0:52))
Here, we create a new vector containing the number of trips taken in each day of the year, then we plot these numbers.
options(repr.plot.width = 5, repr.plot.height = 2.25)
counts.day <- as.vector(table(divvy$trips$start.dayofyear))
ggplot(data.frame(day = 1:366,departures = counts.day),
aes(x = day,y = departures)) +
geom_point(color = "darkblue",shape = 19,size = 1) +
geom_line(color = "darkblue") +
scale_x_continuous(breaks = seq(0,350,25)) +
theme_minimal() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
This plot shows a sizeable increase in bike trips during summer days, but since the number of trips varies widely from one day to the next, the plot is expected to look nicer if instead the number of trips are counted per week.
options(repr.plot.width = 5, repr.plot.height = 2.25)
counts.week <- as.vector(table(divvy$trips$start.week))
ggplot(data.frame(week = 0:52,departures = counts.week),
aes(x = week,y = departures)) +
geom_point(color = "darkblue",shape = 19,size = 1.5) +
geom_line(color = "darkblue") +
theme_minimal() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
Indeed, the seasonal trends are less noisy in this plot; the majority of Divvy bike trips in Chicago are taken when the weather is warmer (weeks 20--40), and very few people are using the Divvy bikes in the cold winter months.
When we analyze trips taken at the University of Chicago bike station, the "bump" during warmer months flattens out. This is probably because a large fraction of University of Chicago students leave during the summer.
options(repr.plot.width = 5, repr.plot.height = 2.25)
dat <- subset(divvy$trips,from_station_name == "University Ave & 57th St")
counts.week.uchicago <- as.vector(table(dat$start.week))
ggplot(data.frame(week = 0:52,departures = counts.week.uchicago),
aes(x = week,y = departures)) +
geom_point(color = "darkblue",shape = 19,size = 1.5) +
geom_line(color = "darkblue") +
theme_minimal() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
This is the version of Jupyter used to generate these results.
system("jupyter --version",intern = TRUE)
This is the version of R and the packages that were used to generate these results.
sessionInfo()
Exported from analysis/seasonal-trends.ipynb
committed by Peter Carbonetto on Fri Jul 14 22:19:28 2017 revision 6, abdcabe