ggplot2spatialshapefileggmap

Coordinate data points onto a shapefile in R


I have coordinate data (New Zealand) and a shapefile both in the CRS - WGS 84 / Mercator 41, "EPSG",3994. I want to place the coordinate data points over the shapefile. I can plot the shapefile: see below

but I am unable to place the points on it. I have tried various versions of this code:

eff_sp4 <- st_transform(eff_sp4, st_crs(CRA6_shp))

ggplot(data = CRA6_shp) +
geom_sf() + 
geom_sf(data = eff_sp4, color = "steelblue", size = 2) +
scale_fill_discrete("Statistical Area")+
labs(title = "Potlifts in CRA6 during Fishing Years 2014 - 2023",
   x = "Longitude", y = "Latitude")+
theme(plot.title = element_text(hjust = 0.5), 
    axis.title.x = element_text(margin = margin(t = 10)),  
    axis.title.y = element_text(margin = margin(r = 10)))  

This only produces the following:

Potlifts in CRA6


Solution

  • I think the key point is that you should get the coordinates of points that you want to add into the map. Use st_coordinates() to extract the coordinates or other methods to get the latitude and longitude in a dataframe. Use geom_sf() to plot the map and geom_point() to add the points.

    library(ggplot2)
    library(sf)
    
    # map data
    nc <- sf::st_read(system.file("shape/nc.shp", package="sf"))
    
    
    # replace 'nc' with your coordinate data here, may be 'eff_sp4'
    # for convenience, just select 5 points here
    data_points <- st_coordinates(nc)[1:5, ]
    data_points
    #             X        Y L1 L2 L3
    #[1,] -81.47276 36.23436  1  1  1
    #[2,] -81.54084 36.27251  1  1  1
    #[3,] -81.56198 36.27359  1  1  1
    #[4,] -81.63306 36.34069  1  1  1
    #[5,] -81.74107 36.39178  1  1  1
    
    
    ggplot() +
      geom_sf(data = nc) +
      geom_point(data = data_points, aes(x = X, y = Y))