I am trying to make a map of the U.S. which shows two categorical variables, for example the income group of the state and the region the state belongs in. The idea is to use the "fill" aesthetic to show the income level of each state, and then the "color" aesthetic to show the outlines of each region. The information that I am trying to communicate is that lower-income and higher-income states are clustered in certain regions.
An alternative would be to somehow show the regional boundaries with a bolder or thicker boundary than the state boundaries, but I am also unsure how to do this. Other ideas which communicate the same information would also be welcome.
Ideally, it would be some combination of the following two plots:
## Create map data
state_map_data <- map_data("state")
state_regions <- tibble(state_name = tolower(state.name), state.region,
as_tibble(state.x77)) %>%
mutate(income_cat = cut(Income, breaks = 3,
labels = c("low", "medium", "high")))
state_map_data <- state_map_data %>%
left_join(state_regions,
by = c("region" = "state_name"))
## Map with just income
p1 <- ggplot()
geom_polygon(data = state_map_data,
aes(x = long, y = lat, group = group,
fill = income_cat))
print(p1)
This generates the following map with income
## Map with just regions
p2 <- ggplot()
geom_polygon(data = state_map_data,
aes(x = long, y = lat, group = group,
color = state.region))
print(p2)
This generates the following map with regions
## Map with both
p <- ggplot()
geom_polygon(data = state_map_data,
aes(x = long, y = lat, group = group,
fill = income_cat))
geom_polygon(data = state_map_data,
aes(x = long, y = lat, group = group,
color = state.region))
print(p)
This does not produce the expected results of a map with both a color outline by region and filled states by income as seen here
CodePudding user response:
The way you have your code you are drawing two sets of polygons, with state.region
polygons on top of the income_cat
polygons. Instead, you want to draw one set of polygons with the correct outline color and fill color:
ggplot()
geom_polygon(data = state_map_data,
aes(x = long, y = lat, group = group,
fill = income_cat, color = state.region)
)