library(leaflet)
library(rgdal)
## Loading required package: sp
## rgdal: version: 1.1-10, (SVN revision 622)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.0.1, released 2015/09/15
##  Path to GDAL shared files: C:/Users/Chris Iyer/Documents/R/win-library/3.3/rgdal/gdal
##  Loaded PROJ.4 runtime: Rel. 4.9.2, 08 September 2015, [PJ_VERSION: 492]
##  Path to PROJ.4 shared files: C:/Users/Chris Iyer/Documents/R/win-library/3.3/rgdal/proj
##  Linking to sp version: 1.2-3
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggmap)
## Loading required package: ggplot2
library(knitr)

Create a data frame me_uni that has the name of each institution and the address in columns name and addr then display the entire data frame. me_uni

name <- c("USM", "UMaine", "UM Farmington")
address <- c("96 Falmouth St., Portland, ME", "University of Maine, Orono, ME", "224 Main St., Farmington, ME")
me_uni <- data.frame(name, address, stringsAsFactors = FALSE)
kable(me_uni)
name address
USM 96 Falmouth St., Portland, ME
UMaine University of Maine, Orono, ME
UM Farmington 224 Main St., Farmington, ME

Also format to convert addresses to proper case. Notice how it changes the state too.

Create a data frame with Edith’s schools. Call it E_Schools

library(tools)
library(stringr)
School <- c("Comprehensive Model School Project, M.S.", "Angela Patri M.S.", "Bronx Green Middle School", "Jackie Robinson School, P.S. 375", "Franklin K. Lane H.S.", "Rockaway Collegiate H.S.", "Knowledge And Power Preparatory Academy VI")
Address <- c("1501 JEROME Ave. Bronx, NY 10452", "2225 Webster Ave. Bronx, NY 10457", "2441 Wallace Ave. Bronx, NY 10467", "46 McKeever Pl. Brooklyn, NY 11225", "999 Jamaica Ave. Brooklyn, NY 11208", "100-00 Beach Channel Dr. Queens, NY 11694", "8-21 Bay 25 St. Queens, NY 11691")
#Convert JEROME to proper case
Address <-  str_to_title(Address)
E_Schools <- data.frame(School, Address, stringsAsFactors = FALSE)
kable(E_Schools)
School Address
Comprehensive Model School Project, M.S. 1501 Jerome Ave. Bronx, Ny 10452
Angela Patri M.S. 2225 Webster Ave. Bronx, Ny 10457
Bronx Green Middle School 2441 Wallace Ave. Bronx, Ny 10467
Jackie Robinson School, P.S. 375 46 Mckeever Pl. Brooklyn, Ny 11225
Franklin K. Lane H.S. 999 Jamaica Ave. Brooklyn, Ny 11208
Rockaway Collegiate H.S. 100-00 Beach Channel Dr. Queens, Ny 11694
Knowledge And Power Preparatory Academy VI 8-21 Bay 25 St. Queens, Ny 11691
str(E_Schools)
## 'data.frame':    7 obs. of  2 variables:
##  $ School : chr  "Comprehensive Model School Project, M.S." "Angela Patri M.S." "Bronx Green Middle School" "Jackie Robinson School, P.S. 375" ...
##  $ Address: chr  "1501 Jerome Ave. Bronx, Ny 10452" "2225 Webster Ave. Bronx, Ny 10457" "2441 Wallace Ave. Bronx, Ny 10467" "46 Mckeever Pl. Brooklyn, Ny 11225" ...

Use ggmap to geocode the addresses and store the geocodes into a variable called me_uni_loc. Show the contents of your new variable.

me_uni_loc

