Instructions

Answer the following questions and complete the exercises in RMarkdown. Please embed all of your code and push your final work to your repository. Your final lab report should be organized, clean, and run free from errors. Remember, you must remove the # for the included code chunks to run. Be sure to add your name to the author header above. For any included plots, make sure they are clearly labeled. You are free to use any plot type that you feel best communicates the results of your analysis.

Make sure to use the formatting conventions of RMarkdown to make your report neat and clean!

Load the libraries

library(tidyverse)
library(janitor)
library(ggmap)
library(leaflet)
register_stadiamaps("777", write = FALSE)

Load the Data

For this homework, we will use the shark attack data to visualize where the attacks occurred. The data are from: State of California- Shark Incident Database.

sharks <- read_csv("data/SharkIncidents_1950_2022_220302.csv") %>% 
  clean_names() %>% 
  filter(longitude !="NA" & latitude !="NA") %>% # pulling out NA locations
  mutate(longitude = as.numeric(longitude)) # converting longitude to numeric
  1. Use the range of the latitude and longitude to build an appropriate bounding box for your map. But, first run the following chunk to get rid of duplicate locations. This will make the map look cleaner.
sharks_dups <- sharks %>% 
  distinct(location, .keep_all = TRUE) # remove duplicate locations, but keep the remaining variables
sharks_dups %>% 
  select(longitude, latitude) %>% 
  summary()
##    longitude         latitude    
##  Min.   :-124.7   Min.   :32.59  
##  1st Qu.:-122.6   1st Qu.:34.04  
##  Median :-121.8   Median :36.55  
##  Mean   :-121.1   Mean   :36.13  
##  3rd Qu.:-119.5   3rd Qu.:37.63  
##  Max.   :-117.1   Max.   :41.56
lat <- c(32.59, 41.56)
long <- c(-117.1, -124.7)
bbox2 <- make_bbox(long, lat, f=0.3)
  1. Load a map from stamen in a terrain style projection and display the map.
map2 <- get_stadiamap(bbox2, maptype = "stamen_terrain", zoom=7)
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
## ℹ 42 tiles needed, this may take a while (try a smaller zoom?)
ggmap(map2)

  1. Build a map that overlays the recorded observations of shark attacks in California.
ggmap(map2)+
  geom_point(data=sharks_dups, aes(x=longitude, y=latitude), size=0.8)+
  labs(x="Longitude", y="Latitude", title="Shark Attacks Coastal California 1950-2022")

  1. What if you only wanted to map the fatalities?
sharks_fatal <- sharks_dups %>% filter(injury == "fatal")
sharks_fatal %>% 
  select(longitude, latitude) %>% 
  summary()
##    longitude         latitude    
##  Min.   :-123.8   Min.   :32.85  
##  1st Qu.:-122.1   1st Qu.:34.03  
##  Median :-121.0   Median :35.40  
##  Mean   :-120.8   Mean   :35.62  
##  3rd Qu.:-120.2   3rd Qu.:36.70  
##  Max.   :-117.3   Max.   :39.58
lat3 <- c(32.85, 39.58)
long3 <- c(-117.3, -123.8)
bbox3 <- make_bbox(long3, lat3, f=0.3)
map3 <- get_stadiamap(bbox3, maptype = "stamen_terrain", zoom=7)
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
ggmap(map3)+
  geom_point(data=sharks_fatal, aes(x=longitude, y=latitude), size=0.8)+
  labs(x="Longitude", y="Latitude", title="Fatal Shark Attacks Coastal California 1950-2022")

  1. Build the same map, but this time make it interactive using leaflet. Also, color the points red.
sharks_fatal %>%
  summarize(
    lon_min = min(longitude, na.rm = TRUE),
    lon_max = max(longitude, na.rm = TRUE),
    lat_min = min(latitude,  na.rm = TRUE),
    lat_max = max(latitude,  na.rm = TRUE))
## # A tibble: 1 × 4
##   lon_min lon_max lat_min lat_max
##     <dbl>   <dbl>   <dbl>   <dbl>
## 1   -124.   -117.    32.9    39.6
lon_min <- -123.7783        
lon_max <- -117.28  
lat_min <- 32.855       
lat_max <- 39.57833 
leaflet(sharks_fatal) %>%
  addProviderTiles(providers$Stadia.StamenTerrain) %>%
  addCircleMarkers(
    lng = ~longitude, #use the longitude column for x-coordinates
    lat = ~latitude, #use the latitude column for y-coordinates
    radius = 2, #set marker size (in pixels)
    stroke = FALSE, #remove the outline around each marker
    fillColor="red", #set the fill color of the markers
    fillOpacity = 1 #set the fill opacity of the markers
  ) %>%
  addScaleBar(position = "bottomleft") %>% # add a scale bar to the map
  fitBounds(lng1 = lon_min, lat1 = lat_min, #southwest corner of bounding box
            lng2 = lon_max, lat2 = lat_max) #northeast corner of bounding box
  1. Which county has the highest number of recorded shark attacks? Make a map that only shows the attacks in this county.
sharks %>% 
  group_by(county) %>%
  summarise(count=n()) %>% 
  arrange(desc(count))
## # A tibble: 21 × 2
##    county          count
##    <chr>           <int>
##  1 San Diego          24
##  2 San Mateo          19
##  3 Santa Barbara      19
##  4 Humboldt           18
##  5 Marin              16
##  6 Monterey           16
##  7 Santa Cruz         15
##  8 Sonoma             15
##  9 San Luis Obispo    14
## 10 Los Angeles         9
## # ℹ 11 more rows
sd_sharks <- sharks %>% 
  filter(county=="San Diego" & incident_num !="NOT COUNTED") %>% 
  distinct(location, .keep_all = TRUE)
sd_sharks %>% 
  select(latitude, longitude) %>% 
  summary()
##     latitude       longitude     
##  Min.   :32.59   Min.   :-118.1  
##  1st Qu.:32.78   1st Qu.:-117.4  
##  Median :32.85   Median :-117.3  
##  Mean   :32.95   Mean   :-117.4  
##  3rd Qu.:33.10   3rd Qu.:-117.3  
##  Max.   :33.37   Max.   :-117.1
lat <- c(32.59, 33.37 )
long <- c(-118.1, -117.1 )
bbox <- make_bbox(long, lat, f = 0.05)
san_diego <- get_stadiamap(bbox, maptype = "stamen_terrain", zoom=10)
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
ggmap(san_diego)

ggmap(san_diego) + 
  geom_point(data = sd_sharks, 
             aes(longitude, latitude), 
             size = 2, 
             color = "black", 
             alpha = 0.8) +
  labs(x = "Longitude", 
       y = "Latitude", 
       title = "Shark Attacks, San Diego County")

Knit and Upload

Please knit your work as an .html file and upload to Canvas. Homework is due before the start of the next lab. No late work is accepted. Make sure to use the formatting conventions of RMarkdown to make your report neat and clean!