1

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:

enter image description here

And here's the code's output with the second shapefile:

enter image description here

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.

6
  • Out of interest, what happens if you remove the minus signs in the second call? Or just the minus sign on the x value? Commented Nov 6, 2020 at 20:38
  • @AllanCameron - just tried it, it didn't work, unfortunately Commented Nov 6, 2020 at 20:41
  • 2
    Are the crs different between the two? Commented Nov 6, 2020 at 20:46
  • @at80, I just checked. The first shapefile (the one that works) has a projected CRS: NA; the second, which does not work, has a projected CRS: WGS 84 / UTM zone 19S. You think this difference may be driving the problem? Commented Nov 6, 2020 at 21:02
  • I'm pretty sure that the point units need to match the crs. If you convert to epsg 4326 then it might fix the issue. I believe there's a ggplot function to handle that for you. Commented Nov 6, 2020 at 21:05

1 Answer 1

3

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")
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.