Part B

Introduction

In this report we will be exploring a dataset that contains information on Bigfoot sightings within the United States. This dataset will be a foundation to which we will create an interactive map using the leaflet package.

This report has been uploaded to RPubs and can be accessed here.

The data for the Bigfoot sightings was obtained here

Getting Started

To begin we will ensure all the necessary packages are loaded.

  library(leaflet)
  library(sf)
  library(tidyverse)
  library(leafpop)

Prepping the Data

# we begin by loading the bigfoot data set into a data frame called all_bigfoot

    all_bigfoot <- read_csv('bfro_reports_geocoded.csv') |>
  
# we filter out any rows with missing or physically impossible coordinates
    
      filter(!is.na(latitude),
             between(longitude, -180, 180),
             between(latitude, -90, 90)) |>
  
# after filtering, we use st_as_sf to convert the standard data frame to a spatial data frame. We let R know that the coordinates are in the "longitude" and "latitude" columns and we will be using coordinate system WGS 84 which is identified via the code 4326
  
      st_as_sf(coords = c("longitude", "latitude"), crs = 4326) |>
  
# we select the season, state, county, date, and observed columns
    
      select(season, state, county, date, observed)

# we create a palette using the colorFactor to to map data categories to specific colors
    
    pal <- colorFactor(c("darkgreen", "navy", "pink", "orange"), domain = c("Fall", "Spring", "Winter", "Summer"))

Now that our data has been prepped, we can use the leaflet package to create an interactive map with the data.

# use the leaflet package to create an interactive map based off the all_bigfoot data frame

    leaflet(data = all_bigfoot) |>
  
# we use addTiles() to utilize OpenStreetMap tiles  
      
      addTiles() |>
  
# creating cricle markers for each point. the color for each marker will be drawn from the palette we created earlier
  
# we also create a popup for each marker and a label  
    
      addCircleMarkers(color = ~pal(season), fillOpacity = .5, stroke = FALSE, popup = leafpop::popupTable(all_bigfoot), label = ~season) |>
  
# we create a legend for the interactive map. the legend is positioned on the bottom right, obtains it palette information from the palette we created earlier, the legend is based off values in the season column, and we give it a title called "Season"  
       
    addLegend(position = "bottomright",
                pal = pal,
                values = ~season,
                title = "Season")

```