2

when I use ggplot2::ggplot() to create a map using a shapefile I have the problem that small features overlaped by bigger ones. Please note image of the Problem: ggplot overlays the small county by the bigger one.

Please use this shapefile as input data.

load("~/Germany_Bremen_LowerSax_NUTS1.Rdata") # Please use input data mentioned above
library(ggplot2)

plot(shp.nuts.test) # normal plot with visible borders.

shp.f <- fortify(shp.nuts.test)

Map <-   ggplot(shp.f, aes(long, lat, group = group, fill = id))+
  geom_polygon()
Map

Is there any possibility to change the plot order of the shapefile within ggplot?

Any help appreciated! Thanks!

1 Answers1

2

There are a couple of options:

  1. Reorder factors so that the lower levels plot on top of the higher ones.
  2. Add another layer of the hidden group over the plot (shown below)
library(dplyr)

ggplot(shp.f, aes(long, lat, group = group, fill = id))+
geom_polygon()+
geom_polygon(aes(long,lat), data=filter(shp.f, group=='4.1'))

enter image description here

I personally prefer option 2, because it is a huge pain reordering factors and can easily result in unintended consequences. In addition, you could handle more layers on top. Note that the filter function requires the dplyr library (more on dplyr use).

Minnow
  • 1,733
  • 2
  • 26
  • 52
  • Thank you for your quick reply! This is a simple and straight forward solution for this example. But I am looking for a **more general solution** because I want to plot more complex polygon shapefiles (e.g. NUTS3-level). I need to ensure that ggplot plot the polygon features the way that smaller features are not hidden by bigger ones. In QGIS that is no problem without doing any corrections but I unfortunately this seems not to be the case in R/ggplot. – KalleBlomquist Oct 21 '15 at 07:07
  • Yes, I can see that you'd want a general fix rather than a kludge. You could make the fill transparent, such that you can always see each, but you'd lose control of the colors to an extent. It is possible that ggplot is not the best tool to plot this type of map. Have you looked at the [CRAN task views](https://cran.r-project.org/web/views/Spatial.html) for other options? – Minnow Oct 21 '15 at 15:36
  • I have found a good solution that works for me [here](http://stackoverflow.com/a/32186989/2725410). – KalleBlomquist Oct 22 '15 at 07:56