Reputation: 73
I'm trying to overlay several points on a map I've created from a shapefile (downloaded at this link). I'm using the sf package to draw my maps and am able to get get the following map.
first.map<-ggplot()+
geom_sf(data = shp)+
ggtitle("First Map")
As you can see, this works fine, but when I try to overlay my points, I get the following results.
second.map<-ggplot()+
geom_sf(data = shp)+
geom_point(data=locations, aes(long,lat),inherit.aes = F)+
ggtitle("Second Map")
I think this has something to do with how sf is interpreting the coordinates for my points, but I can't figure out how to get my point coordinates into what R wants me to. Below are the coordinates that I'm trying to overlay. I'm not super experienced with working with this type of data so any help would be appreciated.
locations<-data.frame(id=c(1,2,3,4,5),
long=c(96.73872,96.69264,96.69264,96.69250,96.68029),
lat=c(43.52769,43.53598,43.53598,43.54669,43.53585))
Upvotes: 2
Views: 657
Reputation: 2924
This isn't a strength of mine so was curious to play too.
I got it to work like this,
*-1
in the input data to swap from west to eastKeen to hear better practices from others but this should get you going.
shp <- st_read("Subdivisions/Subdivisions.shp")
locations<-data.frame(id=c(1,2,3,4,5),
long=c(96.73872,96.69264,96.69264,96.69250,96.68029)*-1,
lat=c(43.52769,43.53598,43.53598,43.54669,43.53585))
locations_g <- st_as_sf(locations, coords = c("long", "lat"), crs = 4326)
shp2 <- st_transform(shp, 4326)
ggplot()+
geom_sf(data = shp2)+
geom_sf(data=locations_g)+
ggtitle("Second Map")
Upvotes: 3