Could convert a square raster to a hexagon when plotting with geom_spatraster #142
-
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi, This is more advanced but it can be done with tidyterra. The trick is, instead of using https://dieghernan.github.io/tidyterra/reference/fortify.Spat.html A working example A. Mock the datalibrary(geodata)
library(tidyterra)
library(terra)
library(ggplot2)
library(hexbin)
# Get global land
global_land <- geodata::gadm(country = "ITA", path = tempdir(), level = 0) %>%
aggregate() %>%
project("+proj=robin")
# Mock ra
ra <- geodata::worldclim_country(
country = "ITA", res = 10, var = "prec",
path = tempdir()
) %>%
select(Intertidal_marine_forests = ITA_wc2.1_30s_prec_6) %>%
project("+proj=robin") %>%
# Downsample for demo
spatSample(size = 1000, method = "regular", as.raster = TRUE) %>%
# Get ra touching boundaries only
terra::mask(as.lines(global_land))
First, ensure that global_land <- terra::project(global_land, terra::crs(ra)) B. Create the plot# Original plot
ggplot() +
geom_spatvector(data = global_land, fill = "grey88", size = 0.1, color = NA) +
geom_spatraster(
data = ra, aes(fill = Intertidal_marine_forests), maxcell = 3 * 10^6,
show.legend = FALSE
) +
scale_fill_hypso_c() +
theme_void() +
theme(legend.position = c(0.1, 0.07)) +
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.background = element_blank()
) +
labs(title = "Original") The alternative. Please note that you may need to fiddle with # Alt: fortifying and using geom_hex
ggplot(ra, maxcell = 3 * 10^6) + # See here
geom_spatvector(data = global_land, fill = "grey88", size = 0.1, color = NA) +
# And here
stat_summary_hex(aes(x = x, y = y, z = Intertidal_marine_forests),
show.legend = FALSE,
# IMPORTANT: Play with this parameter
bins = 40
) +
scale_fill_hypso_c(na.value = "transparent") +
theme_void() +
theme(legend.position = c(0.1, 0.07)) +
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.background = element_blank()
) +
labs(title = "Using fortify and stat_summary_hex")
#> Warning: Removed 910 rows containing non-finite outside the scale range
#> (`stat_summary_hex()`). Created on 2024-04-02 with reprex v2.1.0 |
Beta Was this translation helpful? Give feedback.
-
Another approach is to create an hexagonal grid of # Hex grid with sf ----
library(sf)
library(units)
# Avg size of the cells on the initial grid
target_area <- cellSize(ra, unit = "km") %>%
pull() %>%
mean() %>%
as_units("km^2")
diam_hex <- sqrt(2 * target_area / sqrt(3))
# Create hexagonal grid
ra_grid <- sf::st_make_grid(global_land, cellsize = diam_hex, square = FALSE) %>%
terra::vect()
ra_grid$Intertidal_marine_forests <- terra::extract(ra, ra_grid, fun = "mean") %>%
pull(Intertidal_marine_forests)
# Clean hex grid
ra_grid <- drop_na(ra_grid)
# Alt2
ggplot() +
geom_spatvector(data = global_land, fill = "grey88", size = 0.1, color = NA) +
geom_spatvector(
data = ra_grid, aes(fill = Intertidal_marine_forests), color = NA,
show.legend = FALSE
) +
scale_fill_hypso_c(na.value = "transparent") +
theme_void() +
theme(legend.position = c(0.1, 0.07)) +
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.background = element_blank()
) +
labs(title = "With sf hex conversion") Created on 2024-04-02 with reprex v2.1.0 |
Beta Was this translation helpful? Give feedback.
Hi,
This is more advanced but it can be done with tidyterra. The trick is, instead of using
geom_spatraster()
, useggplot() + stat_summary_hex()
. In fact you would be fortifying theSpatRaster
and applying a newstat
, see some examples here:https://dieghernan.github.io/tidyterra/reference/fortify.Spat.html
A working example
A. Mock the data