Clustering geographical data in R - r

I'm new to R and would like to cluster geographical information using k-medoids. While accounting for curvature of the earth, I need to cluster on latitude, longitude, and depth.
My ultimate goal is to plot the colored data as a map.

Related

masking kernel density map by study region in rhr

I have rhr-produced kernel density estimation of several marine species at the global scale. I want to mask these maps by the world map. However the resulting map is weird since latitude and logitude ranges are not same as a coordinate system.
An example output is same as below:
A kernel density map produced by rhr package, masked by global marine areas

plotting polygons in python using geopandas

I have a data frame with latitude, longitude and annual consumption data. I am trying to plot the annual consumption data using GeoPandas with a quantile scheme by making a polygon column. I followed the following tutorial.
enter link description here
I used the latitude and longitude data to make polygon shapes using the following code.
full_dataframe["geometry"] = Polygon(list(zip(full_dataframe["long"], full_dataframe["lat"])))
merged2 = GeoDataFrame(full_dataframe)
merged2.plot(column='annual_consume', scheme='quantiles', k=4, edgecolor='k',
cmap='OrRd', legend=True,
legend_kwds={'loc': 'center left', 'bbox_to_anchor':(1,0.5)})
My data frame looks like below
The issue is, when I plot it, it looks like a big mess of interconnecting points. The boundaries are not seperated.
I want a similar boundary separation like the tutorial based on longitude and latitude data.

Trying to convert Numerical Values of Lat/Long into Spatial Data

I am working with a dataset that features chemical analyses from different locations within a cave, with each analysis ordered by a site number and that sites latitude and longitude. This first image is what I had done originally simply using ggplot.
Concentrations mapped by color over map
But what I want to do is use the shapefile of the cave system from which the data is sourced from and do something similar by plotting the points over the system and then coloring them by concentration. This below is the shapefile that I uploaded
Cave system shapefile
So basically I want to be able to map the chemical data from my dataset used to map the first figure, but on the map of the shapefile. Initially it kept on saying that it could not plot on top of it. So I figured I had to convert the latitude and longitude into spatial coordinates that could then be mapped on the shapefile.
Master_Cave_data <- Master_cave_data %>%
st_as_sf(MastMaster_cave_data, agr = "identity", coord = Lat_DD)
This was what I had thought to use in order to convert the numerical Latitude cooridnates into spatial data.

R: Species distribution modeling for a riverine species - all coordinates not falling in raster cells

I am running Species Distribution Modeling in R in biomod2 package for an riverine species in the Ganges River basin so I clipped the bioclim layer with the river network that I obtained from hydrosheds. Resolution of bioclim layer and river network at 1km*1km. But the problem arose as not all coordinates fell on the raster cells (probable causes - coordinates taken at banks, river networks not correctly delineated etc.)
So how do I overcome this problem? Do I pull the coordinates to the nearest raster cell (nearest in terms of vertices? - If this is the correct method than an easier way to do this too) or do I just leave the coordinates - more than half occurrence points would be deleted.
enter image description here

Interactive plot for spatial data in R markdown (to html)

So basically I have some spatial data, which I've found weighted matrix by distance dnearneigh in R and I am wondering if I can generate an interactive plot of the link distribution by changing the distance variable for the weighted matrix.
The data set:
CA.poly <- readShapePoly('CaliforniaCounty.shp')
This is a shapefile for California county and using this I can generate a weight matrix based on the distance of each county.
coords<-coordinates(CA.poly)
W_dist<-dnearneigh(coords,0,1.5,longlat = FALSE)
And after generating the matrix I can plot the link distribution by using:
plot(W_dist,coordinates(CA.poly))
This will show a network of counties where two counties are connected if their distance (between centroids) are less than 1.5 km.
All the codes are in a Rmd file and I am wondering is there a way to output in html an interactive plot where you (user) can change the distance parameter (change 1.5 km to 1 km for example) and the graph will change.
I looked up methods like using shiny and plotly but I don't think they suit my goal. Any suggestions?

Resources