I am trying to generate a function which will create a Kernel Density Estimation using spatial data. I would like to create a loop within the section of my function titled "# compute homerange ranges for a,b,c,d% and object is returned as spatial polygon data frames".
I am having trouble creating a for loop for this because the objects are returned as spatial polygon data frames. Is there any way someone can help me with this?
I have attached the code for my function below
magic_map<- function(datapoints, a, b, c, d, spatialdata, Data.type, Area.Name){
#Run the KDE
kde.output <- kernelUD(datapoints, h="href", grid = 1000)
#Convert to Raster
kde <- raster(kde.output)
Set projection to British National Grid
projection(kde) <- CRS("+init=EPSG:27700")
compute homerange ranges for a,b,c,d% and object is returned as spatial polygon data frames
a <- getverticeshr(kde.output, percent = a)
b <- getverticeshr(kde.output, percent = b)
c <- getverticeshr(kde.output, percent = c)
d <- getverticeshr(kde.output, percent = d)
tm_shape(spatialdata) + tm_fill(col = "#bab6b6" ) + tm_borders(alpha=.5, col = "white") +
tm_shape(datapoints) + tm_dots(col = "blue", size=0.005) +
tm_shape(d) + tm_borders(alpha=.7, col = "#a50f60", lwd = 2) + tm_fill(alpha=.3, col = "#a50f60") +
tm_shape(c) + tm_borders(alpha=.7, col = "#a50f15", lwd = 2) + tm_fill(alpha=.2, col = "#a50f15") +
tm_shape(b) + tm_borders(alpha=.7, col = "#de2d26", lwd = 2) + tm_fill(alpha=.15, col = "#de2d26") +
tm_shape(a) + tm_borders(alpha=.7, col = "#fb6a4a", lwd = 2) + tm_fill(alpha=.1, col = "#fb6a4a") +
tm_layout(frame = FALSE, main.title=(paste("Percent Volume Contours for", Data.type, "in", Area.Name, sep = " ")), main.title.size = 0.77)
}