Skip to contents

This geom plots SpatRaster objects (see terra::rast()) as RGB images. The layers are combined so they represent the red, green and blue channels. Use geom_spatraster_rgb(data = tile, r = 1, g = 2, b = 3) to map layers 1, 2 and 3 to those channels. The package includes extdata/cyl_tile.tif as an example RGB raster.

For plotting SpatRaster objects by layer values use geom_spatraster().

The underlying implementation is based on ggplot2::geom_raster().

Usage

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
)

Source

Based on the layer_spatial() implementation in the ggspatial package. Thanks to Dewey Dunnington and to ggspatial contributors.

Arguments

mapping

Optional layer-specific aesthetic mappings created with ggplot2::aes(). Select RGB channels with r, g and b.

data

A SpatRaster object.

interpolate

Logical. If TRUE, the default, interpolate linearly when drawing the raster.

r, g, b

Integer giving the layer number in data used for the red (r), green (g) and blue (b) channel.

alpha

The alpha transparency, a number in [0,1], see argument alpha in hsv.

maxcell

Positive integer or Inf. Target number of raster cells for regular sampling. Sampling starts when the raster has more than 1.1 times maxcell cells, and the sampled grid can still exceed the target. Inf skips this sampling step. Reprojection for plotting can still change the grid. The input raster is not modified.

max_col_value

Number giving the upper bound of the color value range. When this is 255 (the default), the result is computed most efficiently. See grDevices::rgb().

...

Other arguments passed on to layer()'s params argument. These arguments broadly fall into one of 4 categories below. Notably, further arguments to the position argument, or aesthetics that are required can not be passed through .... Unknown arguments that are not part of the 4 categories below are ignored.

  • Static aesthetics that are not mapped to a scale, but are at a fixed value and apply to the layer as a whole. For example, colour = "red" or linewidth = 3. The geom's documentation has an Aesthetics section that lists the available options. The 'required' aesthetics cannot be passed on to the params. Please note that while passing unmapped aesthetics as vectors is technically possible, the order and required length is not guaranteed to be parallel to the input data.

  • When constructing a layer using a stat_*() function, the ... argument can be used to pass on parameters to the geom part of the layer. An example of this is stat_density(geom = "area", outline.type = "both"). The geom's documentation lists which parameters it can accept.

  • Inversely, when constructing a layer using a geom_*() function, the ... argument can be used to pass on parameters to the stat part of the layer. An example of this is geom_area(stat = "density", adjust = 0.5). The stat's documentation lists which parameters it can accept.

  • The key_glyph argument of layer() may also be passed on through .... This can be one of the functions described as key glyphs, to change the display of the layer in the legend.

stretch

character. Option to stretch the values to increase contrast: "lin" (linear) or "hist" (histogram). The linear stretch uses stretch with arguments minq=0.02 and maxq=0.98

zlim

numeric vector of length 2. Range of values to plot (optional). If this is set, and stretch="lin" is used, then the values are stretched within the range of zlim. This allows creating consistent coloring between SpatRasters with different cell-value ranges, even when stretching the colors for improved contrast

mask_projection

Logical, defaults to FALSE. If TRUE, mask out areas outside the input extent. For example, to avoid data wrapping around the dateline in equal-area projections. This argument is passed to terra::project() when reprojecting the SpatRaster.

Value

A ggplot2 layer or, for a raster with a CRS, a list that also contains an auxiliary spatial layer.

terra equivalent

terra::plotRGB().

Aesthetics

RGB values come from the three selected raster layers. No aesthetic mapping is required to draw them.

Coordinates

When the SpatRaster does not have a CRS, that is, terra::crs(rast) == "", the geom does not make any assumption about the scales.

On SpatRaster objects that have a CRS, the geom uses ggplot2::coord_sf() to adjust the scales. This means that the SpatRaster may be reprojected.

Examples

library(ggplot2)
library(tidyterra)

rgb <- terra::rast(nrows = 2, ncols = 2, nlyrs = 3, vals = 1:12)
ggplot() +
  geom_spatraster_rgb(data = rgb, r = 1, g = 2, b = 3)


# \donttest{

# Tile of Castile 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)
#> Reading layer `cyl' from data source 
#>   `/home/runner/work/_temp/Library/tidyterra/extdata/cyl.gpkg' 
#>   using driver `GPKG'
#> Simple feature collection with 9 features and 3 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 2892687 ymin: 2017622 xmax: 3341372 ymax: 2361600
#> Projected CRS: ETRS89-extended / LAEA Europe

ggplot(cyl_sf) +
  geom_spatraster_rgb(data = tile) +
  geom_sf(aes(fill = iso2)) +
  coord_sf(crs = 3857) +
  scale_fill_viridis_d(alpha = 0.7)

# }