Search code examples
rgisgeospatialpolygonr-sf

Problem creating dynamic Alpha Hull for multiple groups


how are you? I would like your help, please, to create alpha polygons in R for different groups. I would like these polygons to be separated by my "species" column.

I understand that the dplyr library could help me, probably with the group_by and summarize functions, but I'm having trouble integrating it.

So far, this is what I have worked on

Creating a data frame and load libraries

library(sf)
library(tidiverse)
library(mapview)
library(ggplot2)
library(rnaturalearth)
library(rangeBuilder)


#create data frame
decimalLat <- c(15.53033,15.53033,15.5219,15.1739,19.08333,17.13333,18.09861,21.27667,25.54863,18.80907,25.54147,
         20.0605,23.299,25.5501,25.55005,25.54997)
decimalLon <- c(-92.802,-92.802,-92.7997,-92.3361,-96.9667,-91.9333,-94.4297,-99.2117,-100.271,-99.2168,-100.272,-97.4713,
         -106.443,-100.27,-100.27,-100.27)
species <- c("Sp1", "Sp1","Sp1", "Sp1","Sp1", "Sp1","Sp1", "Sp2","Sp2","Sp2","Sp2","Sp2","Sp2","Sp2","Sp2","Sp2")
df <- data.frame(decimalLat, decimalLon, species)

Ploting points

              #Mexico map
wrld <- ne_countries(scale = "small",returnclass = "sf")
mexico <- filter(wrld, name %in%  "Mexico")
           #create spatial matrix
df.1<- df %>%
  sf::st_as_sf(coords = c("decimalLon", "decimalLat"),crs="EPSG: 4326")
             #plot points
ggplot()+
  geom_sf(data=mexico)+
  geom_sf(data=df.1)

enter image description here

Creating dynamic alpha hull and transformation to visualization

#Creating dynamic alpha hull

ah_dyna <- getDynamicAlphaHull(
  df, #Tabla de puntos/registros de la especie
  coordHeaders = c("decimalLon", "decimalLat"),# x y y
  fraction = 0.90,   # la fracción mínima de registros que debe incluir el polígono
  partCount = 1,  # el máximo de polígonos disyuntos permitidos
  initialAlpha = 1, # Alpha inicial
  alphaIncrement = 0.3,
  alphaCap = 1000,
  clipToCoast = "terrestrial")


#transforming
alpha <- ah_dyna[[2]]
alpha
dynalpha <- st_make_valid(st_as_sf(ah_dyna[[1]]))

#ploting
ggplot()+
  geom_sf(data=mexico)+
  geom_sf(data=dynalpha,
          fill="blue")


enter image description here

So far, so good, but here I have the problem that I would like to create a dynamic alpha hull for each of the species in my initial data frame. I suppose it's with the group_by function, but I don't know how to implement it all together.

This is challenging for me because I would like to automate it, as my final data frame has more than 200 species.

Thank you very much for your responses.


Solution

  • You can split your data by species then run the function on each data frame inside lapply:

    df.2 <- do.call("rbind", lapply(split(df, df$species), function(d) {
      st_sf(geometry = getDynamicAlphaHull(
      d, 
      coordHeaders = c("decimalLon", "decimalLat"),
      fraction = 0.90,   
      partCount = 1,  
      initialAlpha = 1, 
      alphaIncrement = 0.3,
      alphaCap = 1000,
      clipToCoast = "terrestrial")[[1]])
    })) |>
      cbind(species = levels(factor(df$species)))
    
    ggplot() +
      geom_sf(data = mexico) +
      geom_sf(data = df.2, aes(fill = species)) +
      geom_sf(data = df.1)
    

    enter image description here