What is the cause of 'tearing' of polygons (artifacts) using R, ggplot and geom_polygon?

I have belatedly realised that the sort part of the merge call is to blame. If I use:

foo <- merge(mydf, bar, sort = FALSE)

The polygons plot correctly, at least in this particular case. Thanks to everybody for their input.


Compare the long, lat, piece, and hole columns of foo with those of bar. The merge has somehow lost that information.

The reason for the mess is usually that the polygons are made from more than one piece, and the algorithm draws each piece as separate rings. When the 'piece' info is missing it just draws the whole lot in one go. This reveals itself when there are either real islands or tiny digitising errors.

I think bar has one row per ring, but I guess the merge has produced one row per electoral division. Do the merge at the shapefile level, then fortify it.


dplyr's left_join keeps all of the rows on the left (a) and joins b, adding all the columns from b. This way, none of the information from the data frame that contains the information about the polygons is changed. That might solve this problem.

The command would be:

library(dplyr)
foo <- left_join(mydf, bar)

Tags:

R

Shapefile