Create a proportional symbol map and a graduated symbol map for the same dataset and discuss the differences between the maps. Design an appropriate legend for both maps.

Step 1: Install necessary libraries

# Load necessary libraries
library(sf)
library(leaflet)
library(dplyr)
library(RColorBrewer)
library(htmltools)
library(htmlwidgets)
library(tigris)

Step 2: Load and Prepare the Data

# Load the dataset
data <- read.csv("/Users/ogeohia/Downloads/CleanedUnemploymentData.csv")

# Cleaning data: Rename columns and ensure the unemployment rate is numeric
data_clean <- data %>%
  mutate(Unemployment_rate = as.numeric(Unemployment_rate))

# View cleaned data
head(data_clean)
##           State Unemployment_rate
## 1  South Dakota               1.9
## 2       Vermont               2.4
## 3  North Dakota               2.5
## 4 New Hampshire               2.6
## 5      Nebraska               2.8
## 6   Connecticut               3.0

Step 3: Download US shapefile data for state boundaries

# Download US states shapefile (boundaries)
states <- states(cb = TRUE, progress = FALSE)

# Convert to 'sf' (simple features) format for easier handling
states_sf <- st_as_sf(states)

# View the shapefile data
head(states_sf)
## Simple feature collection with 6 features and 9 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -179.1489 ymin: -14.5487 xmax: 179.7785 ymax: 71.36516
## Geodetic CRS:  NAD83
##   STATEFP  STATENS    AFFGEOID GEOID STUSPS           NAME LSAD        ALAND
## 1      56 01779807 0400000US56    56     WY        Wyoming   00 2.514587e+11
## 2      02 01785533 0400000US02    02     AK         Alaska   00 1.478943e+12
## 3      24 01714934 0400000US24    24     MD       Maryland   00 2.515199e+10
## 4      60 01802701 0400000US60    60     AS American Samoa   00 1.977591e+08
## 5      05 00068085 0400000US05    05     AR       Arkansas   00 1.346608e+11
## 6      38 01779797 0400000US38    38     ND   North Dakota   00 1.786943e+11
##         AWATER                       geometry
## 1   1867503716 MULTIPOLYGON (((-111.0546 4...
## 2 245378425142 MULTIPOLYGON (((179.4825 51...
## 3   6979074857 MULTIPOLYGON (((-76.05015 3...
## 4   1307243751 MULTIPOLYGON (((-168.1458 -...
## 5   3121950081 MULTIPOLYGON (((-94.61792 3...
## 6   4414779956 MULTIPOLYGON (((-104.0487 4...

Step 4: Merge the unemployment data with the shapefile

## Simple feature collection with 6 features and 10 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -179.1489 ymin: -14.5487 xmax: 179.7785 ymax: 71.36516
## Geodetic CRS:  NAD83
##   STATEFP  STATENS    AFFGEOID GEOID STUSPS           NAME LSAD        ALAND
## 1      56 01779807 0400000US56    56     WY        Wyoming   00 2.514587e+11
## 2      02 01785533 0400000US02    02     AK         Alaska   00 1.478943e+12
## 3      24 01714934 0400000US24    24     MD       Maryland   00 2.515199e+10
## 4      60 01802701 0400000US60    60     AS American Samoa   00 1.977591e+08
## 5      05 00068085 0400000US05    05     AR       Arkansas   00 1.346608e+11
## 6      38 01779797 0400000US38    38     ND   North Dakota   00 1.786943e+11
##         AWATER Unemployment_rate                       geometry
## 1   1867503716               3.5 MULTIPOLYGON (((-111.0546 4...
## 2 245378425142               4.7 MULTIPOLYGON (((179.4825 51...
## 3   6979074857               3.1 MULTIPOLYGON (((-76.05015 3...
## 4   1307243751                NA MULTIPOLYGON (((-168.1458 -...
## 5   3121950081               3.4 MULTIPOLYGON (((-94.61792 3...
## 6   4414779956               2.5 MULTIPOLYGON (((-104.0487 4...
## [1] 5
## [1] 0
## sfc_POINT of length 51; first list element:  'XY' num [1:2] -108 43

Step 5: Create a Proportional Symbol Map using leaflet

# Define 5 bins for the proportional symbol map
breaks_proportional <- c(1.9, 2.7, 3.4, 4.2, 4.9, 5.7)

# Define color palette with 5 bins
pal_proportional <- colorBin("YlGnBu", domain = merged_data_sf$Unemployment_rate, bins = breaks_proportional)

# Proportional symbol map using the defined palette
map_proportional <- leaflet(merged_data_sf) %>%
  setView(-98.5, 39.8, zoom = 4) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addCircles(
    lat = ~latitude,
    lng = ~longitude,
    radius = ~Unemployment_rate * 10000,
    weight = 1,
    color = "black",
    fillColor = ~pal_proportional(Unemployment_rate), 
    fillOpacity = 0.7,
    popup = ~paste("State: ", NAME, "<br>", "Unemployment Rate: ", round(Unemployment_rate, 2), "%")
  ) %>%
  addLegend(
    pal = pal_proportional, 
    values = ~Unemployment_rate,
    title = "Unemployment Rate (%)",
    position = "topright",
    opacity = 0.7
  )

# Display the map
map_proportional

Step 6: Create a Graduated Symbol Map

# Define the fewer breaks for unemployment rate categories (bins)
breaks_graduated <- c(0, 2, 4, 6)  # 3 categories: 0-2%, 2-4%, 4-6%

# Define the colors for the legend manually (corresponding to the 3 bins)
colors <- c("lightgreen", "lightblue", "lightcoral")  # Colors for Low, Medium, High

# Create a graduated symbol map using leaflet with larger symbols
map_graduated <- leaflet(merged_data_sf) %>%
  setView(-98.5, 39.8, zoom = 4) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addCircles(
    lat = ~latitude,
    lng = ~longitude,
    radius = ~{
      # Assign circle size based on the unemployment rate category (3 bins)
      category <- cut(Unemployment_rate, breaks_graduated, labels = FALSE)
      radius_map <- c(40000, 60000, 80000)
      return(radius_map[category])
    },
    weight = 1,
    color = "black",
    fillColor = ~colorBin(palette = colors, domain = merged_data_sf$Unemployment_rate, bins = breaks_graduated)(Unemployment_rate), 
    fillOpacity = 0.7,
    popup = ~paste("State: ", NAME, "<br>", "Unemployment Rate: ", round(Unemployment_rate, 2), "%")
  ) %>%
  addLegend(
    position = "topright",
    opacity = 0.7,
    # Manually set the labels for the legend
    labels = c("Low (0 - 2%)", "Medium (2 - 4%)", "High (4 - 6%)"),  # Custom labels
    colors = colors
  )

# Display the map
map_graduated