minnesota_counties <- read_sf("shp_bdry_counties_in_minnesota","mn_county_boundaries")

newmincounties <- minnesota_counties %>%
  group_by(CTY_NAME) %>%
  summarize(count = n())

county_data <- read_excel("CostOfLiving2022_tcm1045-493097.xlsx")

county_data$Area <- sub("Saint Louis County","St. Louis County", county_data$Area, ignore.case = TRUE)

newcounty <- county_data %>%
  group_by(Area) %>%
  summarize(mean_cost_per_person = mean(`Yearly Cost` / `Family Size`)) %>%
  mutate(Area = gsub(" County", "", Area))

merged_data <- left_join(minnesota_counties, newcounty, by = c("CTY_NAME" = "Area"))

city_coordinates <- data.frame(
  name = c("Minneapolis", "St. Paul", "Brainerd", "Duluth", "Rochester"),
  latitude = c(44.9778, 44.9537, 46.3580, 46.7833, 44.0234),
  longitude = c(-93.2650, -93.0892, -94.2005, -92.1066, -92.4802)
)

sf_data <- st_as_sf(city_coordinates, coords = c("longitude", "latitude"), crs=4326)


ggplot(data = merged_data) + 
  geom_sf(mapping = aes(fill = mean_cost_per_person)) + 
  scale_fill_gradient(low = "white", high = "blue") + 
  labs(fill = "Mean cost per person (in US Dollars)",title = "Cost of living per person by county in Minnesota") + 
  geom_sf(data = sf_data, size = 3, color = "red")

LINK TO WATCH THE TUTORIAL: https://youtu.be/X7f4iKmKWEY