---
title: "Leaflet Day 1: COVID Class Activity"
author: "David Palmer"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
  html_document:  
    keep_md: true
    toc: true
    toc_float: true
    code_folding: hide
    fig_height: 6
    fig_width: 12
    fig_align: 'center'
---

```{r, echo=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE, error = TRUE)
```

```{r load_libraries, include=FALSE}
# Use this R-Chunk to load all your libraries!
#install.packages("tidyverse") # run this line once in console to get package
library(USAboundaries)
library(tidyverse)
library(leaflet)

```

Practice Using Leaflet for Interactive Maps 

- We will use leaflet to create interactive maps of the cumulative confirmed cases of COVID-19.
- First, we will plot markers for the counties with the most confirmed cases.

Note, rather than doing copying and pasting, you may learn more by actually typing things out. This will force your mind to actually read over and digest the syntax.

First, let's read in the COVID data using the code below. Replace the stars with the last date in the dataset or a date of your choice using the format m/d/y. (For example, March 9th, 2023 would be 3/9/23). This will give us a column called "confirmed" that contains the cumulative confirmed cases up to that date. 

```{r load_data}
# Use this R-Chunk to import all your datasets!
covid <- read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_US.csv") %>% 
  
  rename(confirmed = `*/*/*`) #replace the stars with the most recent date in the dataset (i.e. the last column)
```

Take a minute to look at the data and understand it's structure.

## Plotting the Top Cities

We will plot the counties that had the most COVID cases (cumulative). You get to decide how many counties to plot. (I'd recommend somewhere between 20 and 40). The code in the next chunk selects just those counties and stores it in the a new dataset.

Fill in the blank in the code below so that the code will run.

```{r tidy_data}
# Use this R-Chunk to clean & wrangle your data!
covid_top<- covid %>% 
  slice_max(_____) %>% #use ?slice_max() to learn what needs to go here
  select(confirmed, everything())#This line reorders the columns so that `confirmed` is first, followed by everything else
```

Now create a plot with a marker placed in the location of each of the top counties
Note the use of the ~ in order to reference a column name.

```{r}
leaflet(data=covid_top) %>% addTiles() %>% 
  addMarkers(lng = ~Long_, lat = ~Lat)
```

Alternatively, you can change the underlying tiles of the base map to something provided by a third-party. Type the first three letters of the desired tiles in the blank in the code chunk below to use the auto-fill feature to find an option you like (they don't all work because some require registration with the third-party). You can preview the tile maps and their names: http://leaflet-extras.github.io/leaflet-providers/preview/index.html. 

Take a couple of minutes to play with different third-party tiles and find one that you like or that is unique. 

```{r}
leaflet(data=covid_top) %>% addProviderTiles(providers$______) %>% 
  addMarkers(lng = ~Long_, lat = ~Lat)
```


Consider the next chunk of code where we set some defaults for the map. What is this setView() doing and what values should you put in the blanks?

```{r}
leaflet(data=covid_top) %>% addTiles() %>% 
  setView(lng = ___, lat = ___, zoom = ___) %>% 
  addMarkers(lng = ~Long_, lat = ~Lat)
```

Let's add labels to the markers so we know the name of the county and the cumulative # of confirmed cases. In addition to blanks, this code has an error in it. Can you find and correct it? (Hopefully the error message provides a hint).

```{r plot_data}
# Use this R-Chunk to plot & visualize your data!
leaflet(data=covid_top) %>% addTiles() %>% 
  #setView(lng = ___, lat = ___, zoom = ___) %>%  
  addMarkers(lng = ~Long_, lat = ~Lat, label = ~paste(Admin2, "=", confirmed, cases))

```

## Circles on the map

Instead of markers we would like to use circles on the map. Let's encode the number of cases into the size of the circles. (If the circles are too large, try dividing the number of cases by 10).

```{r}
leaflet(data=covid_top) %>% addTiles() %>% 
  setView(lng = -99, lat = 40, zoom = 4) %>% 
  ____(lng = ~Long_, lat = ~Lat, 
        _____ = ______,
        label = __________________)
```


In addition to circle size, we will encode the cumulative confirmed case count into the color of the circle. This is not as simple and straight forward as one would like. It is explained here: https://rstudio.github.io/leaflet/colors.html

First the set-up
```{r}
pal<-colorNumeric(palette = c("yellow", "red"), domain = covid_top$confirmed/10)

pal #this is to see what pal contains/is
```

Take the time to answer the following questions about the above line of code:

- What is the domain argument providing? What is it used for?
- Why are we dividing the confirmed cases by 10? (try the plot without dividing it to see)
- What is pal?

Now to actually create the plot. **Can you find the 2 errors?**

```{r}
leaflet(data=covid_top) %>% addTiles() %>% 
  setView(lng = -99, lat = 40, zoom = 4) %>% 
  addCircles(lng = ~Long_, lat = ~Lat, 
             radius = ~confirmed/10,
             label = ~paste0(Admin2," County = ", confirmed , " confirmed cases"), 
             fillOpacity = .1,
             color = ~pal(confirmed/10),  #color is the border color
             fillColor =  pal(confirmed)) #notice the domain above must match exactly what is fed to pal
```


## Big Data and Spatial Data

Pause to read the following article: Spatial Data Often Becomes Big Data
https://blogs.esri.com/esri/arcgis/2017/10/17/strategies-to-effectively-display-large-amounts-of-data-in-web-apps/

Identify the 4 stratagies for displaying map data. Consider recording them in your Readme.md

1. 
2. 
3. 
4. 

Time permitting, read through the [Leaflet guide](https://rstudio.github.io/leaflet/) and find 1-2 additional map features you would like to play with.