# Load necessary libraries
library(ggplot2)
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.3
## 
## 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(maps)
## Warning: package 'maps' was built under R version 4.3.3
# Load the dataset
data <- read.csv("C://Users//xavie//OneDrive//Desktop//ERM 411//WA_EV_Carsales_by_County.csv")
# Display the first few rows to see the data structure
head(data)
##   X subregion population frequency
## 1 1     Adams      20820        65
## 2 2    Asotin      22549        83
## 3 3    Benton     215219      2546
## 4 4    Chelan      79997      1238
## 5 5   Clallam      77616      1217
## 6 6     Clark     521150     12231
# Load map data for Washington state
wa_map <- map_data("county") %>% 
  filter(region == "washington")

# Check the first few rows of the map data
head(wa_map)
##        long      lat group order     region subregion
## 1 -118.2356 46.73617  2891 84970 washington     adams
## 2 -119.3700 46.74190  2891 84971 washington     adams
## 3 -119.3757 46.90232  2891 84972 washington     adams
## 4 -118.9804 46.90805  2891 84973 washington     adams
## 5 -118.9804 47.25756  2891 84974 washington     adams
## 6 -117.9605 47.25756  2891 84975 washington     adams
# Lowercase the county names in both datasets for a successful merge
data <- data %>%
  mutate(subregion = tolower(subregion))

# Merge data with Washington counties map
wa_ev_map <- left_join(wa_map, data, by = "subregion")

# Calculate the centroid (center) of each county for placing text labels
center <- wa_ev_map %>%
  group_by(subregion) %>%
  summarize(long = mean(range(long)), lat = mean(range(lat)))

center <- left_join(center, data, by = "subregion")

# Create the map
ggplot(data = wa_ev_map, aes(x = long, y = lat, group = group)) +
  geom_polygon(color = "black") +  # Draw county borders
  coord_fixed(1.3) +  # Maintain aspect ratio
  geom_text(data = center, aes(x = long, y = lat, label = subregion, group = subregion), 
          color = "red", size = 3) +  # Add county names at centroids
  labs(title = "Electric Vehicle Sales by County in Washington State (2023)") +
  theme_minimal()

Problems I had:

Since the county names in the datasets didn’t match exactly, it was important to make sure they were both formatted correctly and in lowercase.

Another issues was, without getting the center point for each county, adding the names it was on the boarder of the county and it was hard to tell what was what.

For things I could try:

  1. Maybe I can add population density to this map as well.
  2. I don’t know if possible, but can I possible make it interactive, so like I hover over something to see the data instead of it all just sitting out here.
  3. Accompany the map with bar charts showing the total EV sales for each county