KalleBlomquist
KalleBlomquist

Reputation: 21

R - ggplot shapefile small features overlaped by bigger ones

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!

Upvotes: 2

Views: 270

Answers (1)

Minnow
Minnow

Reputation: 1821

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).

Upvotes: 2

Related Questions