Stat 651 homework 3

Author

Yogesh Gupta

library(pacman)
p_load(mosaicData,tidyverse, ggExtra, macleish, leaflet, mdsr, tictoc, sf, tidygeocoder, ggspatial, mapproj, maps)

Problem 1 (Chapter 14 Problem 6)

Link to Tableau Plot

Problem 2 (Chapter 17 Exercises Problem 1)

Use the geocode function from the tidygeocoder package to find the latitude and longitude of the Emily Dickinson Museum in Amherst, Massachusetts.

library(mdsr)
library(tidyverse)
emily_dickinson <- tibble(
  address = "Emily Dickinson Museum in Amherst, Massachusetts"
) %>% 
  tidygeocoder::geocode(address, method="osm")
Passing 1 address to the Nominatim single address geocoder
Query completed in: 1 seconds
emily_dickinson
# A tibble: 1 × 3
  address                                            lat  long
  <chr>                                            <dbl> <dbl>
1 Emily Dickinson Museum in Amherst, Massachusetts  42.4 -72.5

Assuming “long” and “lat” are longitude and latitude, respectively

library(leaflet)
emily_dickinson_map <- leaflet() %>% 
  addTiles() %>% 
  addMarkers(data= emily_dickinson)
Assuming "long" and "lat" are longitude and latitude, respectively
emily_dickinson_map

Problem 3 (Chapter 18 Problem 1)

A. Use the geocode function in tidygeocoder to obtain spatial coordinates for these restaurants.

head(Violations)
# A tibble: 6 × 16
     camis dba   boro  building street zipcode  phone inspection_date     action
     <int> <chr> <chr>    <int> <chr>    <int>  <dbl> <dttm>              <chr> 
1 30075445 MORR… BRONX     1007 MORRI…   10462 7.19e9 2015-02-09 00:00:00 Viola…
2 30075445 MORR… BRONX     1007 MORRI…   10462 7.19e9 2014-03-03 00:00:00 Viola…
3 30075445 MORR… BRONX     1007 MORRI…   10462 7.19e9 2013-10-10 00:00:00 No vi…
4 30075445 MORR… BRONX     1007 MORRI…   10462 7.19e9 2013-09-11 00:00:00 Viola…
5 30075445 MORR… BRONX     1007 MORRI…   10462 7.19e9 2013-09-11 00:00:00 Viola…
6 30075445 MORR… BRONX     1007 MORRI…   10462 7.19e9 2013-08-14 00:00:00 Viola…
# … with 7 more variables: violation_code <chr>, score <int>, grade <chr>,
#   grade_date <dttm>, record_date <dttm>, inspection_type <chr>,
#   cuisine_code <dbl>
Violations <- Violations %>% mutate(state = "NY")

Since the Violations dataset has multiple data on the location, unique function is used.

combine_locations <- Violations %>% 
  filter(building != 0) %>% 
  select(street, zipcode, dba, boro, state) %>% 
  unite(loc, street, boro, state, zipcode, sep=",")
unique_locations <- combine_locations %>% 
  unique() %>% 
  drop_na()
count_location <- combine_locations %>% group_by(loc) %>% 
  summarise(
    count= n()
  )
remove_na <- unique_locations[1:100,] %>% 
  drop_na()

Removing na values

restaurants <- tibble(address = remove_na$loc, business = remove_na$dba) %>% 
tidygeocoder::geocode(address, method = "osm")
Passing 93 addresses to the Nominatim single address geocoder
Query completed in: 133.1 seconds
restaurants
# A tibble: 100 × 4
   address                             business                        lat  long
   <chr>                               <chr>                         <dbl> <dbl>
 1 MORRIS PARK AVE,BRONX,NY,10462      MORRIS PARK BAKE SHOP          40.9 -73.9
 2 FLATBUSH AVENUE,BROOKLYN,NY,11225   WENDY'S                        40.7 -74.0
 3 WEST   57 STREET,MANHATTAN,NY,10019 DJ REYNOLDS PUB AND RESTAURA…  40.8 -74.0
 4 STILLWELL AVENUE,BROOKLYN,NY,11224  RIVIERA CATERER                40.6 -74.0
 5 ASTORIA BOULEVARD,QUEENS,NY,11369   BRUNOS ON THE BOULEVARD        40.8 -73.9
 6 AVENUE U,BROOKLYN,NY,11234          WILKEN'S FINE FOOD             40.6 -73.9
 7 11 AVENUE,BROOKLYN,NY,11219         REGINA CATERERS                40.6 -74.0
 8 NOSTRAND AVENUE,BROOKLYN,NY,11226   TASTE THE TROPICS ICE CREAM    40.7 -73.9
 9 SOUTHERN BOULEVARD,BRONX,NY,10460   WILD ASIA                      40.9 -73.9