#me_uni_loc <- geocode(me_uni$address)
#me_uni_loc
E_Sc_loc <- geocode(E_Schools$Address)
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=1501%20Jerome%20Ave.%20Bronx,%20Ny%2010452&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=2225%20Webster%20Ave.%20Bronx,%20Ny%2010457&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=2441%20Wallace%20Ave.%20Bronx,%20Ny%2010467&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=46%20Mckeever%20Pl.%20Brooklyn,%20Ny%2011225&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=999%20Jamaica%20Ave.%20Brooklyn,%20Ny%2011208&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=100-00%20Beach%20Channel%20Dr.%20Queens,%20Ny%2011694&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=8-21%20Bay%2025%20St.%20Queens,%20Ny%2011691&sensor=false
E_Sc_loc
##         lon      lat
## 1 -73.91638 40.84281
## 2 -73.89704 40.85465
## 3 -73.86459 40.86206
## 4 -73.95952 40.66488
## 5 -73.86907 40.69301
## 6 -73.82344 40.58667
## 7 -73.76398 40.60153

Add lon and lat columns to me_uni and display the data frame. Try to use the bind_cols function in dplyr. After loading dplyr, type ?bind_cols for help.

Can use bind_cold or cbind

#str(me_uni_loc)
#me_uni <- bind_cols(me_uni, me_uni_loc)
#kable(me_uni)
#univ_df <- cbind(me_uni,me_uni_loc)
#kable(me_uni)
E_Schools <- bind_cols(E_Schools, E_Sc_loc)
kable(E_Schools)
School Address lon lat
Comprehensive Model School Project, M.S. 1501 Jerome Ave. Bronx, Ny 10452 -73.91638 40.84281
Angela Patri M.S. 2225 Webster Ave. Bronx, Ny 10457 -73.89704 40.85465
Bronx Green Middle School 2441 Wallace Ave. Bronx, Ny 10467 -73.86459 40.86206
Jackie Robinson School, P.S. 375 46 Mckeever Pl. Brooklyn, Ny 11225 -73.95952 40.66488
Franklin K. Lane H.S. 999 Jamaica Ave. Brooklyn, Ny 11208 -73.86907 40.69301
Rockaway Collegiate H.S. 100-00 Beach Channel Dr. Queens, Ny 11694 -73.82344 40.58667
Knowledge And Power Preparatory Academy VI 8-21 Bay 25 St. Queens, Ny 11691 -73.76398 40.60153
str(E_Schools)
## 'data.frame':    7 obs. of  4 variables:
##  $ School : chr  "Comprehensive Model School Project, M.S." "Angela Patri M.S." "Bronx Green Middle School" "Jackie Robinson School, P.S. 375" ...
##  $ Address: chr  "1501 Jerome Ave. Bronx, Ny 10452" "2225 Webster Ave. Bronx, Ny 10457" "2441 Wallace Ave. Bronx, Ny 10467" "46 Mckeever Pl. Brooklyn, Ny 11225" ...
##  $ lon    : num  -73.9 -73.9 -73.9 -74 -73.9 ...
##  $ lat    : num  40.8 40.9 40.9 40.7 40.7 ...

Using leaflet plot the points as pins using me_uni$name as the popup text.

#note, just the name pops up. 
#m <- leaflet() %>% addTiles() %>% addMarkers(lng = me_uni_loc$lon, lat =  me_uni_loc$lat, popup = me_uni$name)
#m
nyc <- leaflet() %>% addProviderTiles("CartoDB.Positron") %>% addMarkers(lng = E_Schools$lon, lat = E_Sc_loc$lat, popup = E_Schools$School)
nyc

Include addresses in the popups, bold the name, and include labels for both(same map as above)

#label with a bold name and the address. 
#pops <- paste("<strong>Name: ",
              #me_uni$name,
              #"</strong><br>",
              #"Address: ",
              #me_uni$address)
#m <- leaflet() %>%
  #addTiles() %>%
  #addMarkers(lng = me_uni$lon, lat = me_uni$lat, popup = pops)
#m
label <- paste("<strong>School: ", E_Schools$School, "</strong><br>", "Address: ", E_Schools$Address) 

map <- leaflet() %>% addTiles() %>% addMarkers(lng = E_Schools$lon, lat = E_Schools$lat, popup = label)
map

Add a driving route to your map from USM to Farmington, color the route “blue”

