library(leaflet)
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(geojsonio)
## The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
## which was just loaded, were retired 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:.
## Registered S3 method overwritten by 'geojsonsf':
## method from
## print.geojson geojson
##
## Attaching package: 'geojsonio'
## The following object is masked from 'package:base':
##
## pretty
library(csv)
library(ggplot2)
library(rnaturalearth)
## Support for Spatial objects (`sp`) will be deprecated in {rnaturalearth} and will be removed in a future release of the package. Please use `sf` objects with {rnaturalearth}. For example: `ne_download(returnclass = 'sf')`
library(scales)
library(stringr)
# Read GeoJSON file for states
states <- st_read("States_shapefile.geojson")
## Reading layer `States_shapefile' from data source
## `/Users/kylejilla/Desktop/MIDTERM MATERIALS/States_shapefile.geojson'
## using driver `GeoJSON'
## Simple feature collection with 51 features and 6 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -178.2176 ymin: 18.92179 xmax: -66.96927 ymax: 71.40624
## Geodetic CRS: WGS 84
# Read the unemployment data
unemployment_data <- read.csv("Unemployment in America Per US State.csv")
# unemployment_data %>%
# mutate(State_Name= str_to_upper(`State.Area`))
unemployment_data$State.Area <- toupper(unemployment_data$State.Area)
My_data <- states %>%
full_join(unemployment_data, by = c("State_Name" = "State.Area")) %>% filter(State_Name != "ALASKA" & State_Name != "HAWAII")
# Define breaks for the choropleth map
breaks <- c(0, 3, 5, 7, 10) # Adjust these breaks according to your dataset
# Perform a manual breakdown of the variable of interest into bins of unequal width
#merged_data$unemployment_rate_bins <- cut(merged_data$Unemployment.Rate, breaks, labels = FALSE)
# Create a custom color palette
color_palette <- c("#f7f7f7", "#cccccc", "#888888", "#404040")
# Create the choropleth map using ggplot2
ggplot() +
geom_sf(data = My_data, aes(fill = `Percent.....of.Labor.Force.Unemployed.in.State.Area`), color = "white", size = 0.5) +
scale_fill_gradientn(colours = c("darkblue", "lightcyan2", "orange", "darkorange"), limits = c(0, 10)) +
labs(
fill = "Unemployment Percentage",
color = "",
title = "Unemployment Rate in each US State",
subtitle = "*This graph is visualizing the percent of population that is employed in each state within the United States*",
caption = "Author: Kyle Jilla\nYear: 2023"
) +
ggthemes::theme_map() +
theme(
legend.position = "bottom",
plot.caption = element_text(size = 12, margin = margin(t = 10)),
plot.title = element_text(hjust = 0.5) # Center the title
)

# scale_fill_manual(values = color_palette, guide = guide_legend(title = "Unemployment Rate")) +
# scale_color_manual(values = NA) +
# theme_minimal() +
# theme(
# legend.position = "bottom",
# legend.direction = "horizontal",
# legend.key.width = unit(3, "cm"),
# legend.key.height = unit(0.5, "cm"),
# legend.title = element_text(size = 10),
# legend.text = element_text(size = 8),
# plot.title = element_text(size = 14, hjust = 0.5)
# ) +
# labs(title = "Unemployment Rates in California by County") +
# guides(fill = guide_legend(override.aes = list(color = NA)))
# Create an interactive leaflet map with popups
#leaflet_map <- leaflet(merged_data) %>%
#addTiles() %>%
#addPolygons(
#fillColor = ~colorBin("YlOrRd", domain = merged_data$Unemployment.Rate, bins = breaks),
#fillOpacity = 0.7,
#color = "white",
#weight = 1,
#popup = ~paste( "County: ", merged_data$County_Name, "<br>", "Unemployment Rate: ", percent(merged_data$Unemployment.Rate)))
# Display the leaflet map
#leaflet_map