Learn R Programming

tidyterra (version 0.1.0)

geom_spat_contour: Plot SpatRaster contours

Description

These geoms create contours of SpatRaster objects. To specify a valid surface, you should specify the layer on aes(z = layer_name), otherwise all the layers would be consider for creating contours. See also Facets section.

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

Usage

geom_spatraster_contour(
  mapping = NULL,
  data,
  ...,
  maxcell = 5e+05,
  bins = NULL,
  binwidth = NULL,
  breaks = NULL,
  na.rm = TRUE,
  show.legend = NA,
  inherit.aes = TRUE
)

geom_spatraster_contour_filled( mapping = NULL, data, ..., maxcell = 5e+05, bins = NULL, binwidth = NULL, breaks = NULL, na.rm = TRUE, show.legend = NA, inherit.aes = TRUE )

Arguments

mapping

Set of aesthetic mappings created by aes() or aes_(). If specified and inherit.aes = TRUE (the default), it is combined with the default mapping at the top level of the plot. You must supply mapping if there is no plot mapping.

data

A SpatRaster object.

...

Other arguments passed on to layer(). These are often aesthetics, used to set an aesthetic to a fixed value, like colour = "red" or size = 3. They may also be parameters to the paired geom/stat.

maxcell

positive integer. Maximum number of cells to use for the plot.

bins

Number of contour bins. Overridden by binwidth.

binwidth

The width of the contour bins. Overridden by breaks.

breaks

One of:

  • Numeric vector to set the contour breaks

  • A function that takes the range of the data and binwidth as input and returns breaks as output. A function can be created from a formula (e.g. ~ fullseq(.x, .y)).

Overrides binwidth and bins. By default, this is a vector of length ten with pretty() breaks.

na.rm

If FALSE, the default, missing values are removed with a warning. If TRUE, missing values are silently removed.

show.legend

logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes. It can also be a named logical vector to finely select the aesthetics to display.

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. borders().

Value

A ggplot2 layer

terra equivalent

terra::contour()

Aesthetics

geom_spatraster_contour() understands the following aesthetics:

  • z

  • alpha

  • colour

  • linetype

  • size

Additionally, geom_spatraster_contour_filled() understands also the following aesthetics, as well as the ones listed above:

  • fill

Check ggplot2::geom_contour() for more info.

Computed variables

This geom computes internally some variables that are available for use as aesthetics, using (for example) aes(color = after_stat(<computed>)) (see ggplot2::after_stat()).

level

Height of contour. For contour lines, this is numeric vector that represents bin boundaries. For contour bands, this is an ordered factor that represents bin ranges.

nlevel

Height of contour, scaled to maximum of 1.

lyr

Name of the layer.

level_low, level_high, level_mid

(contour bands only) Lower and upper bin boundaries for each band, as well the mid point between the boundaries.

Coords

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.

Facets

You can use facet_wrap(~lyr) for creating a faceted plot by each layer of the SpatRaster object. See ggplot2::facet_wrap() for details.

See Also

ggplot2::geom_contour()

Other ggplot2 utils: autoplot(), geom_spatraster_rgb(), geom_spatraster(), ggspatvector, scale_fill_terrain

Examples

Run this code
# NOT RUN {
library(terra)

# Raster
f <- system.file("extdata/cyl_temp.tif", package = "tidyterra")
r <- rast(f)

library(ggplot2)

ggplot() +
  geom_spatraster_contour(data = r) +
  facet_wrap(~lyr)

# Select one layer

ggplot() +
  geom_spatraster_contour(data = r, aes(z = tavg_04))
# With aes on computed variable

ggplot() +
  geom_spatraster_contour(
    data = r, aes(color = after_stat(level)),
    binwidth = 1,
    size = 0.4
  ) +
  facet_wrap(~lyr) +
  scale_color_gradientn(
    colours = hcl.colors(20, "Inferno"),
    guide = guide_coloursteps()
  ) +
  theme_minimal()

# Filled with breaks
ggplot() +
  geom_spatraster_contour_filled(data = r, breaks = seq(5, 20, 2.5)) +
  facet_wrap(~lyr)

# Both lines and contours aligned with breaks
ggplot() +
  geom_spatraster_contour_filled(
    data = r, breaks = seq(5, 20, 2.5),
    alpha = .7
  ) +
  geom_spatraster_contour(
    data = r, breaks = seq(5, 20, 2.5),
    color = "black"
  ) +
  facet_wrap(~lyr)
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab