rggplot2gisshapefiler-maptools

SpatialPolygonDataFrame plotting using ggplot


I have a shape file for the greater London area. I use the readShapePoly function from the maptools package to load it in R as a SpatialPolygonDataFrame .

I want to the plot those polygons .. Which I have already done by using the basic of plot function in R. The output looks as shown in this image:

plot of London

Now, I am trying to plot the same shape file using ggplot2 but it doesn't work for me. I am getting some weird lines in the graph as shown below: ggplot2 of London

The code I used was :

london.wards <- readShapePoly("~/TD/london_wards2013/london_wards2013.shp"
                          , proj4string=CRS(projString))
wards.count <- nrow(london.wards@data)
# assign id for each lsoa

london.wards@data$id <- 1:wards.count
wards.fort <- fortify(london.wards, region='id')
ggplot(wards.fort, aes(long, lat)) + geom_polygon(colour='black', fill='white')

where projString is the projection string describing the projection used for the input shape file.


Solution

  • You need to add an additional aesthetic, group. Assuming the polygon id is called ID, the synatx will look like:

    ggplot(wards.fort, aes(x = long, y = lat, group = ID)) + 
       geom_polygon(colour='black', fill='white')