I am learning and I made a very similar question when I hadn't learnt what a Spatial Polygons Data Frame was about 10 days ago: https://stackoverflow.com/questions/49193867/select-raster-in-ggplot-near-coastline.
Now, I have discovered the magic of SPDF and choropleth maps and have in essence the same question but with different types of files. I am still wrapping my head around S4 objects and I can't figure out how to subset certain mini-polygons MUNICIPI from my data set.
To the point!
Context:
I have a SPDF that contains these data:

Aim:
I would like to subset all MUNICIPI that are within a certain distance from the coast, clear data for the rest of the MUNICIPI and just work with the my new subset yet plotting the entire geometry.
A choropleth map of what I want looks like this.
Let's say 20km as @urbandatascientist set in his answer to my first question and create a choropleth map of, for example, upper_trees with the subsetted MUNICIPI.
From the https://stackoverflow.com/questions/49193867/select-raster-in-ggplot-near-coastline post I also have the list.of.polygon.boundaries that we'll substract MUNICIPI coordinates from.
Once I subset the coastal MUNICIPI, I'm hoping the map will look something like the green shaded region here. I have also tried to make sure that coordinates are in the same between list.of.polygon.boundaries.
So far, here's my chloropleth map for the entire region using upper_trees as an example:
tm_shape(catasense2)+
tm_fill(col="upper_trees",n=8,style="quantile")+
tm_layout(legend.outside =TRUE)
![map for the whole region[2]](../../images/44a54c4f5143a875aa47c3691ad93fae.webp)