Learn R Programming

tidyterra (version 0.7.2)

autoplot.Spat: Create a complete ggplot for Spat* objects

Description

autoplot() uses ggplot2 to draw plots as the ones produced by terra::plot()/terra::plotRGB() in a single command.

Usage

# S3 method for SpatRaster
autoplot(
  object,
  ...,
  rgb = NULL,
  use_coltab = NULL,
  facets = NULL,
  nrow = NULL,
  ncol = 2
)

# S3 method for SpatVector autoplot(object, ...)

Value

A ggplot2 layer

Arguments

object

A SpatRaster created with terra::rast() or a SpatVector created with terra::vect().

...

other arguments passed to geom_spatraster(), geom_spatraster_rgb() or geom_spatvector().

rgb

Logical. Should be plotted as a RGB image? If NULL (the default) autoplot.SpatRaster() would try to guess.

use_coltab

Logical. Should be plotted with the corresponding terra::coltab()? If NULL (the default) autoplot.SpatRaster() would try to guess. See also scale_fill_coltab().

facets

Logical. Should facets be displayed? If NULL (the default) autoplot.SpatRaster() would try to guess.

nrow, ncol

Number of rows and columns on the facet.

Methods

Implementation of the generic ggplot2::autoplot() function.

SpatRaster

Uses geom_spatraster() or geom_spatraster_rgb().

SpatVector

Uses geom_spatvector(). Labels can be placed with geom_spatvector_text() or geom_spatvector_label().

Details

Implementation of ggplot2::autoplot() method.

See Also

ggplot2::autoplot()

Other ggplot2 utils: fortify.Spat, geom_spat_contour, geom_spatraster(), geom_spatraster_rgb(), ggspatvector, stat_spat_coordinates()

Other ggplot2 methods: fortify.Spat

Examples

Run this code
# \donttest{

file_path <- system.file("extdata/cyl_temp.tif", package = "tidyterra")

library(terra)
temp <- rast(file_path)

library(ggplot2)
autoplot(temp)


# With a tile

tile <- system.file("extdata/cyl_tile.tif", package = "tidyterra") %>%
  rast()

autoplot(tile)

# With coltabs

ctab <- system.file("extdata/cyl_era.tif", package = "tidyterra") %>%
  rast()

autoplot(ctab)

#  With vectors
v <- vect(system.file("extdata/cyl.gpkg", package = "tidyterra"))
autoplot(v)

v %>% autoplot(aes(fill = cpro)) +
  geom_spatvector_text(aes(label = iso2)) +
  coord_sf(crs = 25829)
# }

Run the code above in your browser using DataLab