Jazzmatazz
Jazzmatazz

Reputation: 645

How do I use ggplot2 to draw a US Map that uses defined colors to fill in specific states based on person assigned in df?

I have a list of salespeople assigned to specific territories. I'd like to assign specific colors to color in the states according to the Salesperson assigned to that State. In addition, I want the map to include State Name abbreviations (i.e. TX, NY, etc.). I would like to be able to define the colors to the specific people as well. For example use #1A1446 for Erin, and use #FFD000 for Tim.

library(mapdata)
#> Warning: package 'mapdata' was built under R version 4.1.1
#> Loading required package: maps
#> Warning: package 'maps' was built under R version 4.1.1
library(ggplot2)
library(tidyverse)
#> Warning: package 'tidyr' was built under R version 4.1.1
#> Warning: package 'readr' was built under R version 4.1.1

state <- map_data("state")


assignments <- tibble::tribble(
                  ~State, ~Abbreviation, ~Salesperson,
               "alabama",          "AL",    "Kristen",
                "alaska",          "AK",       "Erin",
               "arizona",          "AZ",       "Erin",
              "arkansas",          "AR",    "Kristen",
            "california",          "CA",       "Erin",
              "colorado",          "CO",       "Erin",
           "connecticut",          "CT",        "Joe",
              "delaware",          "DE",        "Joe",
               "florida",          "FL",        "Tim",
               "georgia",          "GA",        "Tim",
                "hawaii",          "HI",       "Erin",
                 "idaho",          "ID",        "Joe",
              "illinois",          "IL",    "Kristen",
               "indiana",          "IN",    "Kristen",
                  "iowa",          "IA",    "Kristen",
                "kansas",          "KS",    "Kristen",
              "kentucky",          "KY",    "Kristen",
             "louisiana",          "LA",    "Kristen",
                 "maine",          "ME",        "Joe",
              "maryland",          "MD",        "Joe",
         "massachusetts",          "MA",        "Joe",
              "michigan",          "MI",    "Kristen",
             "minnesota",          "MN",    "Kristen",
           "mississippi",          "MS",    "Kristen",
              "missouri",          "MO",    "Kristen",
               "montana",          "MT",       "Erin",
              "nebraska",          "NE",    "Kristen",
                "nevada",          "NV",       "Erin",
         "new hampshire",          "NH",        "Joe",
            "new jersey",          "NJ",        "Joe",
            "new mexico",          "NM",       "Erin",
              "new york",          "NY",        "Joe",
        "north carolina",          "NC",        "Tim",
          "north dakota",          "ND",    "Kristen",
                  "ohio",          "OH",    "Kristen",
              "oklahoma",          "OK",    "Kristen",
                "oregon",          "OR",        "Joe",
          "pennsylvania",          "PA",        "Joe",
          "rhode island",          "RI",        "Joe",
        "south carolina",          "SC",        "Tim",
          "south dakota",          "SD",    "Kristen",
             "tennessee",          "TN",    "Kristen",
                 "texas",          "TX",    "Kristen",
                  "utah",          "UT",        "Joe",
               "vermont",          "VT",        "Joe",
              "virginia",          "VA",        "Tim",
            "washington",          "WA",        "Joe",
         "west virginia",          "WV",    "Kristen",
             "wisconsin",          "WI",    "Kristen",
               "wyoming",          "WY",       "Erin",
  "district of columbia",          "DC",        "Joe"
  )



# join salesperson assignments to state df
state_join <- state %>%
  left_join(assignments, by = c("region" = "State"))


ggplot(data = state_join, aes(x = long, y = lat, fill = region, group = group)) +
  geom_polygon(color = "white") +
  guides(fill = FALSE) +
  theme(
    axis.title.x = element_blank(), axis.text.x = element_blank(), axis.ticks.x = element_blank(),
    axis.title.y = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank()
  ) +
  ggtitle("U.S. Map with States") +
  coord_fixed(1.3)
#> Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
#> "none")` instead.

Upvotes: 0

Views: 695

Answers (1)

crs_16
crs_16

Reputation: 31

You need to add the Salesperson as a fill aesthetic in the 'geom_polygon' step.

geom_polygon(aes(fill = Salesperson), color = "black")

The color = "black", makes the state outlines black.

Also, if you want their names to show in the legend, you can remove the 'guides' step.

To add colors, you add another step in the ggplot process:

scale_fill_manual(values = c("#1A1446", "green", "red", "#FFD000"),
                  breaks = c("Erin", "Joe", "Kristen", "Tim"))

To add the abbreviations, you can make a separate data frame that has the abbreviations and state centers from the datasets state.abbr and state.center included in R.

library(tidyverse)

state_center <- tibble(
  Abbreviation = datasets::state.abb,
  c_long = datasets::state.center$x,
  c_lat = datasets::state.center$y
) %>% 
  filter(!state.abb %in% c("AK", "HI"))


ggplot() +
  geom_polygon(data = state_join,aes(x = long, y = lat, group = group, fill = Salesperson), color = "black") +
  geom_text(data = state_center, aes(c_long, c_lat, label = Abbreviation), color = "white") +
  theme(
    axis.title.x = element_blank(), axis.text.x = element_blank(), axis.ticks.x = element_blank(),
    axis.title.y = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank()
  ) +
  scale_fill_manual(values = c("#1A1446", "green", "red", "#FFD000"),
                    breaks = c("Erin", "Joe", "Kristen", "Tim")) +
  ggtitle("U.S. Map with States") +
  coord_fixed(1.3)

Here is the result: enter image description here

Upvotes: 3

Related Questions