#Load packages
library(leaflet)
library(rgdal)
Creating Map
# Store values into variables
myLAT <- 42.377504
myLNG <- -71.087080
myZOOM <- 12
myTILES <- "CartoDB.Positron"
# Create a map using leaflet and store that map in the variable mymap
mymap <- leaflet() %>%
addProviderTiles(myTILES) %>%
setView(myLNG, myLAT, zoom = myZOOM)
# Display the map by just using the variable name itself
mymap
Adding Transit Layers
# Import subway lines shape file
subway <- readOGR(dsn="transitlayers/mbtarapidtransit", layer="MBTA_ARC")
## OGR data source with driver: ESRI Shapefile
## Source: "/Users/callum/Desktop/Transportation Planning/transitlayers/mbtarapidtransit", layer: "MBTA_ARC"
## with 138 features
## It has 4 fields
subway <- spTransform(subway, CRS("+proj=longlat +ellps=GRS80"))
# Create map
mymap <- leaflet() %>%
addProviderTiles(myTILES) %>%
setView(myLNG, myLAT, zoom = myZOOM) %>%
addPolylines(data = subway)
# Draw map
mymap
Adding color to the map
# Map colors to the different subway lines
pal <- colorFactor(palette = c('blue','green','orange','red','grey'),
domain = c('BLUE','GREEN','ORANGE','RED','SILVER'))
# Create the map
mymap <- leaflet() %>%
addProviderTiles(myTILES) %>%
setView(myLNG, myLAT, zoom = 12) %>%
addPolylines(data = subway, color=~pal(LINE))
# Draw the map
mymap
Adding stations to the map
# Define new MBTA icon
mbtaIcon <- makeIcon(
iconUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/64/MBTA.svg/240px-MBTA.svg.png",
iconWidth = 15, iconHeight = 15
)
# Load the MBTA stops shapefile
stops <- readOGR(dsn="transitlayers/mbtarapidtransit", layer="MBTA_NODE")
## OGR data source with driver: ESRI Shapefile
## Source: "/Users/callum/Desktop/Transportation Planning/transitlayers/mbtarapidtransit", layer: "MBTA_NODE"
## with 166 features
## It has 4 fields
stops <- spTransform(stops, CRS("+proj=longlat +ellps=GRS80"))
# Create the map
mymap <- leaflet() %>%
addProviderTiles(myTILES) %>%
setView(myLNG, myLAT, zoom = 12) %>%
addPolylines(data = subway, color=~pal(LINE)) %>%
addMarkers(data = stops, icon=mbtaIcon)
# Draw the map
mymap
Add commuter rail lines to the map
# Import rail lines
trains <- readOGR(dsn="transitlayers/trains", layer="TRAINS_ARC")
## OGR data source with driver: ESRI Shapefile
## Source: "/Users/callum/Desktop/Transportation Planning/transitlayers/trains", layer: "TRAINS_ARC"
## with 6595 features
## It has 28 fields
## Integer64 fields read as strings: TRACK LINE_CODE ASSET_ID
trains <- spTransform(trains, CRS("+proj=longlat +ellps=GRS80"))
# Subset to only include commuter rail
trains <- subset(trains, COMMRAIL == "Y", na.rm=TRUE)
# Import rail stations
trainstops <- readOGR(dsn="transitlayers/trains", layer="TRAINS_NODE")
## OGR data source with driver: ESRI Shapefile
## Source: "/Users/callum/Desktop/Transportation Planning/transitlayers/trains", layer: "TRAINS_NODE"
## with 387 features
## It has 6 fields
trainstops <- spTransform(trainstops, CRS("+proj=longlat +ellps=GRS80"))
# Subset to only include commuter rail stations
trainstops <- subset(trainstops, trainstops@data$C_RAILSTAT == "Y", na.rm=TRUE)
# Make a purple circle icon for the commuter rail stations
crIcon <- makeIcon(
iconUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/8/80/Eo_circle_purple_circle.svg/240px-Eo_circle_purple_circle.svg.png", iconWidth = 15, iconHeight = 15)
# Create the map
mymap <- leaflet() %>%
addProviderTiles(myTILES) %>%
setView(myLNG, myLAT, zoom = 12) %>%
addPolylines(data = trains, color="purple", weight=2) %>%
addPolylines(data = subway, color=~pal(LINE)) %>%
addMarkers(data = stops, icon=mbtaIcon) %>%
addMarkers(data = trainstops, icon=crIcon)
# Display the map
mymap
Add bus routes
# Import bus routes shapefile
bus <- readOGR(dsn="transitlayers/mbtabus", layer="MBTABUSROUTES_ARC")
## OGR data source with driver: ESRI Shapefile
## Source: "/Users/callum/Desktop/Transportation Planning/transitlayers/mbtabus", layer: "MBTABUSROUTES_ARC"
## with 923 features
## It has 11 fields
## Integer64 fields read as strings: CTPS_ROU_2
bus <- spTransform(bus, CRS("+proj=longlat +ellps=GRS80"))
# Subset the bus routes into "key routes" and "non-key-routes"
keybuslist <- as.character(c(1,15,22,23,28,32,39,57,66,71,73,77,111,116,117))
bus.key <- subset(bus, MBTA_ROUTE %in% keybuslist, na.rm=TRUE)
bus.nokey <- subset(bus, !(MBTA_ROUTE %in% keybuslist), na.rm=TRUE)
# Create the map
mymap <- leaflet() %>%
addProviderTiles(myTILES) %>%
setView(myLNG, myLAT, zoom = 12) %>%
addPolylines(data = bus.nokey, color="grey", weight=1) %>%
addPolylines(data = bus.key, color="yellow", weight=2) %>%
addPolylines(data = trains, color="purple", weight=2) %>%
addPolylines(data = subway, color=~pal(LINE)) %>%
addMarkers(data = stops, icon=mbtaIcon) %>%
addMarkers(data = trainstops, icon=crIcon)
# Display the map
mymap