#devtools::install_github("MiriamLL/sula")
library(sula)
Remove undesired locations
Create a buffer to remove locations.
Intro
When studying animals using GPSs, we often need to remove their central location. Here, I am sharing a function I created to eliminate all the locations within an area.
Data
For the exercises, test data is from masked boobies.
To access the data you have to install the package sula: devtools::install_github(“MiriamLL/sula”)
This data frame contains data from 10 individuals.
unique(sula::GPS_raw$IDs)
To load it into the environment.
<-GPS_raw GPS_ten
Plot your data
library(tidyverse)
Here you can see all the recorded locations using the GPSs.
<-ggplot()+
Original_plotgeom_point(data = GPS_ten,
aes(x=Longitude,y = Latitude),color='red', size=0.5)+
scale_x_continuous(labels = function(x) paste0(-x, '\u00B0')) +
scale_y_continuous(labels = function(x) paste0(-x, '\u00B0')) +
xlab('Longitude')+ylab('Latitude')+
theme(
panel.background = element_rect(fill = '#edf2f4'),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),legend.position='none',
panel.border = element_rect(colour = "black", fill=NA, size=1.5)
) Original_plot
Create a buffer
Select a location
<-data.frame(Longitude=-109.5,Latitude=-27.2) This_point
Create a buffer
I created this function to create a buffer around a point
<-function(central_point=central_point, buffer_km=buffer_km){
create_buffer<- sp::SpatialPoints(cbind(central_point$Longitude,central_point$Latitude))
central_spatial::proj4string(central_spatial)= sp::CRS("+init=epsg:4326")
sp<- sp::spTransform(central_spatial, sp::CRS("+init=epsg:4326"))
central_spatial <-sf::st_as_sf(central_spatial)
central_spatial<-buffer_km*1000
buffer_dist<-sf::st_buffer(central_spatial, buffer_dist)
central_bufferreturn(central_buffer)
}
The parameters to give are the kilometers and the central point
<-create_buffer(central_point=This_point,buffer_km=20) This_buffer
class(This_buffer)
Here you can see the buffer you created using the point (or central location)
<-ggplot()+
Buffer_plotgeom_point(data = GPS_ten,
aes(x=Longitude,y = Latitude),color='red',size=0.5)+
geom_point(data=This_point,
aes(x=Longitude,y=Latitude),color='blue')+
geom_sf(data=This_buffer,colour='blue', fill='transparent', linetype='dashed')+
scale_x_continuous(labels = function(x) paste0(-x, '\u00B0')) +
scale_y_continuous(labels = function(x) paste0(-x, '\u00B0')) +
xlab('Longitude')+ylab('Latitude')+
theme(
panel.background = element_rect(fill = '#edf2f4'),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),legend.position='none',
panel.border = element_rect(colour = "black", fill=NA, size=1.5)
) Buffer_plot
Using the function from the package sp you can create an spatial object using your GPS data
<- GPS_ten
GPS_sp ::coordinates(GPS_sp) <- ~Longitude + Latitude
sp::proj4string(GPS_sp) = sp::CRS("+init=epsg:4326")
sp<-sf::st_as_sf(GPS_sp) GPS_sp
class(GPS_sp)
class(This_buffer)
Here the function over identifies which location intersect with the buffer.
<-sapply(sf::st_intersects(GPS_sp,This_buffer), function(z) if (length(z)==0) NA_integer_ else z[1]) GPS_over
This information can be added as a column in the data frame.
$In_or_out <- as.numeric(GPS_over) GPS_ten
To remove the locations that were within the buffer you can use the function filter and is.na from the package tidyverse
<- GPS_ten %>%
GPS_without filter(is.na(In_or_out)==TRUE)
Here you can see that the locations inside the buffer were removed.
<-ggplot()+
Filtered_plotgeom_point(data = GPS_without,
aes(x=Longitude,y = Latitude),color='red',size=0.5)+
geom_point(data=This_point,
aes(x=Longitude,y=Latitude),color='blue')+
geom_sf(data=This_buffer,colour='blue', fill='transparent', linetype='dashed')+
scale_x_continuous(labels = function(x) paste0(-x, '\u00B0')) +
scale_y_continuous(labels = function(x) paste0(-x, '\u00B0')) +
xlab('Longitude')+ylab('Latitude')+
theme(
panel.background = element_rect(fill = '#edf2f4'),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),legend.position='none',
panel.border = element_rect(colour = "black", fill=NA, size=1.5)
) Filtered_plot
This function can be used to remove all the nest locations within a distance buffer. You could also replace the polygon (here the buffer) for some land shapefiles.
Compare
Using the package patchwork we can see the difference side by side.
library(patchwork)
+Buffer_plot+Filtered_plot Original_plot
Further reading
Other functions that did the job:
gBuffer from rgeos deprecated
Read more about package sf