Anna Alves
Anna Alves

Reputation: 31

How do I use ggplot2 to draw a US Map that uses two colors to fill in specific states based on a yes or no variable

Currently, I have this

library(usmap)
library(ggplot2)
plot_usmap(states = "states") + 
  labs(title = "US States",
  subtitle = "This is a blank map of the counties of the United States.") + 
  theme(panel.background = element_rect(color = "BLACK", fill = "GRAY"))

how would I make certain states like Texas, Utah, and Oklahoma one color, and other states like California, Maine, and Oregon another color.

Upvotes: 3

Views: 1958

Answers (1)

Axeman
Axeman

Reputation: 35392

I think you need to write the plotting yourself in that case, but luckily that isn't too difficult. Here's an example:

# choose your states
my_states <- c('Texas', 'Utah', 'Oklahoma')
# get the data
d <- us_map(regions = "states")

# now plot manually. The `full` here refers to the full name of the state, 
# which is a column in `d`. Use `abbr` to use abbreviations.
ggplot(d, aes(x, y, group = group, fill = full %in% my_states)) +
  geom_polygon(color = 'black') +
  coord_equal() +
  # Choose your two colors here:
  scale_fill_manual(values = c('white', 'firebrick'), guide = 'none') +
  usmap:::theme_map() +
  labs(
    title = "US States",
    subtitle = "This is a blank map of the counties of the United States."
  ) + 
  theme(panel.background = element_rect(color = "BLACK", fill = "GRAY"))

enter image description here

Upvotes: 4

Related Questions