I created a map that shows a range of rates by county. I want to see an outline for each county in the northeast region of North Carolina, but several counties are missing.
Here's the code that I tried
ggplot(state_dat %>% filter(REGION=="1"), aes(fill = Rate)) +
geom_sf() +
scale_fill_stepsn(colours = palmap,
na.value = "#d0d1e6",
breaks = c(0,5, 10, 15, 20),
labels = c("0-5", "5-10", "10-15", "15-20", "20+"),
limits = c(0,25)) +
theme_void()
I specified that the counties with NA values should be filled in with lightest shade of blue, but instead they are blank and counties are disconnected like this....
How can I add the county boundaries?
One way to achieve this is to create a subset of your data that omits the NA values, then plot two geom_sf()
layers in the same plot. Here is a repex with example data. I have changed your desired NA hex value for illustrative purposes:
library(sf)
library(dplyr)
library(ggplot2)
# Sample data
nc <- st_read(system.file("shape/nc.shp", package="sf"))
# Create second sf with Rate column and remove NAs
set.seed(1)
nc1 <- nc %>%
mutate(Rate = sample(c(NA, 1:20), 100, replace = TRUE)) %>%
filter(!is.na(Rate))
ggplot() +
geom_sf(data = nc,
fill = "#ffff6d") +
geom_sf(data = nc1,
aes(fill = Rate)) +
scale_colour_stepsn(colours = terrain.colors(5),
breaks = c(0,5, 10, 15, 20),
labels = c("0-5", "5-10", "10-15", "15-20", "20+"),
limits = c(0,25)) +
theme_void()