### Geometries SpatVector example (points) Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glance-Spat.md Demonstrates the output of glance on a SpatVector with point geometry. ```r glance(p) # A tibble: 1 x 10 geometry nrow ncol xmin xmax ymin ymax source crs crs_units 1 points 1492 3 2892687. 3341372. 2017622. 2.36e6 NA ETRS~ meter ``` -------------------------------- ### CRS example with raster Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glance-Spat.md Demonstrates the output of glance on a SpatRaster with a defined CRS. ```r glance(r1) # A tibble: 1 x 16 nrow ncol nlyr ncell xres yres xmin xmax ymin ymax crs crs_units 1 4 3 1 12 1 1 0 3 0 4 CRS: No~ NA # i 4 more variables: source , has_rgb , has_colors , # has_time ``` -------------------------------- ### Geometries SpatVector example (lines) Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glance-Spat.md Demonstrates the output of glance on a SpatVector with line geometry. ```r glance(l) # A tibble: 1 x 10 geometry nrow ncol xmin xmax ymin ymax source crs crs_units 1 lines 9 3 2892687. 3341372. 2017622. 2.36e6 cyl.g~ ETRS~ meter ``` -------------------------------- ### Glance SpatRaster Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glance-Spat.md Example of using glance() on a SpatRaster object. ```R glance(r) ``` -------------------------------- ### Installation from CRAN Source: https://github.com/dieghernan/tidyterra/blob/main/README.md Standard installation of the tidyterra package from CRAN. ```r install.packages("tidyterra") ``` -------------------------------- ### Glance SpatVectors example Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glance-Spat.md Demonstrates the output of glance on a SpatVector with a defined CRS and geometry. ```r glance(v) # A tibble: 1 x 10 geometry nrow ncol xmin xmax ymin ymax source crs crs_units 1 polygons 9 3 2892687. 3341372. 2017622. 2.36e6 cyl.g~ ETRS~ meter ``` -------------------------------- ### Installation of development version using pak Source: https://github.com/dieghernan/tidyterra/blob/main/README.md Installs the development version of tidyterra using the pak package manager. ```r # install.packages("pak") pak::pak("dieghernan/tidyterra") ``` -------------------------------- ### Installation using r-universe Source: https://github.com/dieghernan/tidyterra/blob/main/README.md Installs tidyterra from the r-universe repository, enabling the specified universe. ```r # Enable this universe install.packages( "tidyterra", repos = c( "https://dieghernan.r-universe.dev", "https://cloud.r-project.org" ) ) ``` -------------------------------- ### Null CRS example Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glance-Spat.md Demonstrates the output of glance on a SpatVector with a null CRS. ```r glance(vnull) # A tibble: 1 x 10 geometry nrow ncol xmin xmax ymin ymax source crs crs_units 1 polygons 9 0 2892687. 3341372. 2017622. 2.36e6 cyl.g~ ETRS~ meter ``` -------------------------------- ### Cartesian (Meter) example Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glance-Spat.md Demonstrates the output of glance on a SpatVector with Cartesian coordinates in meters. ```r glance(v2) # A tibble: 1 x 9 geometry nrow ncol xmin xmax ymin ymax source crs 1 polygons 9 3 -7.08 -1.78 40.1 43.2 NA Cartesian (Meter) ``` -------------------------------- ### NA CRS example with SpatVector Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glance-Spat.md Demonstrates the output of glance on a SpatVector with an undefined CRS. ```r glance(v) # A tibble: 1 x 10 geometry nrow ncol xmin xmax ymin ymax source crs crs_units 1 points 64800 1 -180. 180. -89.5 89.5 NA CRS: Not Define~ NA ``` -------------------------------- ### Empty SpatRaster Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glance-Spat.md Example of using glance() on an empty SpatRaster object. ```R glance(empt) ``` -------------------------------- ### Stress SpatRaster Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glance-Spat.md Example of using glance() on a stressed SpatRaster object. ```R glance(v) ``` -------------------------------- ### NA CRS example with raster Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glance-Spat.md Demonstrates the output of glance on a SpatRaster with an undefined CRS. ```r glance(r) # A tibble: 1 x 16 nrow ncol nlyr ncell xres yres xmin xmax ymin ymax crs crs_units 1 180 360 1 64800 1 1 -180 180 -90 90 CRS: No~ NA # i 4 more variables: source , has_rgb , has_colors , # has_time ``` -------------------------------- ### lon/lat WGS 84 example Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glance-Spat.md Demonstrates the output of glance on a SpatVector with lon/lat coordinates. ```r glance(v2) # A tibble: 1 x 10 geometry nrow ncol xmin xmax ymin ymax source crs crs_units 1 polygons 9 3 -7.08 -1.78 40.1 43.2 NA lon/lat WGS 84 ~ degrees ``` -------------------------------- ### Run local tests Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/test_local/README.md This code block shows how to load the package, set the CRAN test condition, and run the local tests using test_dir. ```r # Load package devtools::load_all() testthat::local_on_cran(FALSE) testthat::test_dir("tests/testthat/test_local", package = "tidyterra", load_package = "source" ) withr::deferred_clear() ``` -------------------------------- ### Basic SpatRaster manipulation and plotting Source: https://github.com/dieghernan/tidyterra/blob/main/index.md A basic example demonstrating how to manipulate and plot SpatRaster objects using tidyterra and terra. ```r library(tidyterra) library(terra) ``` -------------------------------- ### Check crs Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/pull-crs.md Example of using pull_crs and the expected message when a WKT equivalent is not found. ```r res <- pull_crs(list(1)) ! `tidyterra::pull_crs()` could not find a WKT equivalent. Returning NA. ``` -------------------------------- ### Coltab SpatRaster (end) Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glance-Spat.md Example of using glance() on a Coltab SpatRaster object (end). ```R glance(end) ``` -------------------------------- ### Plotting hypsometric maps Source: https://github.com/dieghernan/tidyterra/blob/main/index.md This example shows how to plot a hypsometric map using geom_spatraster and a specialized color scale (scale_fill_hypso_tint_c) from tidyterra. ```r 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" ) ``` -------------------------------- ### Chaining multiple tallies Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/count-tally-SpatVector.md Illustrates chaining multiple count() calls, including the setup of a SpatVector and the output of each step. ```R df <- data.frame(g = c(1, 1, 2, 2), n = 1:4) df$lat <- 1:4 df$lon <- 1:4 df <- terra::vect(df, crs = "EPSG:3857") count(df, g, wt = n) ``` ```R count(count(df, g, wt = n), wt = n) ``` -------------------------------- ### Stress SpatRaster (v2) Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glance-Spat.md Another example of using glance() on a stressed SpatRaster object (v2). ```R glance(v2) ``` -------------------------------- ### Coltab SpatRaster (twocoltabs) Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glance-Spat.md Example of using glance() on a Coltab SpatRaster object (twocoltabs). ```R glance(twocoltabs) ``` -------------------------------- ### Suppressing startup messages in tidyterra 0.2.2 Source: https://github.com/dieghernan/tidyterra/blob/main/NEWS.md Example of how to suppress package startup messages when loading tidyterra. ```r suppressPackageStartupMessages(library(tidyterra)) ``` -------------------------------- ### geom_spatraster with SpatRaster data and maxcell Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/test_local/_snaps/geom-spatraster-1lyr.md Example of using geom_spatraster with SpatRaster data and specifying maxcell. ```r p_res <- ggplot() + geom_spatraster(data = r, maxcell = 20) ``` -------------------------------- ### Return NULL - No Color Table Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/scales-coltab.md Example showing get_coltab_pal returning NULL when the input SpatRaster has no color table. ```r res <- get_coltab_pal(r) ``` -------------------------------- ### Plotting RGB SpatRaster tiles Source: https://github.com/dieghernan/tidyterra/blob/main/README.md Example of plotting RGB SpatRaster tiles with ggplot2 and tidyterra. ```r 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) ``` -------------------------------- ### Facet all layers Source: https://github.com/dieghernan/tidyterra/blob/main/README.md Example of faceting spatial raster layers using ggplot2 and tidyterra. ```r 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" ) ``` -------------------------------- ### Mixed Layer Classes Warning (Factor) Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/geom-spatraster.md Example of a warning and message when mixed layer classes (factor) are found. ```R expectssss <- ggplot2::ggplot() + geom_spatraster(data = smix2) ``` -------------------------------- ### Discrete scale fill - No Color Table Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/scales-coltab.md Example of scale_fill_coltab returning NULL when input data is an empty SpatRaster. ```r pnull <- p + scale_fill_coltab(data = terra::rast()) ``` -------------------------------- ### SpatVector objects manipulation and plotting Source: https://github.com/dieghernan/tidyterra/blob/main/README.md Basic example of manipulating and plotting SpatVector objects with tidyterra. ```r 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" ) ``` -------------------------------- ### Testing preservation of grouping variables Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/select-Spat.md This example tests the preservation of grouping variables when selecting columns, ensuring that missing grouping variables are correctly identified and added. ```r expect_equal(group_keys(select(df, a = c)), group_keys(group_by(tibble::tibble( b = 2, a = 3), b))) Message Adding missing grouping variables: `b` ``` ```r expect_equal(group_keys(select(df, b = c)), group_keys(group_by(tibble::tibble( a = 1, b = 3), a))) Message Adding missing grouping variables: `a` ``` -------------------------------- ### Mixed Layer Classes Warning (Numeric) Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/geom-spatraster.md Example of a warning and message when mixed layer classes (numeric) are found. ```R expectssss <- ggplot2::ggplot() + geom_spatraster(data = smix) ``` -------------------------------- ### Plotting temperature variation between months Source: https://github.com/dieghernan/tidyterra/blob/main/index.md This example shows how to calculate the difference between two raster layers (months) and plot this variation, overlaying a SpatVector for context. ```r # Create the maximum 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" ) ``` -------------------------------- ### Discrete scale color - No Color Table Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/scales-coltab.md Example of scale_color_coltab returning NULL when input data is an empty SpatRaster. ```r pnull <- p + scale_color_coltab(data = terra::rast()) ``` -------------------------------- ### PR 165 - Example 1 Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/scales-grass.md Example output for data$.label in PR 165. ```R data$.label ``` -------------------------------- ### Breaking scale col tint - Invalid Palette Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/scales-cross-blended.md Example demonstrating an error when an unknown palette is provided to `scale_colour_cross_blended_tint_b`. ```r perr <- p + scale_colour_cross_blended_tint_b(palette = "aa") Condition Error in `scale_colour_cross_blended_tint_b()`: ! `palette` "palette" is not a known palette. See `?tidyterra::cross_blended_hypsometric_tints_db()` ``` -------------------------------- ### Hypsometric maps with specific scales Source: https://github.com/dieghernan/tidyterra/blob/main/README.md Example of plotting hypsometric maps using specific scales provided by tidyterra. ```r 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" ) ``` -------------------------------- ### Distinct function with SpatVector Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/distinct-SpatVector.md Example showing the distinct function applied to a SpatVector object and the expected messages regarding column renaming. ```R expect_message(vall <- distinct(v), "with duplicated or reserved") Message ! Renaming columns: Output * `geometry` -> `geometry.1` Message i Column(s) with duplicated or reserved names detected. See About layer/column names section on `tidyterra::as_tibble.SpatRaster()` ! Renaming columns: Output * `geometry` -> `geometry.1` ``` -------------------------------- ### Glimpse SpatVector (Points) Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md Demonstrates the output of glimpse on a SpatVector with Point geometry. ```R glimpse(p) ``` -------------------------------- ### Example of rowwise error Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/rowwise-SpatVector.md This code demonstrates the error that occurs when trying to apply `rowwise()` to a grouped SpatVector without first ungrouping. ```R rowwise(gr, y) ``` -------------------------------- ### Palette - unknown palette error Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/scales-whitebox.md Demonstrates an error when an unknown palette name is provided. ```r whitebox.colors(20, "xx") ``` -------------------------------- ### Breaking Scale - Invalid Direction Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/test_local/_snaps/scales-col-cross-blended.md Example of an invalid direction value for a breaking cross-blended color scale. ```r p + scale_colour_cross_blended_b(direction = 0.5) ``` -------------------------------- ### Glimpse SpatVector (Lines) Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md Demonstrates the output of glimpse on a SpatVector with Line geometry. ```R glimpse(l) ``` -------------------------------- ### Plotting mixed factor layers with CRS and facetting Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/test_local/_snaps/geom-spatraster-3lyr.md Example of plotting mixed factor SpatRaster layers with CRS using facet_wrap and a discrete fill scale, noting the warning about mixed classes. ```r p_mix2 <- ggplot() + geom_spatraster(data = r_mix2) + facet_wrap(~lyr) + scale_fill_terrain_d() ``` -------------------------------- ### Plotting mixed numeric layers with CRS and facetting Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/test_local/_snaps/geom-spatraster-3lyr.md Example of plotting mixed numeric SpatRaster layers with CRS using facet_wrap and a continuous fill scale, noting the warning about mixed classes. ```r p_mix1 <- ggplot() + geom_spatraster(data = r_mix1) + facet_wrap(~lyr) + scale_fill_terrain_c() ``` -------------------------------- ### Glimpse with WKT geometry and reduced width Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md Shows how to specify the geometry type as Well-Known Text (WKT) and reduce the output width for a more compact view. ```r glimpse(v, geom = "WKT", width = 5) ``` -------------------------------- ### Breaking Scale Tint - Invalid Direction Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/test_local/_snaps/scales-col-cross-blended.md Example of an invalid direction value for a breaking tinted cross-blended color scale. ```r p + scale_colour_cross_blended_tint_b(direction = -12) ``` -------------------------------- ### Continuous Scale Tint - Invalid Palette Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/test_local/_snaps/scales-col-cross-blended.md Example of an invalid palette name for a continuous tinted cross-blended color scale. ```r p + scale_colour_cross_blended_tint_c(palette = "x") ``` -------------------------------- ### Continuous Scale Tint - Invalid Direction Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/test_local/_snaps/scales-col-cross-blended.md Example of an invalid direction value for a continuous tinted cross-blended color scale. ```r p + scale_colour_cross_blended_tint_c(direction = -12) ``` -------------------------------- ### Breaking Scale Tint - Invalid Palette (1) Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/test_local/_snaps/scales-col-cross-blended.md Example of an invalid palette name for a breaking tinted cross-blended color scale. ```r p + scale_colour_cross_blended_tint_b(palette = "x") ``` -------------------------------- ### Basic glimpse of a SpatVector Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md Demonstrates the default output of glimpse on a SpatVector, showing its dimensions, geometry type, CRS, and a preview of its attributes. ```r glimpse(v) ``` -------------------------------- ### Discrete Scale Tint - Invalid Direction Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/test_local/_snaps/scales-col-cross-blended.md Example of an invalid direction value for a discrete tinted cross-blended color scale. ```r p + scale_colour_cross_blended_tint_d(direction = -12) ``` -------------------------------- ### Fortify SpatRasters pivot Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/fortify-Spat.md Demonstrates fortifying a SpatRaster with pivot=TRUE, showing a warning about mixed layer classes and a message about plotting specific layers. ```R aa <- fortify(fort2, pivot = TRUE) ``` -------------------------------- ### Glimpse SpatRaster (default) Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md Demonstrates the default output of glimpse on a SpatRaster object. ```R glimpse(r) ``` -------------------------------- ### Discrete scale fill - Invalid Alpha Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/scales-coltab.md Example of scale_fill_coltab error when alpha is outside the valid range. ```r p + scale_fill_coltab(data = r, alpha = -1) ``` -------------------------------- ### Glimpse SpatRaster (with width and n=2) Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md Demonstrates glimpse on a SpatRaster with specified width and showing the first two layers. ```R glimpse(r, width = 50, n = 2) ``` -------------------------------- ### Discrete scale color - Invalid Alpha Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/scales-coltab.md Example of scale_color_coltab error when alpha is outside the valid range. ```r p + scale_color_coltab(data = r, alpha = -1) ``` -------------------------------- ### Glimpse SpatRaster (with width and n=1) Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md Demonstrates glimpse on a SpatRaster with specified width and showing only the first layer. ```R glimpse(r, width = 50, n = 1) ``` -------------------------------- ### Remove geometry from names Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/pivot-wide-SpatVector.md Example demonstrating how to remove geometry from the names when using pivot_wider. ```R nc_unpivot <- pivot_wider(nc_pivoted, values_from = births, names_from = c( geometry, year )) Message ! Omitting "geometry" column from `names_from` argument. ``` -------------------------------- ### Remove geometry from values Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/pivot-wide-SpatVector.md Example showing how to remove geometry from the values when using pivot_wider. ```R nc_unpivot <- pivot_wider(nc_pivoted, values_from = c(births, geometry), names_from = "year") Message ! Omitting "geometry" column from `values_from` argument. ``` -------------------------------- ### Return NULL - Data Frame Input Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/scales-coltab.md Example showing get_coltab_pal returning NULL when input is a data.frame. ```r res <- get_coltab_pal(df) ``` -------------------------------- ### Palettes - unknown palette error Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/scales-grass.md Demonstrates an error when an unknown palette is specified for `grass.colors`. ```R grass.colors(20, "xx") ``` -------------------------------- ### geom_spatraster with SpatRaster data, maxcell, and interpolate Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/test_local/_snaps/geom-spatraster-1lyr.md Example of using geom_spatraster with SpatRaster data, specifying maxcell and interpolate. ```r p_res_int <- ggplot() + geom_spatraster(data = r, maxcell = 20, interpolate = TRUE) ``` -------------------------------- ### Glimpse SpatRaster (with xy and width) Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md Demonstrates glimpse on a SpatRaster with xy coordinates and specified width. ```R glimpse(r, xy = TRUE, width = 50) ``` -------------------------------- ### Glimpse with a single color table Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md Demonstrates the output of glimpse on a SpatRaster with one color table. ```R inv <- glimpse(v) ``` -------------------------------- ### Glimpse with default parameters Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md Shows the output of glimpse() on a SpatRaster with default parameters, displaying a subset of long layer names. ```r glimpse(a_rast, n = NULL, max_extra_cols = NULL) ``` -------------------------------- ### Glimpse with two color tables Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md Illustrates the output of glimpse on a SpatRaster with multiple color tables. ```R inv <- glimpse(twocoltabs) ``` -------------------------------- ### Continous scale col tint - Invalid Direction Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/scales-cross-blended.md Example demonstrating an error when a direction value other than 1 or -1 is provided to `scale_color_cross_blended_tint_c`. ```r p + scale_colour_cross_blended_tint_c(direction = 0.5) Condition Error in `scale_colour_cross_blended_tint_c()`: ! `direction` must be either 1 or -1. ``` -------------------------------- ### Basic SpatRaster manipulation and plotting Source: https://github.com/dieghernan/tidyterra/blob/main/README.md Demonstrates loading a SpatRaster object, renaming its layers, and displaying its structure. ```r 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) ``` -------------------------------- ### as_sf with SpatRaster input Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/as-sf.md Demonstrates the error message when `as_sf` is called with a SpatRaster object instead of a SpatVector. ```R as_sf(r) ``` ```text Error in `as_sf()`: ! `x` is a not a ``` -------------------------------- ### Message for plotting overlapping layers Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/geom-spat-contour-fill.md Provides a message guiding the user on how to handle plotting multiple overlapping layers with geom_spatraster_contour_filled. ```r end <- ggplot_build(aa) Message ! `tidyterra::geom_spatraster_contour_filled()`: Plotting 3 overlapping layers: tavg_04, tavg_05, and tavg_06. Either: * Use `facet_wrap(~lyr)` to facet layers. * Use `aes(fill = )` to display a single layer. ``` -------------------------------- ### geom_spatraster_rgb with resampling Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/test_local/_snaps/geom-spatraster-rgb.md Demonstrates geom_spatraster_rgb with resampling and a message indicating the number of cells. ```r p_res <- ggplot() + geom_spatraster_rgb(data = r, maxcell = 20) ``` -------------------------------- ### Breaking Scale - Invalid Alpha Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/test_local/_snaps/scales-col-cross-blended.md Example of an invalid alpha value for a breaking cross-blended color scale. ```r p_init + scale_color_cross_blended_b(alpha = -1) ``` -------------------------------- ### Mutate with multiple duplicated names Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/mutate-Spat.md Another example of mutating a SpatRaster, highlighting the renaming of multiple layers due to duplication. ```r fixed_names2 <- dplyr::mutate(spatrast, b2 = x.1 + x.2 + y.1) ``` -------------------------------- ### Palette function error Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/scales-wiki.md Demonstrates an error when an invalid palette name is provided to wiki.colors. ```r wiki.colors(20, "xx") ``` -------------------------------- ### Continous scale col tint - Invalid Alpha Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/scales-cross-blended.md Example demonstrating an error when an alpha value outside the valid range (0-1) is provided to `scale_color_cross_blended_tint_c`. ```r p + scale_colour_cross_blended_tint_c(alpha = -1) Condition Error in `scale_colour_cross_blended_tint_c()`: ! `alpha` must be between 0 and 1. ``` -------------------------------- ### Glimpse after subsetting SpatRaster with two color tables Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md Shows the output of glimpse after subsetting a SpatRaster that has two color tables, reordering the layers. ```R glimpse(terra::subset(twocoltabs, c(1, 3, 2))) ``` -------------------------------- ### ggplot2 build Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/test_local/_snaps/autoplot-Spat.md Builds a ggplot2 object from a SpatRaster. ```R b <- ggplot2::ggplot_build(forced) ``` -------------------------------- ### Glimpse of a single-layer SpatRaster with projected CRS Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md This snippet demonstrates the output of glimpse() on a SpatRaster with a projected coordinate system (meters). It highlights the differences in resolution and extent representation compared to a geographic CRS. ```R inv <- glimpse(v2) Output # A SpatRaster 126 x 212 x 1 layer (26,712 cells) # Resolution (x / y): (0.025 / 0.025) # Projected CRS: Cartesian (Meter) # Extent (x / y) : ([-7.07500 / -1.77500] , [40.08333 / 43.23333]) $ elevation_m 700.2969, 780.3889, 706.1250, 568.9722, 584.9028, 506.7361~ ``` -------------------------------- ### Anti join with SpatVector Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/join-SpatVector.md Illustrates the error when performing an anti join with a SpatVector. ```R anti_join(df1, df1) ``` -------------------------------- ### Breaking Scale Tint - Invalid Alpha Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/test_local/_snaps/scales-col-cross-blended.md Example of an invalid alpha value for a breaking tinted cross-blended color scale. ```r p + scale_colour_cross_blended_tint_b(alpha = -1) ``` -------------------------------- ### Glimpse with increased width and limited columns Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md Illustrates using a wider output and limiting the number of columns displayed, with indicators for additional variables. ```r glimpse(v, width = 50, n = 2) ``` -------------------------------- ### Continuous Scale Tint - Invalid Alpha Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/test_local/_snaps/scales-col-cross-blended.md Example of an invalid alpha value for a continuous tinted cross-blended color scale. ```r p + scale_colour_cross_blended_tint_c(alpha = -1) ``` -------------------------------- ### Error with non-existent aesthetic mapping Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/geom-spat-contour.md Demonstrates the error message when a non-existent aesthetic mapping is provided. ```r ggplot() + geom_spatraster_contour(data = r, aes(z = noexist)) ``` -------------------------------- ### geom_spatraster_rgb with resampling and interpolation Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/test_local/_snaps/geom-spatraster-rgb.md Demonstrates geom_spatraster_rgb with resampling and interpolation, showing a message about the number of cells. ```r p_res_int <- ggplot() + geom_spatraster_rgb(data = r, maxcell = 20, interpolate = TRUE) ``` -------------------------------- ### Glimpse of an RGB SpatRaster (3 channels) Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md This snippet illustrates the output of glimpse() on an RGB SpatRaster with 3 channels (Red, Green, Blue). It shows dimensions, resolution, CRS, extent, and the data type and first few values for each RGB channel. ```R inv <- glimpse(v) Output # A SpatRaster 212 x 261 x 3 layers (55,332 cells) # Resolution (x / y): (2,445.985 / 2,445.985) # Projected CRS: WGS 84 / Pseudo-Mercator (EPSG:3857) # CRS projection units: meter # Extent (x / y) : ([ -812,067.0 / -173,664.9] , [4,852,834.1 / 5,371,382.9]) # SpatRaster with 3 RGB channels: cyl_tile_1 (Red), cyl_tile_2 (Green), cyl_tile_3 (Blue) $cyl_tile_1 217, 234, 217, 217, 242, 200, 197, 197, 217, 209, 213, 205,~ $cyl_tile_2 229, 235, 229, 229, 239, 153, 180, 187, 229, 226, 228, 225,~ $cyl_tile_3 206, 224, 206, 206, 233, 169, 194, 193, 206, 198, 202, 194,~ ``` -------------------------------- ### Example of mutate on grouped SpatVector Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/group-by-SpatVector-terra.md This code snippet shows the use of `mutate` on a grouped SpatVector object, which triggers specific messages from tidyterra. ```R gr_v2 <- mutate(gr_v2, a = 1) ``` -------------------------------- ### Glimpse of an RGB SpatRaster with different channel order Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md This snippet shows the output of glimpse() on an RGB SpatRaster where the channel order is specified differently (e.g., Red, Green, Blue vs. Green, Blue, Red). The glimpse output reflects the assigned channel names. ```R inv <- glimpse(v) Output # A SpatRaster 212 x 261 x 3 layers (55,332 cells) # Resolution (x / y): (2,445.985 / 2,445.985) # Projected CRS: WGS 84 / Pseudo-Mercator (EPSG:3857) # CRS projection units: meter # Extent (x / y) : ([ -812,067.0 / -173,664.9] , [4,852,834.1 / 5,371,382.9]) # SpatRaster with 3 RGB channels: cyl_tile_2 (Red), cyl_tile_3 (Green), cyl_tile_1 (Blue) $cyl_tile_1 217, 234, 217, 217, 242, 200, 197, 197, 217, 209, 213, 205,~ $cyl_tile_2 229, 235, 229, 229, 239, 153, 180, 187, 229, 226, 228, 225,~ $cyl_tile_3 206, 224, 206, 206, 233, 169, 194, 193, 206, 198, 202, 194,~ ``` -------------------------------- ### Glimpse with increased width and only one column shown Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md Demonstrates displaying a wider output while showing only one column, with indicators for the remaining variables. ```r glimpse(v, width = 50, n = 1) ``` -------------------------------- ### as_tibble with SpatVector (WKT) Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/as-tibble-Spat.md Converting a SpatVector to a tibble with WKT geometry. ```R res <- dplyr::as_tibble(v2, geom = "WKT") ``` -------------------------------- ### bind_spat_cols() repairs names with tibble::as_tibble Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/bind-cols-SpatVector.md This example shows how bind_spat_cols()'s name repair mechanism is similar to tibble::as_tibble's 'unique' repair. ```R repaired <- tibble::as_tibble(data.frame(a = 1, b = 2, a = 1, b = 2, check.names = FALSE), .name_repair = "unique") ``` -------------------------------- ### Invalid xycols length Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/as-spatraster.md Demonstrates the error when xycols does not have the expected length of 2. ```R as_spatraster(as_tbl, xycols = 2) ``` -------------------------------- ### Glimpse with NA CRS (SpatRaster, different extent) Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md Illustrates the output of glimpse on a different SpatRaster with an undefined CRS and a smaller extent. ```R glimpse(r1) ``` -------------------------------- ### Decimal to degrees conversion (latitude, negative) Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md Converts a negative decimal latitude to degrees, minutes, and seconds format. ```R decimal_to_degrees(-79.89, "lat") ``` -------------------------------- ### Glimpse with no color table and one color table Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md Shows the output of glimpse on a SpatRaster with two layers, one without a color table and one with. ```R inv <- glimpse(end) ``` -------------------------------- ### Warning with zero contours generated Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/geom-spat-contour.md Demonstrates warnings and computation failure when zero contours are generated. ```r end <- ggplot_build(ff) ``` -------------------------------- ### group_modify() with a function that returns an empty SpatVector Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/group-map-SpatVector.md This code snippet shows an example of using group_modify() with a function that returns an empty SpatVector. It is expected to produce an error because the function does not return SpatVector objects. ```R group_modify(v, ~ head(.x, 0)) ``` -------------------------------- ### Tidyselect: starts_with Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/pivot-long-SpatVector.md Tests tidyselect with starts_with for selecting columns in remove_geom_col, including a message about omitting the 'geometry' column. ```R out <- remove_geom_col(tbl, dplyr::starts_with("g"), "test_that") ``` -------------------------------- ### Drop NA from SpatVector with no CRS resulting in empty geometry Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/drop-na-Spat.md This example demonstrates that calling drop_na on a SpatVector object `nocrs` without a Coordinate Reference System (CRS) also results in an empty SpatVector when all geometries have NA values. The message confirms that all geometries were dropped. ```R emptycrs <- drop_na(nocrs) ``` -------------------------------- ### Decimal to degrees conversion (latitude, positive) Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md Converts a positive decimal latitude to degrees, minutes, and seconds format. ```R decimal_to_degrees(79.89, "lat") ``` -------------------------------- ### Glimpse of an empty SpatRaster Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md This snippet shows the output of glimpse() on an empty SpatRaster, indicating its dimensions, number of layers, resolution, CRS, and a message that it contains no values. ```R inv <- glimpse(empt) Output # A SpatRaster 9 x 4 x 2 layers (36 cells) # Resolution (x / y): (987,385.4 / 987,385.4) # Projected CRS: World_Robinson (ESRI:54030) # CRS projection units: meter # Extent (x / y) : ([-2,361,760 / 1,587,782] , [-7,816,949 / 1,069,519]) SpatRaster with no values ``` -------------------------------- ### Invalid xycols length (greater than 2) Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/as-spatraster.md Demonstrates the error when xycols has a length greater than 2. ```R as_spatraster(as_tbl, xycols = 1:3) ``` -------------------------------- ### Continuous scale tint - palette parameter Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/scales-hypso.md Shows an error when attempting to create a continuous hypsometric tint scale with an invalid palette. ```r perr <- p + scale_fill_hypso_tint_c(palette = "aa") ``` -------------------------------- ### Discrete scale tint - direction parameter Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/scales-hypso.md Demonstrates an error when the direction parameter is not 1 or -1 for discrete hypsometric tint scales. ```r p + scale_fill_hypso_tint_d(direction = 0.5) ``` -------------------------------- ### Invalid xycols type Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/as-spatraster.md Demonstrates the error when xycols is not numeric. ```R as_spatraster(as_tbl, xycols = c("x", "y")) ``` -------------------------------- ### Plotting average temperature by month Source: https://github.com/dieghernan/tidyterra/blob/main/index.md This code snippet demonstrates how to load a SpatRaster, rename its layers, and plot the average temperature for different months using ggplot2 and geom_spatraster. ```r 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" ) ``` -------------------------------- ### Glimpse with n = -1 Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md Shows the output of glimpse() with n = -1, which displays the first layer and indicates the number of remaining layers. ```r glimpse(a_rast, n = -1, max_extra_cols = -1) ``` -------------------------------- ### Breaking scale tint - palette parameter Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/scales-hypso.md Shows an error when attempting to create a breaking hypsometric tint scale with an invalid palette. ```r perr <- p + scale_fill_hypso_tint_b(palette = "aa") ``` -------------------------------- ### Decimal to degrees conversion (longitude, negative) Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md Converts a negative decimal longitude to degrees, minutes, and seconds format. ```R decimal_to_degrees(-79.89, "lon") ``` -------------------------------- ### RGB tile specification Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/test_local/_snaps/geom-spatraster-3lyr.md Illustrates the message recommending the use of geom_spatraster_rgb() when an RGB specification is detected. ```r p <- ggplot() + geom_spatraster(data = tile) ``` -------------------------------- ### Continuous scale colour tint - palette parameter Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/scales-hypso.md Shows an error when attempting to create a continuous hypsometric tint colour scale with an invalid palette. ```r perr <- p + scale_color_hypso_tint_c(palette = "aa") ``` -------------------------------- ### Fortify SpatRasters pivot factor Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/fortify-Spat.md Shows fortifying a SpatRaster with mixed layer classes and factor levels, resulting in a warning and a message about plotting specific layers. ```R end <- check_mixed_cols(s_r_f_mix) ``` -------------------------------- ### Breaking scale tint: Invalid palette Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/scales-cross-blended.md Demonstrates an error when a breaking cross-blended tint fill scale is created with an unknown palette. ```r perr <- p + scale_fill_cross_blended_tint_b(palette = "aa") ``` -------------------------------- ### Discrete scale tint - alpha parameter Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/scales-hypso.md Demonstrates an error when the alpha parameter is outside the valid range [0, 1] for discrete hypsometric tint scales. ```r p + scale_fill_hypso_tint_d(alpha = -1) ``` -------------------------------- ### Invalid input type (matrix) Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/as-spatraster.md Demonstrates the error when the input x is a matrix instead of a data frame or tibble. ```R as_spatraster(as.matrix(as_tbl)) ``` -------------------------------- ### Breaking scale tint - direction parameter Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/scales-hypso.md Demonstrates an error when the direction parameter is not 1 or -1 for breaking hypsometric tint scales. ```r p + scale_fill_hypso_tint_b(direction = 0.5) ``` -------------------------------- ### Handle missing combinations Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/pivot-long-SpatVector.md Demonstrates pivoting longer with missing combinations, showing a message about omitting the 'geometry' column. ```R pv <- pivot_longer(df, -id, names_to = c(".value", "n"), names_sep = "_") ``` -------------------------------- ### Decimal to degrees conversion (longitude, positive) Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/glimpse-Spat.md Converts a positive decimal longitude to degrees, minutes, and seconds format. ```R decimal_to_degrees(79.89, "lon") ``` -------------------------------- ### bind_spat_rows() give informative errors - invalid .id Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/bind-rows-SpatVector.md Illustrates the informative error message when an invalid .id argument is provided to bind_spat_rows. ```R # invalid .id df1 <- data.frame(x = 1:3, lat = 1:3, lon = 1:3) df2 <- data.frame(x = 4:6, lat = 1:3, lon = 1:3) df1 <- terra::vect(df1, crs = "EPSG:4326") df2 <- terra::vect(df2, crs = "EPSG:4326") (expect_error(bind_spat_rows(df1, df2, .id = 5))) ``` -------------------------------- ### Continuous scale tint: Invalid palette Source: https://github.com/dieghernan/tidyterra/blob/main/tests/testthat/_snaps/scales-cross-blended.md Demonstrates an error when a continuous cross-blended tint fill scale is created with an unknown palette. ```r perr <- p + scale_fill_cross_blended_tint_c(palette = "aa") ```