browseURL("https://public.tableau.com/authoring/Hw3_14_6/Dashboard1#2")
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)
Dickinson <- tibble(
address = "Emily Dickinson Museum in Amherst, Massachusetts"
) %>%
tidygeocoder::geocode(address, method = "osm")
Dickinson
The Violations data frame in the mdsr package contains information on violations noted in Board of Health inspections of New York City restaurants. These data contain spatial information in the form of addresses and zip codes.
Violations1<-Violations %>% drop_na(violation_code) ## choose the data with real violations
dim(Violations1)
## [1] 471203 16
Use the geocode function in tidygeocoder to obtain spatial coordinates for these restaurants.
## 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.
head(count_viola)
hist(count_viola$count)
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)
dim(count_viola2)
## [1] 24430 7
## we choose the restaurants with more than 80 violations, my computer does not work when try to get the geocode for all the restaurants.
tic()
vio_loc<- count_viola2 %>%
filter(count>80) %>%
tidygeocoder::geocode(loc,method = "osm") %>%
drop_na() %>%
st_as_sf(coords = c("long", "lat")) %>%
st_set_crs(4326)
## Passing 25 addresses to the Nominatim single address geocoder
## Query completed in: 41 seconds
head(vio_loc)
toc()
## 41.128 sec elapsed
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.
ggplot(vio_loc) +
geom_sf()
library(ggspatial)
ggplot(vio_loc) +
annotation_map_tile(type = "osm", zoomin = 0) +
geom_sf(aes(size = count), alpha = 0.7)
## Loading required namespace: raster
## Zoom: 11
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.
library(leaflet)
vio_loc_map <- leaflet() %>%
addTiles() %>%
addMarkers(data = vio_loc)
## pop up the top 5 restaurants which had the most violations.
vio_loc_map %>%
addPopups(
data = filter(vio_loc, count>=93),
popup = ~paste0("<b>", count, "</b></br>", dba,"</b></br>", loc)
)
library( macleish)
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")
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 = subset(macleish_layers[["landmarks"]],
grepl("Met", Label)), popup = ~Label)