Adding County Names to Map in maps package

Hello,

I am trying to create a map of Michigan with some particular counties highlighted. Which I have done so here:
Rplot
The code I'm working with is:
#create Michigan map

library(ggplot2)
library(ggmap)
library(maps)
library(mapdata)

states <- map_data("state")
michigan <- subset(states, region= "michigan")
mi_df <- subset(states, region == "michigan")
counties <- map_data("county")
mi_county <- subset(counties, region == "michigan")
mi_study_counties<- subset(mi_county, subregion %in% c("chippewa", "cheboygan", "delta", "schoolcraft", "otsego", "menominee"))

mi_base <- ggplot(data = mi_df, mapping = aes(x = long, y = lat, group = group)) +
coord_fixed(1.3) +
geom_polygon(color = "black", fill="white")+
theme_void()
mi_base

mi_base+ geom_polygon(data = mi_study_counties, fill = "gray", color = "black") +
geom_polygon(color = "black", fill = NA)

What I need help with is labeling the county names on the counties that I have highlighted. Also, is there a way to do a fill of different colors for the different counties? Like fill 3 of them gray and 3 of them blue? Any help is greatly appreciated!

If you're interested in dipping your toe into a new spatial paradigm in R, I would highly recommend looking into the sf package. It integrates very well with plotting in ggplot, leaftlet, mapview, and other spatial packages in R.

Below is a worked example of getting state and county sf data from the tigris package and then using ggplot2 to make and label the map. FYI: I used the ggsflabel package for the labels and it is only on github for now. If you need CRAN packages only, you can simply replace geom_sf_label_repel() with geom_sf_label() which is part of ggplot2.

library(ggplot2)
library(dplyr, warn.conflicts = FALSE)
library(ggsflabel, warn.conflicts = FALSE)
library(tigris, warn.conflicts = FALSE)

mi_state <- states(cb = TRUE, class = "sf") %>% 
  filter(NAME == "Michigan")

mi_county <- counties(state = "MI", cb = TRUE, class = "sf")

mi_study_counties <- mi_county %>% 
  filter(NAME %in% c("Chippewa", "Cheboygan", "Delta",
                     "Schoolcraft", "Otsego", "Menominee"))

mi_study_counties %>% 
  ggplot() +
  geom_sf(fill = "gray") +
  geom_sf(data = mi_state, fill = NA) +
  geom_sf_label_repel(aes(label = NAME), force = 50) +
  theme_void()

Created on 2020-03-04 by the reprex package (v0.3.0)

6 Likes

This is perfect! Thank you. Any idea on putting colors for specific counties? I need Delta, Schoolcraft, and Chippewa Counties to be one color, and Menominee, Cheboygan, and Otsego Counties to be another color.

Sure thing! One easy way is to define the colors in the data frame you send into ggplot() and the use scale_fill_identity() to use those colors for fill.

library(ggplot2)
library(dplyr, warn.conflicts = FALSE)
library(ggsflabel, warn.conflicts = FALSE)
library(tigris, warn.conflicts = FALSE)

mi_state <- states(cb = TRUE, class = "sf") %>% 
  filter(NAME == "Michigan")

mi_county <- counties(state = "MI", cb = TRUE, class = "sf")

mi_study_counties <- mi_county %>% 
  filter(NAME %in% c("Chippewa", "Cheboygan", "Delta",
                     "Schoolcraft", "Otsego", "Menominee")) %>% 
  mutate(fill_color = if_else(NAME %in% c("Chippewa", "Delta", "Schoolcraft"),
                              "steelblue", "coral")
         )

mi_study_counties %>% 
  ggplot() +
  geom_sf(aes(fill = fill_color)) +
  geom_sf(data = mi_state, fill = NA) +
  geom_sf_label_repel(aes(label = NAME), size = 3, force = 50) +
  scale_fill_identity() +
  theme_void()

Created on 2020-03-04 by the reprex package (v0.3.0)

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.