1
votes

I have a polygon shape file with village polygons for two different districts. When I plot this using base package

plot(shp_test,axes=T)

I get the following figure: enter image description here

However, when I try to plot this using ggplot2,

shp_fort<-fortify(shp_test)
ggplot() + geom_polygon(aes(x=long, y=lat), data=shp_fort, fill="red", alpha=.5)

I observe the following result. enter image description here

Could someone please explain what is happening here and how to resolve this.

I have not shared the shapefile here as its a big data set. However if still required please post a comment and I shall share the shapefile too.

1
Try: ggplot() + geom_polygon(aes(x=long, y=lat, group = id), data=shp_fort, fill="red", alpha=.5) - johannes
@johannes: Many thanks! It worked. I see now that group was the important aesthetic missing. - rar
@johannes turned your comment into an answer, as it seems it worked for OP and so its preserved for future reference - DarkCygnus

1 Answers

0
votes

As indicated by johannes in comments, you should use the group aesthetic and pass it the id of your element:

ggplot() + geom_polygon(aes(x=long, y=lat, group=id), data=shp_fort, fill="red", alpha=.5)

In this example from the ggplot2 page, they indicate the need to use such id when using geom_polygon (emphasis mine):

# When using geom_polygon, you will typically need two data frames:
# one contains the coordinates of each polygon (positions), and the
# other the values associated with each polygon (values). An id
# variable links the two together