I have some code which calculates KUD at 95% from fish position data, restricted by the bounds of a shapefile, and then plots this, and overlays the shapefile on top.
########## BOUND KUD TO SHAPEFILE AREA ##########
library(sf)
library(adehabitatHR)
library(raster)
library(viridis)
library(dplyr)
AB_KUD <- read.csv("AB_KUD.csv")
# Get KUD data
coordinates(AB_KUD) <- c("X_UTM","Y_UTM")
proj4string(AB_KUD) <- CRS("+proj=utm +zone=31 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0")
kud <- kernelUD(AB_KUD[,1], h = "href") # href = the reference bandwidth
# Convert KUD to a spatial points dataframe
kud_points <- getverticeshr(kud, percent = 95)
# Convert to raster
r <- raster(extent(Abberton_utm), ncol=100, nrow=100)
r <- rasterize(kud_points, r)
# Read the shapefile
Abberton <- st_read("WFD_Lake_Water_Bodies_Cycle_2.shp")
# Transform to UTM zone 31
Abberton_utm <- st_transform(Abberton, "+proj=utm +zone=31 +datum=WGS84")
# Convert the reservoir boundary to raster
reservoir_raster <- rasterize(Abberton_utm, r)
# Mask the KUD raster using the reservoir boundary raster
kud_masked <- mask(r, reservoir_raster)
# Plot the masked KUD raster
plot(kud_masked, col = viridis(5),
axes = FALSE, legend = FALSE) # Adding to the existing plot
axis(1, cex.axis = 2)
axis(2, cex.axis = 2)
# Plot just the geometry of the shapefile
plot(st_geometry(Abberton_utm), col = NA, lwd = 1, border = alpha("black", 0.7), add=TRUE)
I wanted to create a loop which then does this for 90%, then 85% all the way down to 10%. I created the first plot as before, then added a loop that created the KUD data at those values, and plotted it on top of the first plot.
########## BOUND KUD TO SHAPEFILE AREA ##########
library(sf)
library(adehabitatHR)
library(raster)
library(viridis)
library(dplyr)
AB_KUD <- read.csv("AB_KUD.csv")
# Get KUD data
coordinates(AB_KUD) <- c("X_UTM","Y_UTM")
proj4string(AB_KUD) <- CRS("+proj=utm +zone=31 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0")
kud <- kernelUD(AB_KUD[,1], h = "href") # href = the reference bandwidth
# Convert KUD to a spatial points dataframe
kud_points <- getverticeshr(kud, percent = 95)
# Convert to raster
r <- raster(extent(Abberton_utm), ncol=100, nrow=100)
r <- rasterize(kud_points, r)
# Read the shapefile
Abberton <- st_read("WFD_Lake_Water_Bodies_Cycle_2.shp")
# Transform to UTM zone 31
Abberton_utm <- st_transform(Abberton, "+proj=utm +zone=31 +datum=WGS84")
# Convert the reservoir boundary to raster
reservoir_raster <- rasterize(Abberton_utm, r)
# Mask the KUD raster using the reservoir boundary raster
kud_masked <- mask(r, reservoir_raster)
# Plot the masked KUD raster
plot(kud_masked, col = viridis(5),
axes = FALSE, legend = FALSE) # Adding to the existing plot
axis(1, cex.axis = 2)
axis(2, cex.axis = 2)
# Add KUD data for different percentiles
for(x in seq(90,10,-5)){
# Convert KUD to spatial points dataframe for the current percentile
kud_points <- getverticeshr(kud, percent = x)
# Convert to raster
r <- raster(extent(Abberton_utm), ncol=100, nrow=100)
r <- rasterize(kud_points, r)
# Mask the KUD raster using the reservoir boundary raster
kud_masked <- mask(r, reservoir_raster)
# Plot the masked KUD raster
plot(kud_masked, col = viridis(100)[100-x], add = TRUE, legend = FALSE)
}
# Plot just the geometry of the shapefile
plot(st_geometry(Abberton_utm), col = NA, lwd = 1, border = alpha("black", 0.7), add=TRUE)
However for some reason it doesn't overlay perfectly. After the third iteration the plotting shifts so they don't overlay but I don't know why.
I tried to see if the extent or resolution changed for some reason for each iteration but they're the same, so I have no idea what causes this. Any idea idea about why it shifts? All the data to replicate this issue can be found here.