How to overlay an osmar shape over a ggmap with R?

151 Views Asked by At

I am having trouble overlaying an osmar shape over a ggmap with R.

I get the osmar object (in this case it is Lake Park in Milwaukee, WI):

lp<-get_osm(relation(6044259), full=T)

I convert it to a shape:

lpp<-as_sp(lp, "lines")

This looks fine and I see the outline of the park:

[plot(lpp)][1]

I then try to overlay this over a map:

area.points <- fortify(lpp)
mapImage <- get_map(location = c(lon = -87.89, lat = 43.05),  color = "color",           source = "google",  zoom = 13)
ggmap(mapImage) + geom_path(aes(x=long,y=lat), data=area.points, color=colors[9], alpha=0.5)+labs(x="Longitude", y="Latitude")

When I plot the map and the area overlay of the park it is not cleanly plotting the park outline but seems to also be plotting a line between each point.

ggmap output

1

There are 1 best solutions below

0
On

adding group=group in the aes section of geom_path solved the problem:

ggmap(mapImage) + geom_path(aes(x=long,y=lat,group=group), data=area.points, 
                            color=colors[9], alpha=0.5)+
                  labs(x="Longitude", y="Latitude")