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: Dataframe inside cataconfidence


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.

Data is here.

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]

  • 1
    Just to confirm: you tagged choropleth but the main question is "I would like to subset all MUNICIPI that are within a certain distance from the coast", so it's a subset problem, not display?
    – Simbamangu
    Commented Mar 22, 2018 at 13:32
  • @Simbamangu Hi! That's a fair point. I tagged because in the end I want my dataset to look like this when plotted: agricultura.gencat.cat/web/.content/ag_agricultura/… but I guess it is more about subsetting. I will change the tag.
    – delcast
    Commented Mar 22, 2018 at 14:43
  • You may want to also look at this post with the understanding that spDists can calculate self distances or between feature distances. gis.stackexchange.com/questions/132384/… Commented Mar 22, 2018 at 15:07
  • @JeffreyEvans I saw that link and all his data in is ONE SPDF. I have two things: the SPDF and the list (that contains two lists) of polygon boundaries. How can I compare these two things? (I'm sorry for the very basic question).
    – delcast
    Commented Mar 22, 2018 at 15:55
  • Thus my comment if x and y arguments are specified "spDists" can calculate distance between two different feature classes. Please see ?sp::spDists The resulting matrix object will represent the pairwise distances where rows and columns correspond to the features in each sp object. Commented Mar 22, 2018 at 16:01


Browse other questions tagged or ask your own question.