Creating Customized US Maps with ggplot2: A Step-by-Step Guide

Introduction to Using ggplot2 for Customizing US Maps

In this article, we will explore how to create a customized US map using ggplot2 that includes specific colors to fill in states based on salespeople assigned to those territories. We will also add state abbreviations and define custom colors for each salesperson.

Overview of ggplot2

ggplot2 is a powerful data visualization library for R that provides a framework for creating high-quality, informative, and insightful visualizations. It uses a grammar-based approach, allowing users to create complex visualizations by combining simple elements in a logical order.

In this article, we will focus on using ggplot2 to create a US map with customized colors based on salespeople assigned to states.

Understanding the Data

To create a customized US map, we first need to prepare our data. We have two datasets: state and assignments. The state dataset provides information about all 50 states in the United States, including their longitudes and latitudes. The assignments dataset contains information about salespeople assigned to specific states.

We will join these two datasets using a left join to create a new dataset called state_join.

# Load required libraries
library(ggplot2)
library(tidyverse)

# Load the state and assignments datasets
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 the state and assignments datasets
state_join <- state %>% 
  left_join(assignments, by = c("region" = "State"))

Creating a Customized US Map

To create a customized US map, we will use ggplot2 to create a polygon layer for each state. We will also add a geom_text layer to display the state abbreviations.

# Create a customized US map
ggplot(data = state_join, aes(x = long, y = lat, group = group, fill = Salesperson)) +
  geom_polygon(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)

Adding Custom Colors

To add custom colors to the map, we can use the scale_fill_manual function. We will specify a list of colors for each salesperson.

# Add custom colors to the map
scale_fill_manual(values = c("#1A1446", "green", "red", "#FFD000"),
                  breaks = c("Erin", "Joe", "Kristen", "Tim"))

Conclusion

In this article, we explored how to create a customized US map using ggplot2 that includes specific colors to fill in states based on salespeople assigned to those territories. We also added state abbreviations and defined custom colors for each salesperson.

We covered the following topics:

  • Overview of ggplot2
  • Understanding the data
  • Creating a customized US map
  • Adding custom colors

By following this guide, you should be able to create your own customized US maps using ggplot2.


Last modified on 2024-04-08