Datum and axis breaks #45
-
From an email of Ken Nussear @knussear:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
My take on this: Modifying the labels to avoid conversion to lon/latThis can ve avoided by using library(tidyterra)
library(terra)
library(ggplot2)
f_volcano <- system.file("extdata/volcano2.tif", package = "tidyterra")
volcano2 <- rast(f_volcano)
p <- ggplot() +
geom_spatraster(data = volcano2) +
geom_spatraster_contour(data = volcano2, breaks = seq(80, 200, 5)) +
scale_fill_whitebox_c() +
labs(fill = "elevation")
p # Keep datum
p +
coord_sf(datum = terra::crs(volcano2)) Modifying the number of axis ticksThis is interesting. It depends on what datum is in use by ggplot2. If no datum is provided, ggplot defaults to WGS84, so my approach is:
# Change axis ticks
# 2 situations: Datum by default WGS84 (lon,lat)
ext_lonlat <- as.double(ext(project(volcano2, "EPSG:4326"))@ptr$vector)
x_breaks_lonlat <- round(seq(ext_lonlat[1], ext_lonlat[2], length.out = 3), 3)
p +
scale_x_continuous(breaks = x_breaks_lonlat) If we want the axis to be in the original datum of the SpatRaster, the approach is similar but the projection step is not needed: # Axis on original datum
ext <- as.double(terra::ext(volcano2)@ptr$vector)[1:2]
x_breaks <- seq(ext[1], ext[2], length.out = 3)
p +
coord_sf(datum = terra::crs(volcano2)) +
scale_x_continuous(breaks = x_breaks) Created on 2022-09-21 by the reprex package (v2.0.1) |
Beta Was this translation helpful? Give feedback.
My take on this:
Modifying the labels to avoid conversion to lon/lat
This can ve avoided by using
coord_sf(datum)
option. Just use the original datum defined on the CRS of the SpatRaster filecoord_sf(datum = terra::crs(volcano2))
:Modifying the number of axis ticks
This is interesting. It depends on …