First setting up document:
library(foreign)
library(plyr); library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:plyr':
##
## arrange, count, desc, failwith, id, mutate, rename, summarise,
## summarize
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(stringr); library(reshape2)
library(tools) # for showNonASCII
library(sp);library(maptools)
## Checking rgeos availability: TRUE
library(proj4)
library(rgdal)
## Warning: package 'rgdal' was built under R version 3.4.2
## rgdal: version: 1.2-13, (SVN revision 686)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 2.1.3, released 2017/20/01
## Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rgdal/gdal
## Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
## Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rgdal/proj
## Linking to sp version: 1.2-5
##
## Attaching package: 'rgdal'
## The following object is masked from 'package:proj4':
##
## project
library(ggmap)
## Loading required package: ggplot2
library(classInt)
library(maps)
##
## Attaching package: 'maps'
## The following object is masked from 'package:plyr':
##
## ozone
source('~/Dropbox (MIT)/Teaching/PS Methods Workshop/2014 Geospatial/functions.R')
library(RColorBrewer) # color scales
library(classInt) # color scales
library(scales)
library(stargazer); library(xtable)
##
## Please cite as:
## Hlavac, Marek (2015). stargazer: Well-Formatted Regression and Summary Statistics Tables.
## R package version 5.2. http://CRAN.R-project.org/package=stargazer
##
## Attaching package: 'xtable'
## The following object is masked from 'package:maptools':
##
## label
library(tidycensus)
## Warning: package 'tidycensus' was built under R version 3.4.2
library(tigris) # to pull census shapefiles from the web API
## As of version 0.5.1, tigris does not cache downloaded data by default. To enable caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
##
## Attaching package: 'tigris'
## The following object is masked from 'package:graphics':
##
## plot
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3
library(leaflet)
set.seed(02139)
setwd("/Users/justin/Dropbox (MIT)/BARI/ACS 2016 Release")
Going to bring in some ACS data:
b <- read.csv("~/Google Drive/BARI/BARI Research Team Data Library/American Community Survey (1)/Data/ACS_1216_TRACT.csv",stringsAsFactors=F) # 1478 rows
b_old <- read.csv("~/Google Drive/BARI/BARI Research Team Data Library/American Community Survey (1)/Data/Archived Data/ACS_0711_TRACT.csv",stringsAsFactors=F)
# b_poly <- readOGR(dsn="~/Google Drive/BARI/BARI Research Team Data Library/American Community Survey (1)/Data/ACS1216 Tracts Shapefiles/ACS1216_TRACT_SHAPE","")
Now going to bring in some shapefiles from the Census API:
Also going to get some data on the existing bike lanes network in Boston:
Now, lets see what changes over time have been since the 2007-2011 ACS:
b$ByBike_1115 <- b_old$ByBike[match(b$CT_ID_10,b_old$CT_ID_10)]
b$ByBike_delta <- 100*(b$ByBike-b$ByBike_1115)
summary(b$ByBike_delta)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -100.00000 0.00000 0.00000 0.05437 0.42063 7.70777
## NA's
## 15
# write.csv(b,"ACS_1216_bike.csv",row.names = F)
# add these to the polygons data:
boston_tracts@data$ByBike_delta <- b$ByBike_delta[match(boston_tracts@data$GEOID,b$CT_ID_10)]
boston_tracts <- boston_tracts[!is.na(boston_tracts@data$ByBike_delta),]
boston_tracts@data$ByBike_delta[which(boston_tracts@data$ByBike_delta==-100)] <- NA
b$TotalPop_1115 <- b_old$TotalPop[match(b$CT_ID_10,b_old$CT_ID_10)]
boston_df <- b[which(b$CT_ID_10 %in% boston_tracts@data$GEOID),]
sum(boston_df$ByBike*boston_df$TotalPop)/sum(boston_df$TotalPop) # 1.87%
## [1] 0.01870627
sum(boston_df$ByBike_1115*boston_df$TotalPop_1115)/sum(boston_df$TotalPop_1115) # 1.32
## [1] 0.01324652
# Make a simple plot of these tracts:
var <- boston_tracts$ByBike_delta
nclr <- 5
plotclr <- brewer.pal(nclr,"Blues")
class <- classIntervals(var,nclr,style="quantile")
## Warning in classIntervals(var, nclr, style = "quantile"): var has missing
## values, omitted in finding classes
colcode <- findColours(class,plotclr)
# pdf("Figures/bikecommuters_delta.pdf")
# par(mar=c(2,2,4,2))
plot(boston_tracts,col=colcode,border="grey")
# title(main="Demographic change, pre-2015")
legend(x="bottomright",cex=1, fill=attr(colcode,"palette"), bty="n", legend=round.intervals(colcode,rnd=1), title="Tract change 2012-2016 in\n% Commuting by Bike, quantiles",ncol=2,xpd=T)
# dev.off()
# Add in new bike lanes:
# pdf("Figures/bikecommuters_delta_lanes.pdf")
# par(mar=c(2,2,4,2))
plot(boston_tracts,col=colcode,border="grey")
lines(bikelanes[bikelanes$InstallDat<2012,],col="black",lwd=1)
lines(bikelanes[bikelanes$InstallDat>=2012,],col="darkgreen",lwd=2)
legend(x="bottomright",cex=1, fill=attr(colcode,"palette"), bty="n", legend=round.intervals(colcode,rnd=1), title="Tract change 2012-2016 in\n% Commuting by Bike, quantiles",ncol=2,xpd=T)
# dev.off()
Making a nicer map with basemap and zoom-able:
tracts_merged <- geo_join(spatial_data = boston_tracts,data_frame = b, by_sp="GEOID", by_df="CT_ID_10",how="left")
pal <- colorNumeric("RdBu", domain=tracts_merged$ByBike_delta)
# Getting rid of rows with NA values
tracts_merged <- subset(tracts_merged, !is.na(ByBike_delta))
# Setting up the pop up text
popup_ct <- paste0("Tract ",tracts_merged$NAME,": ",as.character(round(tracts_merged$ByBike_delta,2)),"% change, 2011 to 2016")
popup_bikelanes <- paste0(bikelanes$STREET_NAM)
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
# setView(-98.483330, 38.712046, zoom = 4) %>%
addPolygons(data = tracts_merged ,
fillColor = ~pal(tracts_merged$ByBike_delta),
fillOpacity = 0.9,
weight = 0.2,
smoothFactor = 0.2,
highlight = highlightOptions(
weight = 5,
color = "#666",
dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE),
label=popup_ct,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto")) %>%
addPolylines(data = bikelanes[bikelanes$InstallDat>=2012,],
color = "green"
# , highlight = highlightOptions(
# weight = 5,
# color = "green",
# dashArray = "",
# fillOpacity = 0.7,
# bringToFront = TRUE),
# label=popup_bikelanes,
# labelOptions = labelOptions(
# style = list("font-weight" = "normal", padding = "3px 8px"),
# textsize = "15px",
# direction = "auto")
) %>%
# addPolylines(data = bikelanes,
# color = "green") %>%
addLegend(pal = pal,
values = tracts_merged$ByBike_delta,
position = "bottomright",
title = "Tract change 2012-2016 in<br />% Commuting by Bike") %>%
addLegend(
colors = "green", labels="",
position = "bottomright",
title = "New Bike lanes since 2012")
Will people bike if cities build the infrastructure to make it safer on the roads? The City of Boston has sought to further develop cycling infrastructure in the last five years, including commitments to bike lanes on reconstructed roads and the launch of several parking-protected bike lanes in the last year. But are people taking advantage of these efforts?
The BARI team found at least one answer to this question in the recently-released ACS data for the 5-year estimation period of 2012-2016. The team compared these new data with the last five-year estimation period (2007-2011), which are also available on BARI’s Boston Data Portal, to show how people are making use of the bike infrastructure in Boston.
The data show some modest increases across the board in the proportion of Boston residents who are riding bikes to work every day. In the most recent period, 1.87% of all Boston residents reported commuting primarily by bike, while in the previous time period (before 2012), only 1.32% of residents reported doing so. Despite the sometimes-freezing weather in Boston, more of our city is showing enthusiasm for this sustainable form of transportation in recent years.
Where exactly are these Boston bicyclists, and what roads are they using? The BARI team mapped the most recent ACS data to figure this out. The maps below show census tracts in Boston, with their color indicating the percentage-point change in the percent of residents commuting by bike since the pre-2012 period.
The broad increase in bike ridership across the city is evident from the mostly-positive change in ridership across census tracts, with a few exceptions. This increase seems concentrated especially in JP, where the most radical increases in bike ridership exist. As far as interpretation goes, it’s pretty hard to make too much of causality here - after all, this is just co-occurrence of bike infrastructure and trends in bike commute adoption. That means that people could be increasing their cycling because of the newly developed bike infrastructure – or it could be unrelated to new bike lanes and paths. Areas such as Allston/Brighton and Dorchester/Neponset have seen some terrific improvement in bike infrastructure since 2012, and have also seen some larger increases in ridership. However, the large increases in ridership in JP are despite the fact that almost all the increases in bike infrastructure in the last five years have been elsewhere – perhaps reflecting the fact that there was already a great deal of bike infrastructure in JP.