Homework3

Author

Dan Hoang_cu2107

Published

November 28, 2022

Problem 1 Tableau

Link

https://public.tableau.com/views/hw3_problem1/Dashboard1?:language=en-US&:display_count=n&:origin=viz_share_link

Screenshot

Problem 17.1

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

library(pacman)
p_load(mosaicData,tidyverse, ggExtra, macleish, leaflet, mdsr, tictoc, sf, tidygeocoder, ggspatial, mapproj, maps, ggmap, dplyr)
ggmap::register_google(key = "AIzaSyCGPPRgTcFbQUi4LNVc-TVZaaE17jzDa5M")
geo1 <- 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
geo1
# A tibble: 1 × 3
  address                                            lat  long
  <chr>                                            <dbl> <dbl>
1 Emily Dickinson Museum in Amherst, Massachusetts  42.4 -72.5
ggmap_center <- ggmap::get_googlemap(center = c(lon = geo1$long, lat = geo1$lat), 
                                     zoom = 10,
                                     maptype = 'roadmap')
ℹ <https://maps.googleapis.com/maps/api/staticmap?center=42.376146,-72.514404&zoom=10&size=640x640&scale=2&maptype=roadmap&key=xxx-TVZaaE17jzDa5M>
ggmap(ggmap_center) +
  geom_point(data = geo1, aes(x = long, y =  lat), size = 7,  color="red", alpha = 0.4) 

Problem 18.1

a.

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

library(mdsr)
head(Violations)
# A tibble: 6 × 16
     camis dba    boro  build…¹ street zipcode  phone inspection_date     action
     <int> <chr>  <chr>   <int> <chr>    <int>  <dbl> <dttm>              <chr> 
1 30075445 MORRI… BRONX    1007 MORRI…   10462 7.19e9 2015-02-09 00:00:00 Viola…
2 30075445 MORRI… BRONX    1007 MORRI…   10462 7.19e9 2014-03-03 00:00:00 Viola…
3 30075445 MORRI… BRONX    1007 MORRI…   10462 7.19e9 2013-10-10 00:00:00 No vi…
4 30075445 MORRI… BRONX    1007 MORRI…   10462 7.19e9 2013-09-11 00:00:00 Viola…
5 30075445 MORRI… BRONX    1007 MORRI…   10462 7.19e9 2013-09-11 00:00:00 Viola…
6 30075445 MORRI… 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>, and abbreviated variable name ¹​building
Violations1<-Violations %>% drop_na(violation_code) ## choose the data with real violations

## combine address 
Violations1 <- Violations1 %>% mutate(state = "NYC") ## to specify all the locations are in the New York city, we create a new variable: state.

caddress <-Violations1 %>%
  select(dba,street,boro,state,zipcode)%>%
  mutate(loc=paste(street,boro,state,zipcode,sep=",")) 
## count the times of violation of a restaurant got.
count_viola <- caddress %>% group_by(dba,loc) %>% 
  summarise(
    count= n()
  )  
`summarise()` has grouped output by 'dba'. You can override using the `.groups`
argument.
Violations2<-Violations1 %>%
  select(street,dba,boro,state,zipcode) %>%
  mutate(loc=paste(street,boro,state,zipcode,sep=",")) %>%
  unique() 

count_viola2 <- count_viola %>% left_join(Violations2,by=c("dba","loc")) %>% unique()
head(count_viola2)  
# A tibble: 6 × 7
# Groups:   dba [6]
  dba                                     loc   count street boro  state zipcode
  <chr>                                   <chr> <int> <chr>  <chr> <chr>   <int>
1 ''W'' CAFE                              5TH …    25 5TH A… MANH… NYC     10018
2 (LEWIS DRUG STORE) LOCANDA VINI E OLII  GATE…    17 GATES… BROO… NYC     11238
3 (LIBRARY)  FOUR & TWENTY BLACKBIRDS     GRAN…     9 GRAND… BROO… NYC     11238
4 (PUBLIC FARE) 81st street and central … CENT…    21 CENTR… MANH… NYC     10019
5 @NINE                                   9 AV…    51 9 AVE… MANH… NYC     10036
6 / L'ECOLE                               BROA…    15 BROAD… MANH… NYC     10013
df<- Violations %>% 
  mutate(state = "NYC")  %>% 
  mutate(loc=paste(street,boro,state,zipcode,sep=",")) %>%
  group_by(loc) %>% 
  summarise(
    count= n()
  )  %>%
  filter(count > 400)%>%
  drop_na()

Violations_loc <-tibble(
  address = df$loc) %>%
  tidygeocoder::geocode(address, method = "osm")
Passing 246 addresses to the Nominatim single address geocoder
Query completed in: 309.7 seconds
head(Violations_loc)
# A tibble: 6 × 3
  address                         lat  long
  <chr>                         <dbl> <dbl>
1 1 AVENUE,MANHATTAN,NYC,10003   40.8 -74.0
2 1 AVENUE,MANHATTAN,NYC,10009   40.7 -74.0
3 1 AVENUE,MANHATTAN,NYC,10021   40.8 -74.0
4 1 AVENUE,MANHATTAN,NYC,10065   40.8 -74.0
5 10 AVENUE,MANHATTAN,NYC,10019  40.7 -74.0
6 10 AVENUE,MANHATTAN,NYC,10036  40.7 -74.0

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.

violation_geo <- Violations_loc %>% 
  mutate(count = (df$count)) %>% 
  drop_na()  %>% 
  st_as_sf(coords=c("long","lat")) %>% 
  st_set_crs(4326) 

head(violation_geo)
Simple feature collection with 6 features and 2 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: -73.99632 ymin: 40.72883 xmax: -73.95407 ymax: 40.77045
Geodetic CRS:  WGS 84
# A tibble: 6 × 3
  address                       count             geometry
  <chr>                         <int>          <POINT [°]>
1 1 AVENUE,MANHATTAN,NYC,10003   1118 (-73.95407 40.77045)
2 1 AVENUE,MANHATTAN,NYC,10009    768  (-73.9844 40.72883)
3 1 AVENUE,MANHATTAN,NYC,10021    597 (-73.95574 40.76814)
4 1 AVENUE,MANHATTAN,NYC,10065    855 (-73.95574 40.76814)
5 10 AVENUE,MANHATTAN,NYC,10019   534 (-73.99632 40.73247)
6 10 AVENUE,MANHATTAN,NYC,10036   454 (-73.99632 40.73247)
ggplot(violation_geo) + 
  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.

violation_map <- leaflet() %>% 
  addTiles() %>%
  addMarkers(data = violation_geo)
violation_map

Problem 18.2

a.

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

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 = "lightgreen", size = 1) +
  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.

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)