Home > Mobile >  Mapping custom geom_point on sf object
Mapping custom geom_point on sf object

Time:10-17

I try to map a geom_point with "custom" coordinates on a sf-object. At first, I receive an error message (I assume because the coordinates are not in the same format). After fixing this issue in the way the error message suggests, however, the point does not appear on the output map (while the map itself is produced). If this is still because of a format-difference between my coordinates, how do I make sure that both point and sf share the same format, ie how can I retrieve the coordinate-format of my sf and use it for my customized geom_points?

nc <- st_read(system.file("shape/nc.shp", package="sf"))

city <- data.frame(name = "raleigh", lat = 45.48067, lng = -122.76204)

ggplot()  
  geom_point(data = city, aes(x = lng, y = lat), color = 'red')

ggplot()  
  geom_sf(data = nc) 

ggplot()  
  geom_point(data = city, aes(x = lng, y = lat), color = 'red')  
  geom_sf(data = nc) 

ggplot()  
  geom_sf(data = nc)   
  coord_sf(lims_method = "geometry_bbox")  
  geom_point(data = city, aes(x = lng, y = lat), color = 'red') 

CodePudding user response:

Hmm.. The point appears on your map.

library(ggplot2)
nc <- sf::st_read(system.file("shape/nc.shp", package="sf"))

city <- data.frame(name = "raleigh", lat = 45.48067, lng = -122.76204)

ggplot()  
  geom_point(data = city, aes(x = lng, y = lat), color = 'red')  
  geom_sf(data = nc) 

However, as the coordinates are far, far west from nc, it doesn't "match" with nc object.

Created on 2022-10-16 with reprex v2.0.2

  • Related