Home > Software engineering >  How do I use ggplot2 to draw a US Map that uses defined colors to fill in specific states based on p
How do I use ggplot2 to draw a US Map that uses defined colors to fill in specific states based on p

Time:11-16

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.

CodePudding user response:

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"))

Here is the result:

enter image description here

  • Related