ghislainv / forest-plot-grid

Make a forest plot grid from point coordinates and azimut

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Regular grid from one point and one azimut

This repository include functions to generate a regular grid from one point and one azimut.

library(sf)
library(ggplot2)
library(dplyr)
source("forest-plot-grid.R")

Function get_grid() needs the coordinates (in longitude and latitude) of the lower left corner of the grid (point A0) and the azimut of the grid (the direction of the lower left corner to the upper left corner in degree). The coordinate reference system for projecting and computing distances must be specified. The length of the plot (distance, in m) and the cell size (bin, in m) must also be specified.

get_grid(coords=c(166.688126, -22.106773),
         azimut=306,
         proj="epsg:3163",
         distance=100,
         bin=10)

Function get_grid() produces two vector files (GPKG format). A polygon vector file for the grid and a point vector file for the cell corners.

corners <- sf::st_read("corners.gpkg", quiet=TRUE)
grid <- sf::st_read("grid.gpkg", quiet=TRUE)
# Filter the four corners
four_corners <- corners |>
  dplyr::filter(name %in% c("A0", "K0", "A10", "K10")) |>
  sf::st_transform("epsg:3163")
# Change coordinates so that labels do not overlap points
cc <- sf::st_coordinates(four_corners)
mm <- rbind(c(0, -7), c(7, 0), c(-7, 0), c(0, 7))
gg <- sf::st_multipoint(cc + mm) |>
  sf::st_geometry() |>
  sf::st_cast("POINT") |>
  sf::st_set_crs("epsg:3163")
sf::st_geometry(four_corners) <- gg
four_corners <- four_corners |>
  sf::st_transform("epsg:4326")

# Make plot
ggplot() +
  geom_sf(data=grid) +
  geom_sf_text(data=grid, aes(label=name), size=3) +
  geom_sf(data=corners) +
  geom_sf_text(data=four_corners, aes(label=name), color="blue", size=4)

figure.png

About

Make a forest plot grid from point coordinates and azimut

License:GNU General Public License v3.0


Languages

Language:R 100.0%