10 EAST   66 STREET,MANHATTAN,NY,10065 1 EAST 66TH STREET KITCHEN     40.8 -74.0
# … with 90 more rows
saveRDS(restaurants, "restaurants.rds")
restaurants <- readRDS("restaurants.rds")

B. Using the spatial coordinates you obtained in the previous exercise, create an informative static map using ggspatial that illustrates the nature and extent of restaurant violations in New York City.

rest <- restaurants %>% drop_na()
restaurant_loc <- rest %>% 
  st_as_sf(coords=c("long","lat")) %>% 
  st_set_crs(4326)
restaurant_loc
Simple feature collection with 91 features and 2 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: -77.5925 ymin: 40.51693 xmax: -73.69327 ymax: 43.15002
Geodetic CRS:  WGS 84
# A tibble: 91 × 3
   address                             business                    geometry
 * <chr>                               <chr>                    <POINT [°]>
 1 MORRIS PARK AVE,BRONX,NY,10462      MORRIS PARK BA… (-73.86115 40.85394)
 2 FLATBUSH AVENUE,BROOKLYN,NY,11225   WENDY'S         (-73.96067 40.66055)
 3 WEST   57 STREET,MANHATTAN,NY,10019 DJ REYNOLDS PU… (-73.98276 40.76667)
 4 STILLWELL AVENUE,BROOKLYN,NY,11224  RIVIERA CATERER  (-73.98302 40.5849)
 5 ASTORIA BOULEVARD,QUEENS,NY,11369   BRUNOS ON THE … (-73.87276 40.76249)
 6 AVENUE U,BROOKLYN,NY,11234          WILKEN'S FINE …  (-73.9225 40.60991)
 7 11 AVENUE,BROOKLYN,NY,11219         REGINA CATERERS (-73.99917 40.57778)
 8 NOSTRAND AVENUE,BROOKLYN,NY,11226   TASTE THE TROP…  (-73.9482 40.67838)
 9 SOUTHERN BOULEVARD,BRONX,NY,10460   WILD ASIA       (-73.88073 40.85681)
10 EAST   66 STREET,MANHATTAN,NY,10065 1 EAST 66TH ST…  (-73.9607 40.77639)
# … with 81 more rows
st_bbox(restaurant_loc)
     xmin      ymin      xmax      ymax 
-77.59250  40.51693 -73.69327  43.15002 
restaurant_merged <- restaurant_loc %>% 
  left_join(count_location, by = c("address" = "loc"))
drop_out <- restaurant_merged[-c(63,64),]
ggplot(drop_out) + 
  annotation_map_tile(type = "osm", zoomin=0) + 
  geom_sf(aes(size = count), alpha = 0.7)
Loading required namespace: raster
Zoom: 11

C. Using the spatial coordinates you obtained in the previous exercises, create an informative interactive map using leaflet that illustrates the nature and extent of restaurant violations in New York City.

restaurant_merged2 <- rest %>% 
  left_join(count_location, by = c("address" = "loc"))

drop_out2 <- restaurant_merged2[-c(63,64),]
leaflet() %>% 
  addTiles() %>% 
  addMarkers(
    lng = drop_out2$long, 
    lat = drop_out2$lat, 
    popup = drop_out2$business
             )

Chapter 18 Problem 2

A. Use the spatial data in the macleish package and ggspatial to make an informative static map of the MacLeish Field Station property.

library(macleish)
library(ggspatial)
boundary <- macleish_layers %>%
  pluck("boundary")
streams <- macleish_layers %>%
  pluck("streams")
buildings <- macleish_layers %>%
  pluck("buildings")
trails <- macleish_layers %>%
  pluck("trails")
landmarks <- macleish_layers %>%
  pluck("landmarks")

boundary_plot <- ggplot(boundary) + 
  geom_sf() + 
  scale_x_continuous(breaks = c(-72.677, -72.683))

boundary_plot +
  geom_sf(data = streams, color = "blue", size = 1.5) +
  geom_sf(data = buildings, color = "red") +
  geom_sf(data = trails, color = "brown") +
  geom_sf(data = landmarks, color = "orange") 

B. Use the spatial data in the macleish package and leaflet to make an informative interactive map of the MacLeish Field Station property.

library(leaflet)
leaflet() %>%
  addTiles() %>%
  addPolygons(
    data = macleish_layers[["boundary"]],
    weight = 1
  ) %>%
  addPolygons(
    data = macleish_layers[["buildings"]],
    weight = 1
  ) %>%
  addMarkers(
    data = filter(macleish_layers[["landmarks"]], grepl("Met", Label)),
    popup = ~Label
  ) %>% 
  addPolylines(
    data = macleish_layers[["trails"]],
    weight = 1
  ) %>% 
  addPolylines(
    data = macleish_layers[["streams"]],
    weight = 1
  )