Squirrels in NYC

Introduction

Central Park in New York City is not just a green oasis in the middle of an urban jungle but also home to a variety of wildlife, including squirrels. In this analysis, we will visualize data from the 2018 Central Park Squirrel Census using R. We will employ the leaflet library for interactive mapping and the sf and spData libraries for handling spatial data.

Setup

First, we need to set up our R environment. We will configure the chunk options to display the code and output.

We will use the following libraries for our analysis:

  • leaflet: For creating interactive maps.
  • sf: For handling spatial data.
  • spData: For accessing spatial datasets
## Warning: package 'leaflet' was built under R version 4.4.1
## Warning: package 'sf' was built under R version 4.4.1
## Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
## Warning: package 'spData' was built under R version 4.4.1
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`

Load the Data

The data for the squirrel census is stored in a CSV file. We will load this data into R using the read.csv function. Make sure to update the file path to the location where your CSV file is stored.

Plotting the Data

Basic Interactive Map

We will start by creating a basic interactive map using the leaflet package. This map will display the locations of squirrels in Central Park using circle markers. The markers will be labeled with the age of the squirrels.

leaflet(squirrels) %>%
  addTiles()%>%
  addCircleMarkers(~X, ~Y, label = ~Age)

Adding a Color Palette

To enhance the visualization, we will add colors to the markers based on the primary fur color of the squirrels. We create a color palette using the colorFactor function. This function allows us to map the fur color to a color palette.

pal <- colorFactor(
  palette = 'Dark2',
  domain = squirrels$Primary.Fur.Color,
  levels = NULL,
  ordered = FALSE,
  na.color = "#808080",
  alpha = FALSE,
  reverse = FALSE
)

Enhanced Interactive Map

Now, we will use the color palette to color the markers based on the primary fur color of the squirrels. This will make it easier to distinguish between squirrels of different fur colors.

leaflet(squirrels) %>%
  addTiles()%>%
  addCircleMarkers(~X, ~Y, label = ~Age, color = ~pal(Primary.Fur.Color))

Conclusion

In this analysis, we visualized the 2018 Central Park Squirrel Census data using R. We created an interactive map with leaflet, displaying the locations of squirrels in Central Park. By adding a color palette, we further enhanced the map to show the primary fur colors of the squirrels. This approach allows us to better understand the distribution and characteristics of the squirrel population in Central Park.