Introduction

Since 1872, the United States Congress has set aside 63 protected areas to be designated as “National Parks”. I have been fortunate enough to have been visiting these parks since childhood, and I am currently on a life-long mission to visit all current and future National Parks.

For this final R Markdown homework assignment, I chose to use Leaflet to map all the National Parks to show whether I have visited them or not, as well as use Plotly to show the percentage of parks I have visited as of May 2022.

Data Preparation

I chose to source the National Park coordinates through Wikipedia, where the coordinates were available in a .kml file. I pulled in the .kml file with st_read() then saved it as an Excel file with write_xlsx for easier use (KML had spatial settings that I didn’t want to work with)

library(sf) library(writexl)

coord_kml <- st_read("doc.kml")
## Reading layer `National parks' from data source 
##   `C:\Users\Garrett\Documents\R\HW5\doc.kml' using driver `KML'
## Simple feature collection with 63 features and 2 fields
## Geometry type: POINT
## Dimension:     XYZ
## Bounding box:  xmin: -170.68 ymin: -14.25 xmax: -64.73 ymax: 67.78
## z_range:       zmin: 0 zmax: 0
## Geodetic CRS:  WGS 84
write_xlsx(coord_kml,"C:\\Users\\Garrett\\Documents\\R\\HW5\\NP_coords.xlsx")

Additionally, I had a personal list in Google Sheets of National Parks and whether I had visited them or not. I converted this sheet to Excel and ensured that the park names were matching.

Files were then read in to R as coords and Visit_List.

library(readxl)

coords <- read_excel("NPCoords.xlsx")
head(coords)
## # A tibble: 6 x 2
##   Name                            geometry        
##   <chr>                           <chr>           
## 1 Acadia                          -68.21,44.35,0  
## 2 National Park of American Samoa -170.68,-14.25,0
## 3 Arches                          -109.57,38.68,0 
## 4 Badlands                        -102.5,43.75,0  
## 5 Big Bend                        -103.25,29.25,0 
## 6 Biscayne                        -80.08,25.65,0
Visit_List <- read_excel("NP_List.xlsx")
head(Visit_List)
## # A tibble: 6 x 2
##   Name                            Visited
##   <chr>                           <chr>  
## 1 Acadia                          No     
## 2 National Park of American Samoa No     
## 3 Arches                          Yes    
## 4 Badlands                        No     
## 5 Big Bend                        No     
## 6 Biscayne                        No

Since the coordinates in coords$geography were in one column, I needed to separate them. I split them into their own columns as strings using str_split_fixed(), then reverted Longitude and Latitude back into numeric form with as.numeric()

library(tidyverse)

coords[c('Long', 'Lat', "delete")] <- str_split_fixed(coords$geometry, ',', 3)
coords$Long <- as.numeric(coords$Long)
coords$Lat <- as.numeric(coords$Lat)
head(coords)
## # A tibble: 6 x 5
##   Name                            geometry           Long   Lat delete
##   <chr>                           <chr>             <dbl> <dbl> <chr> 
## 1 Acadia                          -68.21,44.35,0    -68.2  44.4 0     
## 2 National Park of American Samoa -170.68,-14.25,0 -171.  -14.2 0     
## 3 Arches                          -109.57,38.68,0  -110.   38.7 0     
## 4 Badlands                        -102.5,43.75,0   -102.   43.8 0     
## 5 Big Bend                        -103.25,29.25,0  -103.   29.2 0     
## 6 Biscayne                        -80.08,25.65,0    -80.1  25.6 0

Then, I felt it was best to get rid of the columns I no longer needed!

coords <- coords[c("Name", "Lat", "Long")]
head(coords)
## # A tibble: 6 x 3
##   Name                              Lat   Long
##   <chr>                           <dbl>  <dbl>
## 1 Acadia                           44.4  -68.2
## 2 National Park of American Samoa -14.2 -171. 
## 3 Arches                           38.7 -110. 
## 4 Badlands                         43.8 -102. 
## 5 Big Bend                         29.2 -103. 
## 6 Biscayne                         25.6  -80.1

The last step of prepping was to merge coords and visit_list into the new NP_List using left_join()

NP_List <- left_join(coords, Visit_List, by="Name")
head(NP_List)
## # A tibble: 6 x 4
##   Name                              Lat   Long Visited
##   <chr>                           <dbl>  <dbl> <chr>  
## 1 Acadia                           44.4  -68.2 No     
## 2 National Park of American Samoa -14.2 -171.  No     
## 3 Arches                           38.7 -110.  Yes    
## 4 Badlands                         43.8 -102.  No     
## 5 Big Bend                         29.2 -103.  No     
## 6 Biscayne                         25.6  -80.1 No

Building the map with Leaflet

Knowing that I wanted to plot these coordinates on the map using two colors, I consulted the this website to choose my colors to fit in with the National Park Service theme.

This code will assist me when I plot that circle markers in the map later

Color_Legend <- colorFactor(c("#99542C", "#2D4B1E"), domain = c("Yes", "No"))

Next, it was time to build the map. Calling leaflet, I passed the NP_List dataframe into it, added circle markers to the map, and then added the legend.

addCircleMarkers required longitude, latitude, popup info (name of the park, and whether I visited or not), and then its colors and size.

addLegend utilized the same colors to match with the overall theme.

library(leaflet)

NP_map <- leaflet(NP_List) %>%
  addTiles() %>%
  addCircleMarkers(~Long, ~Lat, popup=paste(NP_List$Name,"<br>", "Visited: ", NP_List$Visited), weight=5, radius = 5, color = ~Color_Legend(Visited)) %>%
  addLegend("bottomright", colors = c("#99542C", "#2D4B1E"), labels = c("No", "Yes"),
            title = "National Parks Visited")

NP_map

Pie Chart (because why not?)

This homework assignment called for either adding markers to a map with Leaflet, or to create a plot with Plotly. I wanted to do both, so I thought I would also learn how to make a pie chart to show how many of the parks I have been to so far

The first step was to create a dataframe with the count of Yes and No from the NP_List$Visit column

Visit_Count <- as.data.frame(table(NP_List['Visited']))
Visit_Count
##   Var1 Freq
## 1   No   44
## 2  Yes   19

With those numbers, its pretty straight forward to create the pie chart. Plot_ly() requires the dataframe, values, labels, and type. Since I wanted to keep the same colors, I also added those in.

library(plotly)

pie_chart <- plot_ly(Visit_Count, values = ~Freq, labels=~factor(Var1), marker=list(colors = c("#99542C", "#2D4B1E")), type="pie") %>%
  layout(title = "Percentage of US National Parks Visited")
pie_chart

#Conclusion

Creating map markers with leaflet and graphs with plotly in R is pretty quick! When I made the first map marker, I thought I was going to have to build a function or run iterative loops for all 63 points, but luckily Leaflet was able to take it all in without issue.

I hope you enjoyed the read!

- Garrett