首页 > 解决方案 > 如何使用 ggplot2 绘制一张美国地图,该地图使用定义的颜色根据 df 中分配的人员填充特定状态?

问题描述

我有一个分配到特定区域的销售人员列表。我想根据分配给该州的销售人员为该州的颜色分配特定的颜色。此外,我希望地图包含州名缩写(即 TX、NY 等)。我也希望能够为特定的人定义颜色。例如#1A1446用于 Erin,#FFD000用于 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.

标签: rggplot2

解决方案


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


推荐阅读