Learn R Programming

tidyterra

The goal of tidyterra is to provide methods from tidyverse packages for SpatRaster and SpatVector objects created with terra. It also provides ggplot2 geoms and scales for plotting those objects.

Please cite tidyterra as:

Hernangómez, D. (2023). Using the tidyverse with terra objects: the tidyterra package. Journal of Open Source Software, 8(91), 5751, https://doi.org/10.21105/joss.05751.

A BibTeX entry for LaTeX users is:

@article{Hernangómez2023,
  doi = {10.21105/joss.05751},
  url = {https://doi.org/10.21105/joss.05751},
  year = {2023},
  publisher = {The Open Journal},
  volume = {8},
  number = {91},
  pages = {5751},
  author = {Diego Hernangómez},
  title = {Using the {tidyverse} with {terra} objects: the {tidyterra} package},
  journal = {Journal of Open Source Software}
}

Overview

The full manual for the latest release of tidyterra on CRAN is online: https://dieghernan.github.io/tidyterra/

Methods implemented in tidyterra work differently depending on the type of Spat* object:

  • SpatVector: Methods are implemented using terra::as.data.frame() coercion. Rows correspond to geometries and columns correspond to attributes of each geometry.

  • SpatRaster: Methods can be applied to layers or cells. tidyterra’s overall approach is to treat the layers as columns of a tibble and the cells as rows. For example, select(a_spatraster, 1) selects the first layer of a SpatRaster object.

Implemented methods return the same type of object as the input, unless the method is expected to return another type of object. For example, as_tibble() returns a tibble.

Current methods and functions provided by tidyterra are:

tidyverse methodSpatVectorSpatRaster
tibble::as_tibble()✔️✔️
dplyr::select()✔️✔️ Select layers
dplyr::mutate()✔️✔️ Create or modify layers
dplyr::transmute()✔️✔️
dplyr::filter()✔️✔️ Modify cell values and optionally remove outer cells.
dplyr::filter_out()✔️
dplyr::slice()✔️✔️ Additional methods for slicing by row and column.
dplyr::pull()✔️✔️
dplyr::rename()✔️✔️
dplyr::relocate()✔️✔️
dplyr::distinct()✔️
dplyr::arrange()✔️
dplyr::glimpse()✔️✔️
dplyr::inner_join() family✔️
dplyr::nest_join()✔️
dplyr::cross_join()✔️
dplyr::summarise()✔️
dplyr::reframe()✔️
dplyr::group_by() family✔️
dplyr::rowwise()✔️
dplyr::count(), tally()✔️
dplyr::add_count()✔️
dplyr::rows_*()✔️
dplyr::bind_cols() / dplyr::bind_rows()✔️ as bind_spat_cols() / bind_spat_rows()
tidyr::drop_na()✔️✔️ Remove cell values with NA on any layer and outer cells with NA.
tidyr::complete()✔️
tidyr::expand()✔️
tidyr::replace_na()✔️✔️
tidyr::fill()✔️
tidyr::nest()✔️
tidyr::pivot_longer()✔️
tidyr::pivot_wider()✔️
tidyr::uncount()✔️
tidyr::unite()✔️✔️ Create a categorical layer.
ggplot2::autoplot()✔️✔️
ggplot2::fortify()✔️ to sf through sf::st_as_sf()To a tibble with coordinates.
ggplot2::geom_*()✔️ geom_spatvector()✔️ geom_spatraster() and geom_spatraster_rgb().
generics::tidy()✔️✔️
generics::glance()✔️✔️
generics::required_pkgs()✔️✔️

[!IMPORTANT]

A note on performance

tidyterra is a user-friendly wrapper around terra that provides tidyverse-style methods and verbs. This approach has a performance cost.

If you frequently use terra or work with large SpatRaster objects, terra is usually much faster. Whenever possible, each tidyterra function refers to its equivalent in terra.

As a rule of thumb, if your raster has fewer than 10,000,000 data slots, for example terra::ncell(your_rast) * terra::nlyr(your_rast) < 1e7, tidyterra is a good fit.

When plotting rasters, resampling is performed automatically (as terra::plot() does, see the help page). You can adjust this with the maxcell argument.

Installation

Install tidyterra from CRAN:

install.packages("tidyterra")

Check the documentation for the development version at https://dieghernan.github.io/tidyterra/dev/

You can install the development version of tidyterra with:

# install.packages("pak")
pak::pak("dieghernan/tidyterra")

Alternatively, you can install tidyterra using r-universe:

# Enable this universe
install.packages(
  "tidyterra",
  repos = c(
    "https://dieghernan.r-universe.dev",
    "https://cloud.r-project.org"
  )
)

Examples

SpatRaster objects

This basic example shows how to manipulate and plot SpatRaster objects:

library(tidyterra)
library(terra)

# Temperatures
rastertemp <- rast(system.file("extdata/cyl_temp.tif", package = "tidyterra"))

rastertemp
#> class       : SpatRaster
#> size        : 87, 118, 3  (nrow, ncol, nlyr)
#> resolution  : 3881.255, 3881.255  (x, y)
#> extent      : -612335.4, -154347.3, 4283018, 4620687  (xmin, xmax, ymin, ymax)
#> coord. ref. : World_Robinson (ESRI:54030)
#> source      : cyl_temp.tif
#> names       :   tavg_04,   tavg_05,   tavg_06
#> min values  :  1.885463,  5.817587, 10.463377
#> max values  : 13.283829, 16.740898, 21.113781

# Rename
rastertemp <- rastertemp |>
  rename(April = tavg_04, May = tavg_05, June = tavg_06)

# Facet all layers
library(ggplot2)

ggplot() +
  geom_spatraster(data = rastertemp) +
  facet_wrap(~lyr, ncol = 2) +
  scale_fill_whitebox_c(
    palette = "muted",
    labels = scales::label_number(suffix = "º"),
    n.breaks = 12,
    guide = guide_legend(reverse = TRUE)
  ) +
  labs(
    fill = "",
    title = "Average temperature in Castile and Leon (Spain)",
    subtitle = "Months of April, May and June"
  )
# Create the difference between two months.
variation <- rastertemp |>
  mutate(diff = June - May) |>
  select(variation = diff)

# Add a SpatVector overlay.
prov <- vect(system.file("extdata/cyl.gpkg", package = "tidyterra"))

ggplot(prov) +
  geom_spatraster(data = variation) +
  geom_spatvector(fill = NA) +
  scale_fill_whitebox_c(
    palette = "deep",
    direction = -1,
    labels = scales::label_number(suffix = "º"),
    n.breaks = 5
  ) +
  theme_minimal() +
  coord_sf(crs = 25830) +
  labs(
    fill = "Variation",
    title = "Temperature variation in Castile and Leon (Spain)",
    subtitle = "Average temperatures: June vs. May"
  )

tidyterra also provides a geom for plotting RGB SpatRaster objects, such as map tiles, with ggplot2:

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

ggplot(prov) +
  geom_spatraster_rgb(data = rgb_tile) +
  geom_spatvector(fill = NA) +
  theme_light() +
  # Change the CRS and datum (useful for relabeling graticules).
  coord_sf(crs = 3857, datum = 3857)

tidyterra provides ggplot2 scales for plotting maps with hypsometric tints:

asia <- rast(system.file("extdata/asia.tif", package = "tidyterra"))

ggplot() +
  geom_spatraster(data = asia) +
  scale_fill_hypso_tint_c(
    palette = "gmt_globe",
    labels = scales::label_number(),
    # Further refinements
    breaks = c(-10000, -5000, 0, 2000, 5000, 8000),
    guide = guide_colorbar(reverse = TRUE)
  ) +
  labs(
    fill = "elevation (m)",
    title = "Hypsometric map of Asia"
  ) +
  theme(
    legend.position = "bottom",
    legend.title.position = "top",
    legend.key.width = rel(3),
    legend.ticks = element_line(colour = "black", linewidth = 0.3),
    legend.direction = "horizontal"
  )

SpatVector objects

This basic example shows how to manipulate and plot SpatVector objects:

vect(system.file("ex/lux.shp", package = "terra")) |>
  mutate(pop_dens = POP / AREA) |>
  glimpse() |>
  autoplot(aes(fill = pop_dens)) +
  scale_fill_whitebox_c(palette = "pi_y_g") +
  labs(
    fill = "population per km2",
    title = "Population density of Luxembourg",
    subtitle = "By canton"
  )
#> #  A SpatVector 12 x 7
#> #  Geometry type: Polygons
#> #  Geodetic CRS: lon/lat WGS 84 (EPSG:4326)
#> #  Extent (x / y): ([5° 44' 38.9" E / 6° 31' 41.71" E] , [49° 26' 52.11" N / 50° 10' 53.84" N])
#> 
#> $ ID_1     <dbl> 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3
#> $ NAME_1   <chr> "Diekirch", "Diekirch", "Diekirch", "Diekirch", "Diekirch", "…
#> $ ID_2     <dbl> 1, 2, 3, 4, 5, 6, 7, 12, 8, 9, 10, 11
#> $ NAME_2   <chr> "Clervaux", "Diekirch", "Redange", "Vianden", "Wiltz", "Echte…
#> $ AREA     <dbl> 312, 218, 259, 76, 263, 188, 129, 210, 185, 251, 237, 233
#> $ POP      <dbl> 18081, 32543, 18664, 5163, 16735, 18899, 22366, 29828, 48187,…
#> $ pop_dens <dbl> 57.95192, 149.27982, 72.06178, 67.93421, 63.63118, 100.52660,…

Feedback

Please leave your feedback or open an issue on https://github.com/dieghernan/tidyterra/issues.

Need help?

Check the FAQs or open a new issue.

You can also ask on Stack Overflow using the tag [tidyterra].

Acknowledgments

tidyterra’s ggplot2 geoms are based on the ggspatial implementation by Dewey Dunnington and ggspatial contributors.

Copy Link

Version

Install

install.packages('tidyterra')

Monthly Downloads

14,489

Version

1.3.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Diego Hernang<c3><b3>mez

Last Published

August 24th, 2026

Functions in tidyterra (1.3.0)

cross_blended_hypsometric_tints_db

Cross-blended hypsometric tints
filter-joins.SpatVector

Filtering joins for SpatVector objects
complete.SpatVector

Complete missing combinations in a SpatVector
expand.SpatVector

Expand SpatVector attribute combinations
cross_join.SpatVector

Cross joins for SpatVector objects
drop_na.Spat

Drop attributes of Spat* objects containing missing values
distinct.SpatVector

Keep distinct/unique rows and geometries of SpatVector objects
filter.Spat

Subset cells/geometries of Spat* objects
count.SpatVector

Count the observations in each SpatVector group
fill.SpatVector

Fill in missing values with previous or next value on a SpatVector
ggspatvector

Plot SpatVector objects
group_by.SpatVector

Group a SpatVector by one or more variables
group_data.SpatVector

Grouping metadata for SpatVector objects
geom_spat_contour

Plot SpatRaster contours
grass_db

GRASS color tables
glimpse.Spat

Preview Spat* objects
glance.Spat

Glance at an Spat* object
geom_spatraster_rgb

Plot SpatRaster objects as images
fortify.Spat

Fortify Spat* objects
geom_spatraster

Plot SpatRaster objects
group_nest.SpatVector

Nest grouped SpatVector rows
is_regular_grid

Check whether x and y positions form a regular grid
nest.SpatVector

Nest SpatVector rows
mutate-joins.SpatVector

Mutating joins for SpatVector objects
hypsometric_tints_db

Hypsometric palettes database
group_map.SpatVector

Apply a function to each SpatVector group
group_split.SpatVector

Split SpatVector by groups
is_grouped_spatvector

A grouped SpatVector
group_trim.SpatVector

Trim grouping structure
mutate.Spat

Create, modify and delete cell values/layers/attributes of Spat* objects
pivot_longer.SpatVector

Pivot SpatVector from wide to long
nest_join.SpatVector

Nest join SpatVector objects
%>%

Pipe operator
pull_crs

Extract CRS in WKT format
relocate.Spat

Change layer/attribute order
pull.Spat

Extract a single layer/attribute
pivot_wider.SpatVector

Pivot SpatVector from long to wide
reexports

Objects exported from other packages
reframe.SpatVector

Reframe each group of a SpatVector
princess_db

Princess palettes database
rows.SpatVector

Row operations for SpatVector objects
replace_na.Spat

Replace NAs with specified values
rename.Spat

Rename layers/attributes
required_pkgs.Spat

Determine packages required by Spat* objects
scale_hypso

Gradient scales for hypsometric and bathymetric tints
scale_cross_blended

Cross-blended hypsometric tint scales
scale_grass

GRASS scales
scale_princess

Gradient scales from princess color schemes
rowwise.SpatVector

Group SpatVector objects by rows
scale_coltab

Discrete scales based on SpatRaster color tables
summarise.SpatVector

Summarise each group of a SpatVector down to one geometry
tidyterra-package

tidyterra: 'tidyverse' Methods and 'ggplot2' Helpers for 'terra' Objects
tidy.Spat

Tidy Spat* objects for plotting
scale_whitebox

Gradient scales from WhiteboxTools color schemes
slice.Spat

Subset cells/rows/columns/geometries using their positions
scale_wiki

Gradient scales from Wikipedia color schemes
scale_terrain

Terrain color scales from grDevices
select.Spat

Subset layers/attributes of Spat* objects
transmute.Spat

Create, modify and delete cell values/layers/attributes of Spat* objects
stat_spat_coordinates

Extract coordinates from SpatVector objects
volcano2

Updated topographic information on Auckland's Maungawhau volcano
uncount.SpatVector

Duplicate SpatVector rows
unite.Spat

Unite Spat* layers or attributes
as_spatvector

Coerce objects to SpatVector
bind_rows.SpatVector

Bind multiple SpatVector, sf, sfc and data frame objects by row
bind_cols.SpatVector

Bind multiple SpatVector, sf and data frame objects by column
as_spatraster

Coerce a data frame to SpatRaster
as_coordinates

Get cell number, row and column from a SpatRaster
as_sf

as_tibble.Spat

Coerce SpatRaster and SpatVector objects to tibbles
arrange.SpatVector

Order a SpatVector using column values
autoplot.Spat

Create a complete ggplot for Spat* objects
compare_spatrasters

Compare attributes of two SpatRaster objects