Skip to content

Could convert a square raster to a hexagon when plotting with geom_spatraster #142

Answered by dieghernan
zzzqiii asked this question in Q&A
Discussion options

You must be logged in to vote

Hi,

This is more advanced but it can be done with tidyterra. The trick is, instead of using geom_spatraster(), use ggplot() + stat_summary_hex(). In fact you would be fortifying the SpatRaster and applying a new stat, see some examples here:

https://dieghernan.github.io/tidyterra/reference/fortify.Spat.html

A working example

A. Mock the data

library(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(Inter…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by zzzqiii
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
good first issue Good for newcomers 📊 ggplot2 Issues related with the implementation of ggplot2 functionalities ❔ q&a Questions on the usage of tidyterra 🏼 spatraster Related with SpatRaster
2 participants
Converted from issue

This discussion was converted from issue #141 on April 02, 2024 07:15.