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

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.

Related

R Matching coordinate points into geopolitical areas

I'm trying to find the correspondence between coordinate points and municipalities in a given state. This is, for every coordinate point, the municipality in which it is contained. On one hand, I have the shapefile of the state and its municipalities, on the other, a data frame with coordinate points corresponding to certain named locations.
This is the shapefile zip and the shp file is 22mun.shp
This is an example data frame of locations:
locations <-
tibble(name = c("A", "B"),
Latitude = c(20.598670161163884, 20.741478412120905),
Longitude = c(-100.3565447475217, -99.94098729516392))
In this example, I should obtain that point A is in the municipality named "Queretaro" and that point B is in the municipality "Ezequiel Montes".
How can I find such correspondence on a larger scale where I might have multiple points within a given municipality? Any insights on how to do this efficiently or guidance would be very appreciated.
Thank you

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.

geom_sf does not use geometry coordinates in axes but plots correct shape of polygon?

My overall aim is to combine multiple shape files (polygons of river sub-basins from within a large river basin) into one file and plot as a map. This new combined file will later combine with variable data e.g.(rainfall) and plot by aes().
My problem is:
ggplot()+geom_sf() plots the correct shapes of the polygons but doesn't have the correct co-ordinates on the axes - it doesn't use the values given in the geometry column on the axes.
My thoughts on what is wrong, but I'm not sure how to correct:
The shape file read in has geometry in 'long' 'lat' (crs= 4326) but the crs is saying the coordinates are in UTM Zone 48N WGS84 (crs=32648). If I try and force the crs to 4326 the coordinate values change as if the conversion formula is trying to correct them.
geom_sf and coord_sf are doing something that I don't understand!
.
library(sp)
library(raster)
library(ggplot2)
library(sf)
library(ggsf)
library(rgdal)
library(plyr)
library(dplyr)
library(purrr)
setwd("/Users/.../Sub_Basin_Outlines_withSdata/")
list.files('/Users/.../Sub_Basin_Outlines_withSdata/', pattern='\\.shp$')
Read in individual polygon shape files from folder. Combine with ID.
bangsai <- st_read("./without_S_data/", "Nam Bang Sai")
BasinID <- "BGS"
bangsai <- cbind(bangsai,BasinID)
ing <- st_read("./without_S_data/", "Nam Ing Outline")
BasinID <- "ING"
The two individual shape files import as simple features, see image of R code
Combine the individual sub-basin polygon shape files into one shapefile with multiple features.
all_sub_basins <- rbind(bangsai,ing)
The image shows the values of the coordinates of the polygons/features in all_sub_basins$geometry. They are long lat format yet the proj4sting suggests UTM?
Plot the all_sub_basins simple feature shapefile in ggplot
subbasins <- ggplot()+
geom_sf(data=all_sub_basins, colour="red", fill=NA)
subbasins
The result is a correctly plotted shape file with multiple features (there are more polygons in this image than read in above). However the axes are incorrect (nonsense values) and are not plotting the same values as in the geometry field.
If I add in coord_sf and confirm the crs:
subbasins <- ggplot() +
geom_sf(data=all_sub_basins, colour="red", fill=NA)
coord_sf(datum=st_crs(32648), xlim = c(94,110), ylim = c(9,34))
subbasins
Then I get the Correct axes values but not as coordinates with N and E. It seems as if the geometry isn't recognised as coordinates, just as forced numbers?
I don't mind if the coordinates are UTM Zone 48N or lat long. Could I fix it in any of these ways? If so, how do I achieve that?
Change the shape file crs without changing the values in the geometry column so geom_sf would know to plot the correct axes text.
Extract the geometry from the shape file into a two column .csv file with long and lat columns. Convert csv into a sf and create my own shape file with correct crs.
Last resort, leave the plot as it is and replace new axes text manually.
Any help is much appreciated!

Overlap in labels of point data in rasterVis

I have a raster r, one polygon shapefile regions and a point shapefile cities. I need to plot all three into one map layout. In addition to this I need to label point file with names of cities (cities$city$Town.Name) and their temperature and precipitation value (assigned as cities$labels). So I have used the following code with packages 'raster' and 'rasterVis'.
p1<-levelplot(regions.r,par.settings=mytheme,scales=list(draw=FALSE),xlab="",ylab="",margin=F)+
layer(sp.polygons(regions))+
layer(sp.points(cities,pch=20,cex=1.5,col="black"))
p1+
layer(sp.text(coordinates(cities), txt = cities$city$Town.Name, pos = 3,col="black",font=list(face="bold"),cex=0.8))+
layer(sp.text(coordinates(cities),txt = cities$label,
pos = 1,cex=0.6,col="black"))#Add shapefile labels
This works fine when area has scattered cities distribution (see figure below).
However, if the cities are concentrated in one part I experience overlap of labels (see figure below). Is there a way to avoid the label overlap?

Clustering geographical data in 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.

Resources