#USM <- "96 Falmoth St., Portland, ME"
#UMF <- "224 Main St., Farmington, ME"
#drive <- route(me_uni$address[1], me_uni$address[3], mode = "driving", structure = "route")

#m <- leaflet() %>% addTiles() %>% addMarkers(lng = me_uni$lon, lat = me_uni$lat, popup = pops) %>% addPolylines(lng = drive$lon, lat = #drive$lat, fill=FALSE, color ="blue")
#m

Bronx Green to Rockaway Collegiate

path <- route(E_Schools$Address[3], E_Schools$Address[6], mode = "driving", structure = "route")
## Information from URL : http://maps.googleapis.com/maps/api/directions/json?origin=2441+Wallace+Ave.+Bronx,+Ny+10467&destination=100-00+Beach+Channel+Dr.+Queens,+Ny+11694&mode=driving&units=metric&alternatives=false&sensor=false

Question 7 Add a second driving route from USM to UMaine and color it “green”. Also, use the Stamen.Watercolor map tiles.

path <- route(E_Schools$Address[3], E_Schools$Address[6], mode = "driving", structure = "route")
## Information from URL : http://maps.googleapis.com/maps/api/directions/json?origin=2441+Wallace+Ave.+Bronx,+Ny+10467&destination=100-00+Beach+Channel+Dr.+Queens,+Ny+11694&mode=driving&units=metric&alternatives=false&sensor=false
path2 <- route(E_Schools$Address[3], E_Schools$Address[4], mode = "driving", structure = "route")
## Information from URL : http://maps.googleapis.com/maps/api/directions/json?origin=2441+Wallace+Ave.+Bronx,+Ny+10467&destination=46+Mckeever+Pl.+Brooklyn,+Ny+11225&mode=driving&units=metric&alternatives=false&sensor=false
map <- leaflet() %>% addProviderTiles("Stamen.Watercolor") %>% addMarkers(lng = E_Schools$lon, lat = E_Schools$lat, popup = label) %>% addPolylines(lng = path2$lon, lat = path2$lat, fill = FALSE, color = "blue") %>% addPolylines(lng = path$lon, lat = path$lat, fill = FALSE, color = "green")
map

Read in the Maine Counties shapefile we used in the lecture notes and add that to the map.

#library(rgdal)
#me_counties <- readOGR("./county", "county")
#kable(head(me_counties))
#names(me_counties)
#me_counties <- spTransform(me_counties, CRS("+proj=longlat +datum=WGS84"))

#m <-leaflet() %>%
#addProviderTiles("Stamen.Toner") %>% 
#addMarkers(lng = me_uni$lon, lat = me_uni$lat, popup = pops) %>% addPolylines(lng = drive$lon, lat = drive$lat, fill = FALSE, color = "#84080E")%>% addPolygons(data = me_counties) 
#m
crime <- read.csv("Crime_by_County_2013.csv", header = TRUE, stringsAsFactors = FALSE)
names(crime)
##  [1] "County"                     "Annual.Crime.Rat.per.1.000"
##  [3] "Total.Index.Crimes"         "Murder"                    
##  [5] "Rape"                       "Robbery"                   
##  [7] "Aggravated.Assault"         "Burglary"                  
##  [9] "Larceny"                    "Motor.Vehicle.Theft"       
## [11] "Arson"                      "Percent.Clearance"         
## [13] "Location.1"
#me_crime_county <- merge(me_counties, crime, by.x="COUNTY", by.y = "County")

#county_popup <- paste("<strong>County: </strong>", me_crime_county$COUNTY, "<br><strong>Crime Rate (per m: </strong>", me_crime_county$ Annual.Crime.Rat.per.1.000, "<br><strong>Robbery (n): </strong>", me_crime_county$Robbery)
#pal <- colorQuantile("YlGn", NULL, n = 4)

#leaflet() %>% addProviderTiles("Stamen.Toner") %>% addPolygons(data = me_crime_county, fillColor = ~pal(Annual.Crime.Rat.per.1.000), color = "#84080E", weight = 1, popup= ~county_popup)