I have a data frame which has just three columns: Latitude, Longitude, Species. This df has about 6,000 rows.
I have successfully mapped it with ggplot, but there are many plot points which are on land and in the middle of the ocean (the species I'm plotting are intertidal - Patella vulgata limpets). I need to get rid of these specific points somehow (one by one from the csv file will not do - this would take days). I only want the high quality plot points which are along the coastline only.
secondly, I need to present the final plot in a 'clean' way so that it can feature in a scientific paper. For this I need to make the plot points appear as a continuous line, or shaded area where the species exists. Right now it looks like a mess because of the number of points.
This is the code I have so far, which does not seem to work. (this was suggested by AI)
########################################################################################
library(sf)
library(ggplot2)
Read the land shapefile into R as an sf object
land_sf <- st_read("ne_50m_land.shp")
Read in the Vulgata.combined data as a data.frame object
Vulgata.combined <- read.csv("Patella GBIF and OBIS COMBINED (new).csv", stringsAsFactors = FALSE)
Use subset function to extract only 'Patella vulgata' rows
Vulgata.combined <- subset(Vulgata.combined, Species == "Patella vulgata")
Remove rows with missing coordinate values
Vulgata.combined <- na.omit(Vulgata.combined[c("Longitude", "Latitude")])
Remove full row duplicates
library(dplyr)
Vulgata.combined <- distinct(Vulgata.combined)
Convert the Vulgata.combined data.frame to an sf object
Vulgata.combined_sf <- st_as_sf(Vulgata.combined, coords = c("Longitude", "Latitude"), crs = 4326)
Perform a spatial join between the Vulgata.combined sf object and the land sf object
Vulgata.land <- st_intersection(Vulgata.combined_sf, land_sf) # this does nothing
#check progress like this
library(sf)
library(ggplot2)
library(progressr)
Remove all points that are more than 100 meters away from land
Vulgata.land <- st_buffer(land_sf, dist = 100) %>%
st_difference(Vulgata.land)
Plot the resulting sf object using ggplot2
ggplot() +
geom_sf(data = land_sf, fill = "lightgray") +
geom_sf(data = Vulgata.land, color = "red", size = 0.5)