Discrete scale when using tidyterra::geom_spatraster #43
-
Hi, I just came across your package yesterday and it help me to drastically limit the code I used to plot a spatRaster from the Now I am not sure how to proceed with this, as |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hi @schonhose: On These functions are passed to Notice that the scales provided with So, in short, I think you can use Find here a reprex showing several examples: library(tidyterra)
library(terra)
#> terra 1.5.34
library(ggplot2)
f_volcano <- system.file("extdata/volcano2.tif", package = "tidyterra")
volcano2 <- rast(f_volcano)
volcano2
#> class : SpatRaster
#> dimensions : 174, 122, 1 (nrow, ncol, nlyr)
#> resolution : 5, 5 (x, y)
#> extent : 1756969, 1757579, 5917003, 5917873 (xmin, xmax, ymin, ymax)
#> coord. ref. : NZGD2000 / New Zealand Transverse Mercator 2000 (EPSG:2193)
#> source : volcano2.tif
#> name : elevation
#> min value : 76.26222
#> max value : 195.5542
# Use scales binned
# With scales from tidyterra package
ggplot() +
geom_spatraster(data = volcano2) +
scale_fill_wiki_b() # Control breaks, and use a scale from ggplot2
ggplot() +
geom_spatraster(data = volcano2) +
scale_fill_binned(breaks = c(90, 100, 130, 140, 180)) # Or create a categorial variable
volcano2 <- volcano2 %>%
mutate(cat = cut(elevation, breaks = c(70, 90, 100, 130, 140, 180, 200),
include.lowest=TRUE))
ggplot() +
geom_spatraster(data = volcano2, aes(fill = cat)) +
scale_fill_manual(values=hcl.colors(6, "Inferno")) Created on 2022-07-19 by the reprex package (v2.0.1) |
Beta Was this translation helpful? Give feedback.
-
Just to complete my previous answer, note that all those approachs plots the raster after it have been categorised (e.g. continuos values are internally discretized). You have another option that is to discretize only the legend, this is achieved with library(tidyterra)
#> terra 1.5.34
library(ggplot2)
f_volcano <- system.file("extdata/volcano2.tif", package = "tidyterra")
volcano2 <- rast(f_volcano)
# This modify the plot and the guide
ggplot() +
geom_spatraster(data = volcano2) +
scale_fill_viridis_b() # Use guide on _c to modify only the guide
ggplot() +
geom_spatraster(data = volcano2) +
scale_fill_viridis_c(guide = guide_colorsteps()) Created on 2022-07-19 by the reprex package (v2.0.1) |
Beta Was this translation helpful? Give feedback.
-
Thank you so much for your response! I found another way by reclassifying the raster using |
Beta Was this translation helpful? Give feedback.
Hi @schonhose:
On
ggplot2
system, you can create a discrete scale on continuos mapped values with thescale_*_binned()
functions https://ggplot2.tidyverse.org/reference/scale_colour_continuous.html. Some times this is abbreviated with a_b
suffix, like inscale_fill_viridis_b()
https://ggplot2.tidyverse.org/reference/scale_viridis.html.These functions are passed to
binned_scale
https://ggplot2.tidyverse.org/reference/binned_scale.html that supports abreaks
argument, so that may be what you are looking for.Notice that the scales provided with
tidyterra
have three versions:*_c
for continous values,*_b
for binned values and*_d
for discrete values.So, in short, I think you can use
*_b/…