library(ggplot2)
library(ggmap)
library(maps)
library(mapdata)
library(tidyverse)

Loading US State Data

states <- map_data("state")

Subsetting CA Counties

ca_df <- subset(states, region == "california")
counties <- map_data("county")
ca_county <- subset(counties, region == "california")
#View(ca_county)
#Plot the state first but let’s ditch the axes gridlines, and gray background by using the super-wonderful theme_nothing().
ca_base <- ggplot(data = ca_df, mapping = aes(x = long, y = lat, group = group)) + 
  coord_fixed(1.3) + 
  geom_polygon(color = "black", fill = "gray")
ca_base + theme_nothing()

#Now plot the county boundaries in white:
ca_base + theme_nothing() + 
  geom_polygon(data = ca_county, fill = NA, color = "white") +
  geom_polygon(color = "black", fill = "NA")  # get the state border back on top

Subset Quadrants

#Grouping Counties 
North_Coast <- c("del norte",
  "humboldt",
  "mendocino",
  "sonoma",
  "napa",
  "marin",
  "solano",
  "san francisco",
  "alameda",
  "contra costa",
  "san mateo",
  "santa clara",
  "santa cruz" ,
  "monterey",
  "san luis obispo")
  
Inland_North <-c("siskiyou",
    "modoc",
    "trinity",
    "shasta",
    "lassen",
    "plumas",
    "tehama",
    "glenn",
    "butte",
    "sierra",
    "yuba",
    "nevada",
    "placer",
    "sutter",
    "colusa",
    "yolo",
    "lake",
    "san joaquin",
    "sacramento",
    "amador",
    "el dorado",
    "alpine",
    "calaveras",
    "tuolumne",
    "stanislaus",
    "merced",
    "mariposa",
    "madera",
    "mono",
    "fresno", 
    "san benito"
            )
  
South_Coast <- c("santa barbara", "ventura", "los angeles", "orange", "san diego")
  
Inland_South <- c("imperial", "riverside", "san bernardino", "kern", "inyo", "tulare", "kings")

Add Subsetted Quadrants to Dataframe

quadrants <-ca_county %>% 
  select(long, lat, group, subregion) %>% 
  group_by(subregion) %>% 
  mutate(quadrant = case_when(
         subregion %in% Inland_North ~ "inland_north",
         subregion %in% North_Coast ~ "north_coast",
         subregion %in% South_Coast ~ "south_coast",
         subregion %in% Inland_South ~ "inland_south"
     ))
#View(quadrants)

Color By Quandrant

ca_base + theme_nothing() + 
  geom_polygon(data = quadrants, aes(fill = quadrant)) +
  geom_polygon(color = "black", fill = NA)   # get the state border back on top

Highlight North Coast

north_coast <- subset(quadrants, quadrant == "north_coast") #the == is essential!
  
  ca_base + theme_void() + 
  geom_polygon(data = ca_county, fill = NA, color = "white") +
  geom_polygon(color = "grey70", fill = NA)  +
  geom_polygon(data = north_coast, fill = "royalblue4", color = "white")

Highlight South Coast

south_coast <- subset(quadrants, quadrant == "south_coast") #the == is essential!
  
  ca_base + theme_void() + 
  geom_polygon(data = ca_county, fill = NA, color = "white") +
  geom_polygon(color = "grey70", fill = NA)  +
  geom_polygon(data = south_coast, fill = "royalblue4", color = "white")

Inland North Map

inland_north <- subset(quadrants, quadrant == "inland_north") #the == is essential!
  
  ca_base + theme_void() + 
  geom_polygon(data = ca_county, fill = NA, color = "white") +
  geom_polygon(color = "grey70", fill = NA)  +
  geom_polygon(data = inland_north, fill = "royalblue4", color = "white")

Inland South Map

inland_south <- subset(quadrants, quadrant == "inland_south") #the == is essential!
  
  ca_base + theme_void() + 
  geom_polygon(data = ca_county, fill = NA, color = "white") +
  geom_polygon(color = "grey70", fill = NA)  +
  geom_polygon(data = inland_south, fill = "royalblue4", color = "white")