I am using the sf
library to plot aggregate data and I would like to restrict my polygons to the borders of another set of polygons.
The image below is what I have
I'm new to sf
so I apologize if I am not using the right terminology but I would like to "constrain" the image to the grey borders while retaining all of it's contents. Is there a clean way to do this? In other words, dropping everything outside the borders.
Would it be easier to define new polygons in df_map
or format the image itself within the ggplot
call? I would prefer the latter but, if the former, how would this be done.
The code I can share on how the above to generate the above figure is as follows:
library(tidyverse)
library(sf)
library(ggplot2)
ggplot() +
geom_sf(data = df_map, aes(fill = yellow_or_red), color = NA) +
geom_sf(data = grey_borders, fill = NA, lwd = 1) +
scale_fill_gradientn(breaks = seq(0, 1250, 250),
colors = brewer.pal(length(seq(0, 1250, 250)),
"YlOrRd")) +
theme(legend.title = element_blank()) +
theme(axis.title = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.text = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank())