### Example with SpatRaster Source: https://dieghernan.github.io/tidyterra/reference/glimpse.Spat.html Demonstrates how to use glimpse() with a SpatRaster object. ```R r <- rast(system.file("extdata/cyl_elev.tif", package = "tidyterra")) r |> glimpse() #> # A SpatRaster 126 x 212 x 1 layer (26,712 cells) #> # Resolution (x / y): (1' 30" , 1' 30") #> # Geodetic CRS: lon/lat WGS 84 (EPSG:4326) #> # Extent (x / y) : ([7° 4' 30" W / 1° 46' 30" W] , [40° 4' 60" N / 43° 13' 60" N]) #> #> $ elevation_m 700.2969, 780.3889, 706.1250, 568.9722, 584.9028, 506.7361… ``` -------------------------------- ### Example with SpatVector Source: https://dieghernan.github.io/tidyterra/reference/glimpse.Spat.html Demonstrates how to use glimpse() with a SpatVector object, showing a reduced number of rows and then using it within a pipeline with additional options. ```R library(terra) # SpatVector v <- vect(system.file("extdata/cyl.gpkg", package = "tidyterra")) v |> glimpse(n = 2) #> # A SpatVector 9 x 3 #> # Geometry type: Polygons #> # Projected CRS: ETRS89-extended / LAEA Europe (EPSG:3035) #> # CRS projection units: meter #> # Extent (x / y) : ([2,892,687 / 3,341,372] , [2,017,622 / 2,361,600]) #> #> $ iso2 "ES-AV", "ES-BU", "ES-LE", "ES-P", "ES-SA", "ES-SG", "ES-SO", "ES… #> $ cpro "05", "09", "24", "34", "37", "40", "42", "47", "49" #> # ℹ 1 more variable : name #> # ℹ Use `tidyterra::glimpse(n = ...)` to see more columns # Use on a pipeline v | glimpse() | mutate(a = 30) | # with options glimpse(geom = "WKT") #> # A SpatVector 9 x 3 #> # Geometry type: Polygons #> # Projected CRS: ETRS89-extended / LAEA Europe (EPSG:3035) #> # CRS projection units: meter #> # Extent (x / y) : ([2,892,687 / 3,341,372] , [2,017,622 / 2,361,600]) #> #> $ iso2 "ES-AV", "ES-BU", "ES-LE", "ES-P", "ES-SA", "ES-SG", "ES-SO", "ES… #> $ cpro "05", "09", "24", "34", "37", "40", "42", "47", "49" #> $ name "Avila", "Burgos", "Leon", "Palencia", "Salamanca", "Segovia"… #> # A SpatVector 9 x 4 #> # Geometry type: Polygons #> # Projected CRS: ETRS89-extended / LAEA Europe (EPSG:3035) #> # CRS projection units: meter #> # Extent (x / y) : ([2,892,687 / 3,341,372] , [2,017,622 / 2,361,600]) #> #> $ iso2 "ES-AV", "ES-BU", "ES-LE", "ES-P", "ES-SA", "ES-SG", "ES-SO",… #> $ cpro "05", "09", "24", "34", "37", "40", "42", "47", "49" #> $ name "Avila", "Burgos", "Leon", "Palencia", "Salamanca", "Segovia"… #> $ a 30, 30, 30, 30, 30, 30, 30, 30, 30 #> $ geometry "POLYGON ((3126360.241699999 2066777.7545, 3125073.9751999993… ``` -------------------------------- ### Examples Source: https://dieghernan.github.io/tidyterra/reference/stat_spat_coordinates.html Examples of how to use stat_spat_coordinates. ```R # \donttest{ cyl <- terra::vect(system.file("extdata/cyl.gpkg", package = "tidyterra")) library(ggplot2) ggplot(cyl) + stat_spat_coordinates() ggplot(cyl) + geom_errorbarh( aes( geometry = geometry, xmin = after_stat(x) - 50000, xmax = after_stat(x) + 50000, y = after_stat(y), height = 10000 ), stat = "sf_coordinates" ) #> Warning: `geom_errorbarh()` was deprecated in ggplot2 4.0.0. #> ℹ Please use the `orientation` argument of `geom_errorbar()` instead. #> Warning: Ignoring unknown aesthetics: height # } ``` -------------------------------- ### Examples Source: https://dieghernan.github.io/tidyterra/reference/fill.SpatVector.html Examples of how to use the fill.SpatVector function. ```R library(dplyr) lux <- terra::vect(system.file("ex/lux.shp", package = "terra")) # Leave some blanks for demo purposes lux_blnk <- lux |> mutate(NAME_1 = if_else(NAME_1 != NAME_2, NA, NAME_2)) as_tibble(lux_blnk) ``` ```R # `fill()` defaults to replacing missing data from top to bottom lux_blnk | fill(NAME_1) | as_tibble() ``` ```R # direction = "up" lux_blnk | fill(NAME_1, .direction = "up") | as_tibble() ``` -------------------------------- ### Examples Source: https://dieghernan.github.io/tidyterra/reference/ggspatvector.html Examples of how to use geom_spatvector. ```R # \donttest{ # Create a SpatVector extfile <- system.file("extdata/cyl.gpkg", package = "tidyterra") cyl <- terra::vect(extfile) class(cyl) #> [1] "SpatVector" #> attr(,"package") #> [1] "terra" library(ggplot2) ggplot(cyl) + geom_spatvector() # With params ggplot(cyl) + geom_spatvector(aes(fill = name), color = NA) + scale_fill_viridis_d() + coord_sf(crs = 3857) ``` -------------------------------- ### Examples Source: https://dieghernan.github.io/tidyterra/reference/is_regular_grid.html Examples of how to use the is_regular_grid function. ```R p <- matrix(1:90, nrow = 45, ncol = 2) is_regular_grid(p) # Jitter location set.seed(1234) jitter <- runif(length(p)) / 10e4 p_jitter <- p + jitter # Need to adjust digits is_regular_grid(p_jitter, digits = 4) ``` -------------------------------- ### Examples Source: https://dieghernan.github.io/tidyterra/reference/rename.Spat.html Examples demonstrating how to use `rename()` and `rename_with()` on a SpatRaster object. ```R library(terra) spatrast <- rast(f) |> mutate(aa = 1, bb = 2, cc = 3) spatrast #> class : SpatRaster #> size : 212, 261, 6 (nrow, ncol, nlyr) #> resolution : 2445.985, 2445.985 (x, y) #> extent : -812067, -173664.9, 4852834, 5371383 (xmin, xmax, ymin, ymax) #> coord. ref. : WGS 84 / Pseudo-Mercator (EPSG:3857) #> source(s) : memory #> names : cyl_tile_1, cyl_tile_2, cyl_tile_3, aa, bb, cc #> min values : 35, 35, 35, 1, 2, 3 #> max values : 253, 251, 250, 1, 2, 3 spatrast |> rename( this_first = cyl_tile_1, this_second = cyl_tile_2 ) #> class : SpatRaster #> size : 212, 261, 6 (nrow, ncol, nlyr) #> resolution : 2445.985, 2445.985 (x, y) #> extent : -812067, -173664.9, 4852834, 5371383 (xmin, xmax, ymin, ymax) #> coord. ref. : WGS 84 / Pseudo-Mercator (EPSG:3857) #> source(s) : memory #> names : this_first, this_second, cyl_tile_3, aa, bb, cc #> min values : 35, 35, 35, 1, 2, 3 #> max values : 253, 251, 250, 1, 2, 3 spatrast |> rename_with( toupper, .cols = starts_with("c") ) #> class : SpatRaster #> size : 212, 261, 6 (nrow, ncol, nlyr) #> resolution : 2445.985, 2445.985 (x, y) #> extent : -812067, -173664.9, 4852834, 5371383 (xmin, xmax, ymin, ymax) #> coord. ref. : WGS 84 / Pseudo-Mercator (EPSG:3857) #> source(s) : memory #> names : CYL_TILE_1, CYL_TILE_2, CYL_TILE_3, aa, bb, CC #> min values : 35, 35, 35, 1, 2, 3 #> max values : 253, 251, 250, 1, 2, 3 ``` -------------------------------- ### Examples Source: https://dieghernan.github.io/tidyterra/reference/required_pkgs.Spat.html Examples demonstrating the usage of `required_pkgs` with SpatRaster and SpatVector objects. ```R file_path <- system.file("extdata/cyl_temp.tif", package = "tidyterra") library(terra) r <- rast(file_path) # With rasters r #> 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 #> 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 required_pkgs(r) #> [1] "terra" # With vectors v <- vect(system.file("extdata/cyl.gpkg", package = "tidyterra")) v #> class : SpatVector #> geometry : polygons #> dimensions : 9, 3 (geometries, attributes) #> extent : 2892687, 3341372, 2017622, 2361600 (xmin, xmax, ymin, ymax) #> source : cyl.gpkg #> coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035) #> names : iso2 cpro name #> type : #> values : ES-AV 05 Avila #> ES-BU 09 Burgos #> ES-LE 24 Leon #> ... required_pkgs(v) #> [1] "terra" ``` -------------------------------- ### Examples Source: https://dieghernan.github.io/tidyterra/reference/scale_coltab.html Example of how to use scale_fill_coltab with a SpatRaster and ggplot2. ```R library(terra) # Geological Eras # Spanish Geological Survey (IGME) r <- rast(system.file("extdata/cyl_era.tif", package = "tidyterra")) plot(r) # Get coltab coltab_pal <- get_coltab_pal(r) coltab_pal #> Precambric-Paleozoic Paleozoic Paleozoic-Mesozoic #> "#FFBFE9" "#9ADDCF" "#D79EBD" #> Mesozoic Mesozoic-Cenozoic Cenozoic #> "#A4FF74" "#FFD480" "#FFFFBF" #> Undetermined #> "#FFFFFF" # \donttest{ # With ggplot2 + tidyterra library(ggplot2) gg <- ggplot() + geom_spatraster(data = r) # Default plot gg # With coltabs gg + scale_fill_coltab(data = r) #> Scale for fill is already present. #> Adding another scale for fill, which will replace the existing scale. # } ``` -------------------------------- ### Examples Source: https://dieghernan.github.io/tidyterra/reference/glance.Spat.html Examples of using glance with SpatVector and SpatRaster objects. ```R library(terra) # SpatVector v <- vect(system.file("extdata/cyl.gpkg", package = "tidyterra")) glance(v) #> # A tibble: 1 × 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 # SpatRaster r <- rast(system.file("extdata/cyl_elev.tif", package = "tidyterra")) glance(r) #> # A tibble: 1 × 16 #> nrow ncol nlyr ncell xres yres xmin xmax ymin ymax crs crs_units #> #> 1 126 212 1 26712 0.025 0.025 -7.08 -1.77 40.1 43.2 lon/lat… degrees #> # ℹ 4 more variables: source , has_rgb , has_colors , #> # has_time ``` -------------------------------- ### Examples Source: https://dieghernan.github.io/tidyterra/reference/summarise.SpatVector.html Examples of using summarise.SpatVector for dissolving and not dissolving geometries. ```R library(terra) library(ggplot2) v <- vect(system.file("extdata/cyl.gpkg", package = "tidyterra")) # Grouped gr_v <- v %>% mutate(start_with_s = startsWith(name, "S")) %>% group_by(start_with_s) # Dissolving diss <- gr_v %>% summarise(n = dplyr::n(), mean = mean(as.double(cpro))) diss #> class : SpatVector #> geometry : polygons #> dimensions : 2, 3 (geometries, attributes) #> extent : 2892687, 3341372, 2017622, 2361600 (xmin, xmax, ymin, ymax) #> coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035) #> names : start_with_s n mean #> type : #> values : FALSE 6 28 #> TRUE 3 39.6667 autoplot(diss, aes(fill = start_with_s)) + ggplot2::labs(title = "Dissolved") # Not dissolving no_diss <- gr_v %>% summarise(n = dplyr::n(), mean = mean(as.double(cpro)), .dissolve = FALSE) # Same statistic no_diss #> class : SpatVector #> geometry : polygons #> dimensions : 2, 3 (geometries, attributes) #> extent : 2892687, 3341372, 2017622, 2361600 (xmin, xmax, ymin, ymax) #> coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035) #> names : start_with_s n mean #> type : #> values : FALSE 6 28 #> TRUE 3 39.6667 autoplot(no_diss, aes(fill = start_with_s)) + ggplot2::labs(title = "Not Dissolved") ``` -------------------------------- ### Examples Source: https://dieghernan.github.io/tidyterra/reference/relocate.Spat.html Demonstrates how to use relocate() to reorder layers in a SpatRaster object. ```R library(terra) f <- system.file("extdata/cyl_tile.tif", package = "tidyterra") spatrast <- rast(f) |> mutate(aa = 1, bb = 2, cc = 3) names(spatrast) #> [1] "cyl_tile_1" "cyl_tile_2" "cyl_tile_3" "aa" "bb" #> [6] "cc" spatrast | relocate(bb, .before = cyl_tile_3) | relocate(cyl_tile_1, .after = last_col()) #> class : SpatRaster #> size : 212, 261, 6 (nrow, ncol, nlyr) #> resolution : 2445.985, 2445.985 (x, y) #> extent : -812067, -173664.9, 4852834, 5371383 (xmin, xmax, ymin, ymax) #> coord. ref. : WGS 84 / Pseudo-Mercator (EPSG:3857) #> source(s) : memory #> names :cyl_tile_2, bb, cyl_tile_3, aa, cc, cyl_tile_1 #> min values : 35, 2, 35, 1, 3, 35 #> max values : 251, 2, 250, 1, 3, 253 ``` -------------------------------- ### Discrete hypsometric tint palette example Source: https://dieghernan.github.io/tidyterra/reference/scale_hypso.html Demonstrates the usage of `scale_fill_hypso_d` for discrete hypsometric palettes. ```R factor <- volcano2_rast |> mutate(cats = cut(elevation, breaks = c(100, 120, 130, 150, 170, 200), labels = c( "Very Low", "Low", "Average", "High", "Very High" ) )) ggplot() + geom_spatraster(data = factor, aes(fill = cats)) + scale_fill_hypso_d(na.value = "gray10", palette = "dem_poster") ``` -------------------------------- ### Examples Source: https://dieghernan.github.io/tidyterra/reference/princess_db.html This example demonstrates how to load and use the princess_db dataset to create a custom color scale for a raster plot. ```R # \donttest{ data("princess_db") princess_db #> # A tibble: 75 × 5 #> pal r g b hex #> #> 1 snow 227 75 98 #e34b62 #> 2 snow 138 192 229 #8ac0e5 #> 3 snow 10 100 165 #0a64a5 #> 4 snow 255 235 148 #ffeb94 #> 5 snow 255 202 53 #ffca35 #> 6 ella 215 234 246 #d7eaf6 #> 7 ella 177 222 245 #b1def5 #> 8 ella 140 197 232 #8cc5e8 #> 9 ella 59 139 189 #3b8bbd #> 10 ella 40 98 135 #286287 #> # ℹ 65 more rows # Select a palette maori <- princess_db |> filter(pal == "maori") f <- system.file("extdata/volcano2.tif", package = "tidyterra") r <- terra::rast(f) library(ggplot2) p <- ggplot() + geom_spatraster(data = r) + labs(fill = "elevation") p + scale_fill_gradientn(colors = maori$hex) # } ``` -------------------------------- ### Install tidyterra from CRAN Source: https://dieghernan.github.io/tidyterra/index.html Install tidyterra from CRAN. ```r install.packages("tidyterra") ``` -------------------------------- ### Examples Source: https://dieghernan.github.io/tidyterra/reference/bind_rows.SpatVector.html Examples of using bind_spat_rows with SpatVector and sf objects. ```R library(terra) v <- vect(system.file("extdata/cyl.gpkg", package = "tidyterra")) v1 <- v[1, "cpro"] v2 <- v[3:5, c("name", "iso2")] # You can supply individual SpatVector as arguments: bind_spat_rows(v1, v2) #> class : SpatVector #> geometry : polygons #> dimensions : 4, 3 (geometries, attributes) #> extent : 2892687, 3180130, 2017622, 2361600 (xmin, xmax, ymin, ymax) #> coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035) #> names : cpro name iso2 #> type : #> values : 05 NA NA #> NA Leon ES-LE #> NA Palencia ES-P #> ... # When you supply a column name with the `.id` argument, a new # column is created to link each row to its original data frame bind_spat_rows(v1, v2, .id = "id") #> class : SpatVector #> geometry : polygons #> dimensions : 4, 4 (geometries, attributes) #> extent : 2892687, 3180130, 2017622, 2361600 (xmin, xmax, ymin, ymax) #> coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035) #> names : id cpro name iso2 #> type : #> values : 1 05 NA NA #> 2 NA Leon ES-LE #> 2 NA Palencia ES-P #> ... # \donttest{ # Use with sf sfobj <- sf::st_as_sf(v2[1, ]) sfobj #> Simple feature collection with 1 feature and 2 fields #> Geometry type: POLYGON #> Dimension: XY #> Bounding box: xmin: 2926589 ymin: 2233673 xmax: 3125372 ymax: 2361600 #> Projected CRS: ETRS89-extended / LAEA Europe #> name iso2 geometry #> 1 Leon ES-LE POLYGON ((3049427 2233673, ... bind_spat_rows(v1, sfobj) #> class : SpatVector #> geometry : polygons #> dimensions : 2, 3 (geometries, attributes) #> extent : 2926589, 3126360, 2017622, 2361600 (xmin, xmax, ymin, ymax) #> coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035) #> names : cpro name iso2 #> type : #> values : 05 NA NA #> NA Leon ES-LE ``` -------------------------------- ### Examples Source: https://dieghernan.github.io/tidyterra/reference/select.Spat.html Examples of using the select function with SpatRaster objects. ```R library(terra) # SpatRaster method spatrast <- rast( crs = "EPSG:3857", nrows = 10, ncols = 10, extent = c(100, 200, 100, 200), nlyr = 6, vals = seq_len(10 * 10 * 6) ) spatrast |> select(1) #> class : SpatRaster #> size : 10, 10, 1 (nrow, ncol, nlyr) #> resolution : 10, 10 (x, y) #> extent : 100, 200, 100, 200 (xmin, xmax, ymin, ymax) #> coord. ref. : WGS 84 / Pseudo-Mercator (EPSG:3857) #> source(s) : memory #> name : lyr.1 #> min value : 1 #> max value : 100 # By name spatrast |> select(lyr.1:lyr.4) #> class : SpatRaster #> size : 10, 10, 4 (nrow, ncol, nlyr) #> resolution : 10, 10 (x, y) #> extent : 100, 200, 100, 200 (xmin, xmax, ymin, ymax) #> coord. ref. : WGS 84 / Pseudo-Mercator (EPSG:3857) #> source(s) : memory #> names : lyr.1, lyr.2, lyr.3, lyr.4 #> min values : 1, 101, 201, 301 #> max values : 100, 200, 300, 400 # Rename spatrast |> select(a = lyr.1, c = lyr.6) #> class : SpatRaster #> size : 10, 10, 2 (nrow, ncol, nlyr) #> resolution : 10, 10 (x, y) #> extent : 100, 200, 100, 200 (xmin, xmax, ymin, ymax) #> coord. ref. : WGS 84 / Pseudo-Mercator (EPSG:3857) #> source(s) : memory #> names : a, c #> min values : 1, 501 #> max values : 100, 600 ``` -------------------------------- ### Examples Source: https://dieghernan.github.io/tidyterra/reference/cross_blended_hypsometric_tints_db.html This example demonstrates how to load and inspect the cross_blended_hypsometric_tints_db dataset, select a specific palette, and use it with ggplot2 to visualize raster data, including options for using elevation limits. ```r # \donttest{ data("cross_blended_hypsometric_tints_db") cross_blended_hypsometric_tints_db #> # A tibble: 41 × 6 #> pal limit r g b hex #> #> 1 arid 0 160 152 141 #A0988D #> 2 arid 50 170 160 150 #AAA096 #> 3 arid 200 180 170 158 #B4AA9E #> 4 arid 600 202 190 174 #CABEAE #> 5 arid 1000 212 201 180 #D4C9B4 #> 6 arid 2000 212 184 163 #D4B8A3 #> 7 arid 3000 212 193 179 #D4C1B3 #> 8 arid 4000 212 207 204 #D4CFCC #> 9 arid 5000 220 220 220 #DCDCDC #> 10 arid 6000 235 235 237 #EBEBED #> # ℹ 31 more rows # Select a palette warm <- cross_blended_hypsometric_tints_db | filter(pal == "warm_humid") f <- system.file("extdata/asia.tif", package = "tidyterra") r <- terra::rast(f) library(ggplot2) p <- ggplot() + geom_spatraster(data = r) + labs(fill = "elevation") p + scale_fill_gradientn(colors = warm$hex) # Use with limits p + scale_fill_gradientn( colors = warm$hex, values = scales::rescale(warm$limit), limit = range(warm$limit), na.value = "lightblue" ) # } ``` -------------------------------- ### Examples Source: https://dieghernan.github.io/tidyterra/reference/group_data.SpatVector.html Examples demonstrating the usage of grouping functions on SpatVector objects, including ungrouped, grouped by one variable, and grouped by multiple variables. ```R library(terra) v <- vect(system.file("extdata/cyl.gpkg", package = "tidyterra")) v$gr_1 <- rep_len(c("A", "A", "B"), length.out = nrow(v)) v$gr_2 <- rep_len(c("C", "D"), length.out = nrow(v)) # Ungrouped n_groups(v) #> [1] 1 group_vars(v) #> character(0) group_keys(v) #> # A tibble: 1 × 0 group_size(v) #> [1] 9 groups(v) #> list() group_rows(v) #> [1]> #> [[1]] #> [1] 1 2 3 4 5 6 7 8 9 #> group_data(v) #> # A tibble: 1 × 1 #> .rows #> > #> 1 [9] group_indices(v) #> [1] 1 1 1 1 1 1 1 1 1 # Grouped by one var gv <- group_by(v, gr_1) n_groups(gv) #> [1] 2 group_vars(gv) #> [1] "gr_1" group_keys(gv) #> # A tibble: 2 × 1 #> gr_1 #> #> 1 A #> 2 B group_size(gv) #> [1] 6 3 groups(gv) #> [[1]] #> gr_1 #> group_rows(gv) #> [2]> #> [[1]] #> [1] 1 2 4 5 7 8 #> #> [[2]] #> [1] 3 6 9 #> group_data(gv) #> # A tibble: 2 × 2 #> gr_1 .rows #> > #> 1 A [6] #> 2 B [3] group_indices(gv) #> [1] 1 1 2 1 1 2 1 1 2 # Grouped by several vars gv2 <- group_by(v, gr_1, gr_2) n_groups(gv2) #> [1] 4 group_vars(gv2) #> [1] "gr_1" "gr_2" group_keys(gv2) #> # A tibble: 4 × 2 #> gr_1 gr_2 #> #> 1 A C #> 2 A D #> 3 B C #> 4 B D group_size(gv2) #> [1] 3 3 2 1 groups(gv2) #> [[1]] #> gr_1 #> #> [[2]] #> gr_2 #> group_rows(gv2) #> [4]> #> [[1]] #> [1] 1 5 7 #> #> [[2]] #> [1] 2 4 8 #> #> [[3]] #> [1] 3 9 #> #> [[4]] #> [1] 6 #> group_data(gv2) #> # A tibble: 4 × 3 #> gr_1 gr_2 .rows #> > #> 1 A C [3] #> 2 A D [3] #> 3 B C [2] #> 4 B D [1] group_indices(gv2) #> [1] 1 2 3 2 1 4 1 2 3 ``` -------------------------------- ### Examples Source: https://dieghernan.github.io/tidyterra/reference/arrange.SpatVector.html Examples of using the arrange.SpatVector function with different sorting criteria. ```R library(terra) #> terra 1.9.27 library(dplyr) #> #> Attaching package: ‘dplyr’ #> The following objects are masked from ‘package:terra’: #> #> intersect, union #> The following objects are masked from ‘package:stats’: #> #> filter, lag #> The following objects are masked from ‘package:base’: #> #> intersect, setdiff, setequal, union v <- vect(system.file("extdata/cyl.gpkg", package = "tidyterra")) # Single variable v |> arrange(desc(iso2)) #> class : SpatVector #> geometry : polygons #> dimensions : 9, 3 (geometries, attributes) #> extent : 2892687, 3341372, 2017622, 2361600 (xmin, xmax, ymin, ymax) #> coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035) #> names : iso2 cpro name #> type : #> values : ES-ZA 49 Zamora #> ES-VA 47 Valladolid #> ES-SO 42 Soria #> ... # Two variables v |> mutate(even = as.double(cpro) %% 2 == 0) |> arrange(desc(even), desc(iso2)) #> class : SpatVector #> geometry : polygons #> dimensions : 9, 4 (geometries, attributes) #> extent : 2892687, 3341372, 2017622, 2361600 (xmin, xmax, ymin, ymax) #> coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035) #> names : iso2 cpro name even #> type : #> values : ES-SO 42 Soria TRUE #> ES-SG 40 Segovia TRUE #> ES-P 34 Palencia TRUE #> ... # With new variables v |> mutate(area_geom = terra::expanse(v)) |> arrange(area_geom) #> class : SpatVector #> geometry : polygons #> dimensions : 9, 4 (geometries, attributes) #> extent : 2892687, 3341372, 2017622, 2361600 (xmin, xmax, ymin, ymax) #> coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035) #> names : iso2 cpro name area_geom #> type : #> values : ES-SG 40 Segovia 6.92148e+09 #> ES-P 34 Palencia 8.04248e+09 #> ES-AV 05 Avila 8.05299e+09 #> ... ``` -------------------------------- ### Examples Source: https://dieghernan.github.io/tidyterra/reference/pull_crs.html Examples demonstrating how to use pull_crs with sf objects, terra objects, integers, and characters. ```R # sf objects sfobj <- sf::st_as_sfc("MULTIPOINT ((0 0), (1 1))", crs = 4326) fromsf1 <- pull_crs(sfobj) fromsf2 <- pull_crs(sf::st_crs(sfobj)) # terra v <- terra::vect(sfobj) r <- terra::rast(v) fromterra1 <- pull_crs(v) fromterra2 <- pull_crs(r) # integers fromint <- pull_crs(4326) # Characters fromchar <- pull_crs("epsg:4326") all( fromsf1 == fromsf2, fromsf2 == fromterra1, fromterra1 == fromterra2, fromterra2 == fromint, fromint == fromchar ) #> [1] TRUE cat(fromsf1) #> GEOGCRS["WGS 84", #> ENSEMBLE["World Geodetic System 1984 ensemble", #> MEMBER["World Geodetic System 1984 (Transit)"], #> MEMBER["World Geodetic System 1984 (G730)"], #> MEMBER["World Geodetic System 1984 (G873)"], #> MEMBER["World Geodetic System 1984 (G1150)"], #> MEMBER["World Geodetic System 1984 (G1674)"], #> MEMBER["World Geodetic System 1984 (G1762)"], #> MEMBER["World Geodetic System 1984 (G2139)"], #> ELLIPSOID["WGS 84",6378137,298.257223563, #> LENGTHUNIT["metre",1]], #> ENSEMBLEACCURACY[2.0]], #> PRIMEM["Greenwich",0, #> ANGLEUNIT["degree",0.0174532925199433]], #> CS[ellipsoidal,2], #> AXIS["geodetic latitude (Lat)",north, #> ORDER[1], #> ANGLEUNIT["degree",0.0174532925199433]], #> AXIS["geodetic longitude (Lon)",east, #> ORDER[2], #> ANGLEUNIT["degree",0.0174532925199433]], #> USAGE[ #> SCOPE["Horizontal component of 3D system."], #> AREA["World."], #> BBOX[-90,-180,90,180]], #> ID["EPSG",4326]] ``` -------------------------------- ### Install development dependencies and check package Source: https://dieghernan.github.io/tidyterra/CONTRIBUTING.html Install all development dependencies and ensure the package passes R CMD check. ```r devtools::install_dev_deps() devtools::check() ``` -------------------------------- ### Basic hypsometric tint palette example Source: https://dieghernan.github.io/tidyterra/reference/scale_hypso.html Demonstrates the basic usage of `scale_fill_hypso_tint_c` with a specified palette. ```R library(terra) filepath <- system.file("extdata/volcano2.tif", package = "tidyterra") volcano2_rast <- rast(filepath) library(ggplot2) ggplot() + geom_spatraster(data = volcano2_rast) + scale_fill_hypso_tint_c(palette = "colombia_hypso") ``` -------------------------------- ### Example 1: Basic slicing with glimpse and autoplot Source: https://dieghernan.github.io/tidyterra/reference/slice.Spat.html Demonstrates how to use glimpse and autoplot with a SpatVector, showing the structure and plotting it. ```r glimpse(v) |> autoplot(aes(fill = NAME_1)) ``` -------------------------------- ### With options Source: https://dieghernan.github.io/tidyterra/reference/glimpse.Spat.html Shows how to use glimpse() with the 'xy' option set to TRUE to include coordinate information. ```R r |> glimpse(xy = TRUE) ``` -------------------------------- ### Binned hypsometric tint palette example Source: https://dieghernan.github.io/tidyterra/reference/scale_hypso.html Demonstrates the usage of `scale_fill_hypso_b` for binned hypsometric palettes. ```R ggplot() + geom_spatraster(data = volcano2_rast) + scale_fill_hypso_b(breaks = seq(70, 200, 25), palette = "wiki-2.0_hypso") ```