View source: R/geom_spatraster_rgb.R
geom_spatraster_rgb | R Documentation |
SpatRaster
objects as imagesThis geom is used to visualise SpatRaster
objects (see terra::rast()
) as
RGB images. The layers are combined such that they represent the red,
green and blue channel.
For plotting SpatRaster
objects by layer values use geom_spatraster()
.
The underlying implementation is based on ggplot2::geom_raster()
.
geom_spatraster_rgb(
mapping = aes(),
data,
interpolate = TRUE,
r = 1,
g = 2,
b = 3,
alpha = 1,
maxcell = 5e+05,
max_col_value = 255,
...,
stretch = NULL,
zlim = NULL,
mask_projection = FALSE
)
mapping |
Ignored. |
data |
A |
interpolate |
If |
r , g , b |
Integer representing the number of layer of |
alpha |
The alpha transparency, a number in [0,1], see argument alpha in
|
maxcell |
positive integer. Maximum number of cells to use for the plot. |
max_col_value |
Number giving the maximum of the color values range.
When this is |
... |
Other arguments passed on to
|
stretch |
character. Option to stretch the values to increase contrast: "lin" (linear) or "hist" (histogram). The linear stretch uses |
zlim |
numeric vector of length 2. Range of values to plot (optional). If this is set, and |
mask_projection |
logical, defaults to |
A ggplot2 layer
terra::plotRGB()
No aes()
is required. In fact, aes()
will be ignored.
When the SpatRaster
does not present a crs (i.e.,
terra::crs(rast) == ""
) the geom does not make any assumption on the
scales.
On SpatRaster
that have a crs, the geom uses ggplot2::coord_sf()
to
adjust the scales. That means that also the
SpatRaster
may be reprojected.
Based on the layer_spatial()
implementation on ggspatial package.
Thanks to Dewey Dunnington and ggspatial contributors.
ggplot2::geom_raster()
, ggplot2::coord_sf()
, grDevices::rgb()
.
You can get also RGB tiles from the maptiles package,
see maptiles::get_tiles()
.
Other ggplot2 utils:
autoplot.Spat
,
fortify.Spat
,
geom_spat_contour
,
geom_spatraster()
,
ggspatvector
,
stat_spat_coordinates()
# Tile of Castille and Leon (Spain) from OpenStreetMap
file_path <- system.file("extdata/cyl_tile.tif", package = "tidyterra")
library(terra)
tile <- rast(file_path)
library(ggplot2)
ggplot() +
geom_spatraster_rgb(data = tile) +
# You can use coord_sf
coord_sf(crs = 3035)
# Combine with sf objects
vect_path <- system.file("extdata/cyl.gpkg", package = "tidyterra")
cyl_sf <- sf::st_read(vect_path)
ggplot(cyl_sf) +
geom_spatraster_rgb(data = tile) +
geom_sf(aes(fill = iso2)) +
coord_sf(crs = 3857) +
scale_fill_viridis_d(alpha = 0.7)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.