I have a SpatialPolygonDataframe (loaded with readOGR) I want to plot in either tmap or leaflet. A lot of the polygons are overlapping, and the smaller polygons are completely covered by larger polygons. Is there a way of rearranging poltorder based on polygonsize?

Sorry for not making a reproducible example…

  • Would giving the polygons a transparent fill be enough? If not, my guess is you will have to split your object into several layers. – jsta Nov 23 '18 at 13:51

You can set the plotting order of a SpatialPolygonsDataFrame by assigning to the plotOrder slot.

You can use area to get the area of each row of a SpatialPolygonsDataFrame.

You can use order to get a vector that orders its argument.

You can negate such an argument to sort in the right direction.

Hence for some SpatialPolygonsDataFrame with 3 features ps:

> ps@plotOrder = order(-area(ps))
> plot(ps, col=c(1,2,3)) # colour the three features in the first three colours

will plot the smallest last so they show over larger ones.

That's fine for base graphics - I don't know if leaflet and tmap will respect this. In which case if they are using the order in the data frame, reorder the data frame by decreasing area:

ps = ps[order(-area(ps)),]

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.