Using ggplot
I want to add points to a sf
map. Take this code:
ggplot(data = shapefile) +
geom_sf()+
geom_point(x = -70.67,y =-33.45, size = 10, colour = "red")
This code works fine for one of my shapefiles but not for another, and I'm not sure why. Here's the code's output with my first shapefile:
And here's the code's output with the second shapefile:
Potential reasons for why the second call is not recognizing the coordinates? The only difference I'm seeing between the two plots is that in the first, longitude and latitude are annotated numerically, and in the second, after their north/south and east/west orientation.
The inconsistent behavior is due to different projections for each shapefile. You typically need to supply the point locations that match the units of the projection you are using. You could either convert the points to your shapefile's projection, or, if you don't care if the data are in a geographic coordinate system, you can convert to 4326 which is lat/long on WGS84 datum.
Method 1: Maintaining your shapefile's projection. This method will convert your point(s) to a spatial sf
data type, so you can just plot with geom_sf
.
pts <- st_point(c(-70.67, -33.45)) %>% st_sfc() %>% st_as_sf(crs=st_crs(4326))
ggplot(data = shapefile) +
geom_sf() +
geom_sf(data = pts, size = 10, colour = "red")
Method 2: Converting the shapefile to EPSG 4326.
ggplot(data = shapefile %>% st_transform(st_crs(4326))) +
geom_sf() +
geom_point(x = -70.67,y =-33.45, size = 10, colour = "red")