library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
library(gplots)
## 
## Attaching package: 'gplots'
## 
## The following object is masked from 'package:stats':
## 
##     lowess
library(maps)
## 
## Attaching package: 'maps'
## 
## The following object is masked from 'package:purrr':
## 
##     map
library(ggmap) 
## The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
## which was just loaded, will retire in October 2023.
## Please refer to R-spatial evolution reports for details, especially
## https://r-spatial.org/r/2023/05/15/evolution4.html.
## It may be desirable to make the sf package available;
## package maintainers should consider adding sf to Suggests:.
## The sp package is now running under evolution status 2
##      (status 2 uses the sf package in place of rgdal)
## ℹ Google's Terms of Service: <https://mapsplatform.google.com>
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
library(RColorBrewer) 
library(leaflet)
library(ggthemes) 
library(usdata)
library(htmltools)
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
geojson_path <- "Desktop/washsh_area.geojson"
county_data <- st_read(geojson_path)
## Reading layer `Washington_Counties_with_Natural_Shoreline___washsh_area' from data source `/Users/baileylengfelder/Desktop/washsh_area.geojson' using driver `GeoJSON'
## Simple feature collection with 39 features and 7 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -124.7331 ymin: 45.54373 xmax: -116.9152 ymax: 49.00253
## Geodetic CRS:  WGS 84
csv_path <- "Desktop/WACounties.csv"

# Load the selected CSV file
data <- read.csv(csv_path)
countywa <- filter(county,
                   state=='Washington')

countywa$name<- sub(" County$","",countywa$name)

colnames(countywa)[colnames(countywa) == "name"] <- "COUNTYLABEL"
county_data1 <- county_data %>%
  semi_join(data, by = c("COUNTYLABEL" = "COUNTYLABEL"))

county_data2 <- county_data1 %>%
  full_join(countywa, by = c("COUNTYLABEL" = "COUNTYLABEL"))
ggplot() +
  geom_sf(data = county_data2, aes(fill = unemployment_rate)) +
  scale_fill_viridis_c() +  
  labs(title = "Washington State Unemployment by County",
       fill = "Unemployment Rate") +
  theme_minimal() 
A chloropleth map of Washington state showing the unemployment rate for each county. The map shows that Ferry county has the highest unemployment rate at around 11%, while all the others counties fall between 4 and 8%.

Created by Bailey Lengfelder

map <- leaflet() %>%
  addTiles()  

colors <- colorNumeric(palette = "PuRd", domain = county_data2$unemployment_rate) 

map <- map %>%
  addPolygons(
    data = county_data2,
    fillColor = ~colors(unemployment_rate), 
    fillOpacity = 0.7,
    color = "white",
    weight = 1,
    highlight = highlightOptions(
      weight = 3,
      color = "black",
      bringToFront = TRUE
    ),
     popup = ~paste("<strong>County: </strong>", COUNTYLABEL, "<br><strong>Unemployment Rate:</strong> ", unemployment_rate, "%"),
    label = ~paste("Unemployment Rate: ", unemployment_rate, "%"))

      
  
map <- map %>%
  addLegend(
    pal = colors,
    values = county_data2$unemployment_rate,
    position = "bottomright",
    title = "Unemployment Rate (%)",
    labFormat = labelFormat()
  )


map

Created by Bailey Lengfelder

library(leaflet)
library(RColorBrewer)
library(sf)

# Assuming you have your county_data2 and other required data loaded as sf objects

# Create a leaflet map
map <- leaflet() %>%
  addTiles(
    options = tileOptions(
      attribution = ''  # You can adjust this if your tile provider allows
    )
  )

# Define colors based on the unemployment rate
colors <- colorNumeric(palette = "PuRd", domain = county_data2$unemployment_rate) 

# Add polygons to the map
map <- map %>%
  addPolygons(
    data = county_data2,
    fillColor = ~colors(unemployment_rate), 
    fillOpacity = 0.7,
    color = "white",
    weight = 1,
    highlight = highlightOptions(
      weight = 3,
      color = "black",
      bringToFront = TRUE
    )
  )

# Calculate centroids of multipolygons
centroids <- st_centroid(county_data2)
## Warning: st_centroid assumes attributes are constant over geometries
# Extract coordinates and labels
label_data <- data.frame(
  lng = st_coordinates(centroids)[, 1],
  lat = st_coordinates(centroids)[, 2],
  label = county_data2$COUNTYLABEL
)

# Add labels to the map
map <- map %>%
  addLabelOnlyMarkers(
    data = label_data,
    lng = ~lng,
    lat = ~lat,
    label = ~label,
    labelOptions = labelOptions(
  noHide = TRUE,
  textOnly = TRUE,
  style = list(
    "font-weight" = "normal",
    "font-style" = "italic",
    "color" = "black",
    "background-color" = "white",
    "border" = "1px solid darkgray",
    "padding" = "2px",
    "border-radius" = "1px"
  ),
  textsize = "8px"
))

map <- map %>%
  addPolygons(
    data = county_data2,
    fillColor = ~colors(unemployment_rate), 
    fillOpacity = 0.7,
    color = "white",
    weight = 1,
    highlight = highlightOptions(
      weight = 3,
      color = "black",
      bringToFront = TRUE
    ),
     popup = ~paste("<strong>County: </strong>", COUNTYLABEL, "<br><strong>Unemployment Rate:</strong> ", unemployment_rate, "%"),
    label = ~paste("Unemployment Rate: ", unemployment_rate, "%"))


map <- map %>%
  addControl(
    html = '<div style="text-align: center; font-size: 18px; font-weight: bold;">Washington State Unemployment by County</div>',
    position = "topright"
  )

# Add a legend to the map
map <- map %>%
  addLegend(
    pal = colors,
    values = county_data2$unemployment_rate,
    position = "bottomright",
    title = "Unemployment Rate (%)",
    labFormat = labelFormat()
  )



map <- map %>%
  addControl(
    html = '<div style="text-align: right; font-size: 10px; color: gray; padding: 0.00001px;">Data Source: gisdata.kingcounty.gov</div>',
    position = "bottomleft"
  )
# Display the map
map