divid_plot | R Documentation |
Divide 1 ha square plots into 25 squares subplots of 400m² following a regular 5*5 grid
divid_plot(coordinates_sf, plot_name = "plot_name")
coordinates_sf |
a spatial object representing the plot geometries, either a |
plot_name |
the name as character of the column where the plot name are stored. Default value is 'plot_name'. |
The function takes either a SpatialPolygonsDataFrame
or sf
object containing the plot geometries and the plot names. For each plot, it first
identifies the 4 corners, then creates the 25 square subplots following a regular 5*5 grid. The subplots are named using the xy coordinates inside the plot, starting
from 0_0 for the southeasterly corner to 80_80 for the northwesternly.
A sf
object with the 25 subplots geometries with 2 fields : sous_plot_name and plot_name for each plot.
Hugo Leblanc
## Test 1
# Define the coordinates of the 4 corners
x1 <- c(0, 1, 1, 0, 0)
y1 <- c(0, 0, 1, 1, 0)
x2 <- c(2, 2, 3, 3, 2)
y2 <- c(1, 2, 2, 1, 1)
# Combine x and y coordinates into matrix
coords1 <- cbind(x1, y1)
coords2 <- cbind(x2, y2)
# Rotate by 20 degrees the matrix coord1
angle <- pi/9 # angle in radians
rotation_mat <- matrix(c(cos(angle), sin(angle), -sin(angle), cos(angle)), nrow = 2)
coords1 <- coords1 %*% rotation_mat
# Create SF object
poly1 <- st_sfc(st_polygon(list(coords1)))
poly2 <- st_sfc(st_polygon(list(coords2)))
coordinates_sf <-
st_as_sf(data.frame(
plot_name = c('Plot_001', 'Plot_002'),
geometry = c(poly1, poly2)
))
#Plot
plot(coordinates_sf$geometry)
# Divide the plot into smaller squares
sub_plot <- divid_plot(coordinates_sf = coordinates_sf, plot_name = 'plot_name')
# Plot the plots and the result subplots
par(mfrow = c(1, 2))
plot(coordinates_sf$geometry, main = "Plots")
plot(sub_plot$geometry, main = "Subplots")
# Plot the plots and the result subplots
library(ggplot2)
ggplot(sub_plot) +
geom_sf() +
scale_fill_continuous(type = 'viridis')+
geom_sf_text(aes(label = as.character(sous_plot_name)))
## Test 2
library(plotsdatabase)
# Extract datas
x <- query_plots(locality_name = "Mbalmayo", extract_individuals = TRUE, show_all_coordinates = TRUE)
coordinates_sf <- x$coordinates_sf
sub_plot <- divid_plot(coordinates_sf,'plot_name')
par(mfrow = c(1, 1))
for(i in 1:length(unique(sub_plot$plot_name))) {
print(ggplot(sub_plot %>% filter(plot_name == unique(plot_name)[i])) +
geom_sf() +
scale_fill_continuous(type = 'viridis')+
geom_sf_label(aes(label = as.character(sous_plot_name))) +
ggtitle(paste(unique(unique(sub_plot$plot_name)[i]))) )
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.