OverFlow Police
OverFlow Police

Reputation: 861

change border color of a county in ggplot in R

I want to use a given data frame with a column called frequency to plot counties in the USA, and color the counties using the freq. column. In order to do so, I do:

data(county.fips) # Load the county.fips dataset for plotting
cnty <- map_data("county") # Load the county data from the maps package
cnty2 <- cnty %>%
         mutate(polyname = paste(region, subregion, sep=",")) %>%
         left_join(county.fips, by="polyname")

head(cnty2)

       long        lat group order  region subregion        polyname fips
-86.5051651 32.3491974     1     1 alabama   autauga alabama,autauga 1001
-86.5338211 32.3549271     1     2 alabama   autauga alabama,autauga 1001
-86.5452728 32.3663864     1     3 alabama   autauga alabama,autauga 1001
-86.5567322 32.3778458     1     4 alabama   autauga alabama,autauga 1001
-86.5796585 32.3835716     1     5 alabama   autauga alabama,autauga 1001
-86.5911102 32.3778458     1     6 alabama   autauga alabama,autauga 1001

Where the first few rows of my own data table looks like, dt:

     query                NN   freq         long        lat group order    region   subregion
      53047             55121    7308  -91.4383392 43.9916992  3048 90132 wisconsin trempealeau
      53047             55121    7308  -91.4956284 44.0146179  3048 90133 wisconsin trempealeau
      53047             55121    7308  -91.5471954 44.0318031  3048 90134 wisconsin trempealeau
      53047             55121    7308  -91.5529251 44.1292114  3048 90136 wisconsin trempealeau
      53047             55121    7308  -91.5701141 44.1463966  3048 90137 wisconsin trempealeau
               polyname
 wisconsin,trempealeau
 wisconsin,trempealeau
 wisconsin,trempealeau
 wisconsin,trempealeau
 wisconsin,trempealeau

How can I change the color of borders of two counties via fips column?

Lets say I want the county associated with fips 53047 and 55121 from columns query and NN to have red and yellow boundaries respectively.

What I have done so far is the following:

ggplot(dt, aes(long, lat, group = group)) + 
           geom_polygon(data = county2, fill="lightgrey") +
           geom_polygon(aes(fill = analog_freq), colour = rgb(1, 1, 1, 0.2))  +
           coord_quickmap() + 
           theme(legend.title = element_blank(),
                 axis.text.x = element_blank(),
                 axis.text.y = element_blank(),
                 axis.ticks.x = element_blank(),
                 axis.ticks.y = element_blank(),
                 axis.title.x = element_blank(),
                 axis.title.y = element_blank()) + 
           ggtitle(title_p)

Any suggestions?

Upvotes: 1

Views: 1171

Answers (1)

yake84
yake84

Reputation: 3236

I like scale_*_identity() for these kinds of plots. See if this example helps you out.

states <- 
  map_data("state") %>%
  group_by(region) %>% 
  filter(min(long) < -100) %>% 
  ungroup() %>% 
  # specify the colors I want to use + line thickness
  mutate(
    outline = case_when(
      region == "idaho" ~ "yellow",
      region == "arizona" ~ "red",
      TRUE ~ "white"),
    size = ifelse(outline == "white", 0.5, 3)
  )

ggplot(states, aes(long, lat, group = group)) +
  geom_polygon(aes(color = outline, size = size)) +
  scale_color_identity() +
  scale_size_identity()

enter image description here

Upvotes: 3

Related Questions