1

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.

3
  • 2
    Try: ggplot() + geom_polygon(aes(x=long, y=lat, group = id), data=shp_fort, fill="red", alpha=.5) Commented Aug 21, 2018 at 6:48
  • @johannes: Many thanks! It worked. I see now that group was the important aesthetic missing. Commented Aug 21, 2018 at 6:55
  • @johannes turned your comment into an answer, as it seems it worked for OP and so its preserved for future reference Commented Sep 19, 2018 at 18:08

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.