I am trying to use scales::pretty_breaks
with geom_spatraster
like
library(terra)
library(ggplot2)
library(tidyterra)
r1 <- rast(system.file("ex/elev.tif", package="terra"))
r2 <- r1
r <- c(r1, r2)
names(r) <- c("R1", "R2")
p <- ggplot() +
geom_spatraster(data = r) +
facet_wrap(~lyr, ncol = 2)+
scale_fill_viridis_c(na.value = "transparent", name = "Elevation",
option = "viridis", direction = -1) +
theme(text=element_text(family = "serif", size=15),
axis.text.x = element_text(colour="black"),
axis.text.y = element_text(colour="black", angle = 90, hjust = 0.5))
p + scale_x_continuous(breaks = scales::pretty_breaks(n = 3)) +
scale_y_continuous(breaks = scales::pretty_breaks(n = 3))
which returns the following error
Error in min(lon) : invalid 'type' (closure) of argument
How can I solve this error?
This is an issue on
ggplot2
side (see https://github.com/tidyverse/ggplot2/issues/4622, https://github.com/tidyverse/ggplot2/pull/5442 and How to specify exacts axis breaks in a map using sf and ggplot2?).In the meantime tidyterra FAQs suggests an approach that I adapted a bit for your case:
Created on 2023-11-07 with reprex v2.0.2