I have a spatialpolygonsdataframe with many overlapping features. I want to create a map in which the smaller features are displayed on top of the larger ones so that the small ones aren't fully covered by the large, using ggplot2 in R. How do I reorder the features based on size in the map? ggplot2 code below. I am trying to reorder the geom_polygon bit.

ggplot() +
geom_map(data = US, map = US, aes(map_id=region),
       color="grey45", fill="white", size=0.15)+
geom_polygon(data = all_basins, aes(x = long, y = lat, fill = factor(id)))

1 Answer 1


As this answer points out, if you use sf, you can use st_area to get the area of each polygon, then order the polygons based on area with forcats::fct_reorder. Something like this should work:

US %>%
  st_as_sf(US) %>%
  mutate(area = st_area(geometry)) %>%
  mutate(region = region %>% forcats::fct_reorder(-area)) %>%
  ggplot() +
  geom_sf(aes(fill = region))

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.