dieghernan / tidyterra

tidyverse and ggplot2 methods for terra spatial objects

Home Page:https://dieghernan.github.io/tidyterra/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error in `[.data.frame`(data, c("x", "y", "size", "colour", "linetype")) : undefined columns selected

kdizzard opened this issue · comments

I'm getting the folowing error that I can't work around or find help on:

ggplot() +

  • geom_spatraster_contour(
  • data = elevation, binwidth = 250,
    
  • alpha = 0.7, color = "black"
    
  • ) +
  • coord_sf(expand = FALSE)
    SpatRaster resampled to ncells = 501184
    Error in [.data.frame(data, c("x", "y", "size", "colour", "linetype")) :
    undefined columns selected

I'm using an elevation raster:

elevation
class : SpatRaster
dimensions : 4296, 3687, 1 (nrow, ncol, nlyr)
resolution : 25, 25 (x, y)
extent : 1488688, 1580863, 466187.5, 573587.5 (xmin, xmax, ymin, ymax)
coord. ref. : NAD83 / BC Albers
source : tsa02_elev25m.tif
name : tsa02_elev25m
min value : 434
max value : 2412

when I run the following it runs w/o issue:
ggplot() +

  • geom_spatraster(data = elevation) +
  • scale_fill_whitebox_c(
  • palette = "muted",
    
  • na.value = NA,
    
  • name = "elevation (m)"
    
  • )+
  • labs(
  • title = "Elevation",
    
  • subtitle = "Boundary TSA"
    
  • )+
  • coord_sf(datum = pull_crs(elevation)) +
  • theme_bw()
    SpatRaster resampled to ncells = 501184

Would it be possible that you share the raster file (tsa02_elev25m.tif I guess)?

Thanks, I’ll have a look when I can, in the meantime you can try with a pure ggplot2 approach (need latest CRAN tidyterra release, v0.3.1) leveraging on the fortify method added on that version:

https://dieghernan.github.io/tidyterra/reference/fortify.Spat.html

ggplot(elevation) +
  geom_contour(aes(x,y, z= tsa02_elev25m), binwidth = 250) +
  coord_sf(datum = pull_crs(elevation), expand=FALSE)

Hi,

With tidyterra v0.3.1 (latest CRAN release) I can't reproduce the issue, everything seems to be fine. Please make sure you have the latest version of the package. See also the sessioninfo attached on this reproducible example

tmp <- file.path(tempdir(), "tsa02_elev25m.tif")
download.file(
  "https://www.for.gov.bc.ca/ftp/HTS/external/outgoing/kelly/tsa02_elev25m.tif",
  tmp,
  mode = "wb"
)

library(terra)
#> terra 1.6.47
elevation <- rast(tmp)
elevation
#> class       : SpatRaster 
#> dimensions  : 4296, 3687, 1  (nrow, ncol, nlyr)
#> resolution  : 25, 25  (x, y)
#> extent      : 1488688, 1580863, 466187.5, 573587.5  (xmin, xmax, ymin, ymax)
#> coord. ref. : NAD83 / BC Albers (EPSG:3005) 
#> source      : tsa02_elev25m.tif 
#> name        : tsa02_elev25m 
#> min value   :           434 
#> max value   :          2412

# Plot
library(ggplot2)
library(tidyterra)
#> 
#> Attaching package: 'tidyterra'
#> The following object is masked from 'package:stats':
#> 
#>     filter

# No fail...

ggplot() +
  geom_spatraster_contour(
    data = elevation, binwidth = 250,
    alpha = 0.7, color = "black"
  ) +
  coord_sf(expand = FALSE)
#> SpatRaster resampled to ncells = 501184

# No problem...

ggplot() +
  geom_spatraster(data = elevation) +
  scale_fill_whitebox_c(
    palette = "muted", na.value = NA,
    name = "elevation (m)"
  ) +
  labs(
    title = "Elevation",
    subtitle = "Boundary TSA"
  ) +
  coord_sf(datum = pull_crs(elevation)) +
  theme_bw()
#> SpatRaster resampled to ncells = 501184

Created on 2023-01-02 with reprex v2.0.2

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.2 (2022-10-31 ucrt)
#>  os       Windows 10 x64 (build 22621)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  Spanish_Spain.utf8
#>  ctype    Spanish_Spain.utf8
#>  tz       Europe/Paris
#>  date     2023-01-02
#>  pandoc   2.19.2 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  ! package     * version date (UTC) lib source
#>    assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.2.0)
#>    class         7.3-20  2022-01-16 [2] CRAN (R 4.2.2)
#>    classInt      0.4-8   2022-09-29 [1] CRAN (R 4.2.1)
#>    cli           3.5.0   2022-12-20 [1] CRAN (R 4.2.2)
#>    codetools     0.2-18  2020-11-04 [2] CRAN (R 4.2.2)
#>    colorspace    2.0-3   2022-02-21 [1] CRAN (R 4.2.1)
#>    curl          4.3.3   2022-10-06 [1] CRAN (R 4.2.1)
#>    data.table    1.14.6  2022-11-16 [1] CRAN (R 4.2.2)
#>    DBI           1.1.3   2022-06-18 [1] CRAN (R 4.2.1)
#>    digest        0.6.31  2022-12-11 [1] CRAN (R 4.2.2)
#>    dplyr         1.0.10  2022-09-01 [1] CRAN (R 4.2.2)
#>    e1071         1.7-12  2022-10-24 [1] CRAN (R 4.2.2)
#>    ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.2.0)
#>    evaluate      0.19    2022-12-13 [1] CRAN (R 4.2.2)
#>  D fansi         1.0.3   2022-03-24 [1] CRAN (R 4.2.0)
#>    farver        2.1.1   2022-07-06 [1] CRAN (R 4.2.1)
#>    fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.2.0)
#>    fs            1.5.2   2021-12-08 [1] CRAN (R 4.2.0)
#>    generics      0.1.3   2022-07-05 [1] CRAN (R 4.2.1)
#>    ggplot2     * 3.4.0   2022-11-04 [1] CRAN (R 4.2.2)
#>  D glue          1.6.2   2022-02-24 [1] CRAN (R 4.2.0)
#>    gtable        0.3.1   2022-09-01 [1] CRAN (R 4.2.1)
#>    highr         0.10    2022-12-22 [1] CRAN (R 4.2.2)
#>    htmltools     0.5.4   2022-12-07 [1] CRAN (R 4.2.2)
#>    httr          1.4.4   2022-08-17 [1] CRAN (R 4.2.1)
#>    isoband       0.2.7   2022-12-20 [1] CRAN (R 4.2.2)
#>    KernSmooth    2.23-20 2021-05-03 [2] CRAN (R 4.2.2)
#>    knitr         1.41    2022-11-18 [1] CRAN (R 4.2.2)
#>    labeling      0.4.2   2020-10-20 [1] CRAN (R 4.2.0)
#>    lifecycle     1.0.3   2022-10-07 [1] CRAN (R 4.2.1)
#>  D magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.2.0)
#>    mime          0.12    2021-09-28 [1] CRAN (R 4.2.0)
#>    munsell       0.5.0   2018-06-12 [1] CRAN (R 4.2.0)
#>    pillar        1.8.1   2022-08-19 [1] CRAN (R 4.2.1)
#>    pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.2.0)
#>    proxy         0.4-27  2022-06-09 [1] CRAN (R 4.2.0)
#>    purrr         1.0.0   2022-12-20 [1] CRAN (R 4.2.2)
#>    R.cache       0.16.0  2022-07-21 [1] CRAN (R 4.2.1)
#>    R.methodsS3   1.8.2   2022-06-13 [1] CRAN (R 4.2.0)
#>    R.oo          1.25.0  2022-06-12 [1] CRAN (R 4.2.0)
#>    R.utils       2.12.2  2022-11-11 [1] CRAN (R 4.2.2)
#>    R6            2.5.1   2021-08-19 [1] CRAN (R 4.2.0)
#>    Rcpp          1.0.9   2022-07-08 [1] CRAN (R 4.2.1)
#>    reprex        2.0.2   2022-08-17 [1] CRAN (R 4.2.1)
#>    rlang         1.0.6   2022-09-24 [1] CRAN (R 4.2.1)
#>    rmarkdown     2.19    2022-12-15 [1] CRAN (R 4.2.2)
#>    rstudioapi    0.14    2022-08-22 [1] CRAN (R 4.2.1)
#>    scales        1.2.1   2022-08-20 [1] CRAN (R 4.2.1)
#>    sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.2.0)
#>    sf            1.0-9   2022-11-08 [1] CRAN (R 4.2.2)
#>    stringi       1.7.8   2022-07-11 [1] CRAN (R 4.2.1)
#>    stringr       1.5.0   2022-12-02 [1] CRAN (R 4.2.2)
#>    styler        1.8.1   2022-11-07 [1] CRAN (R 4.2.2)
#>    terra       * 1.6-47  2022-12-02 [1] CRAN (R 4.2.2)
#>    tibble        3.1.8   2022-07-22 [1] CRAN (R 4.2.1)
#>    tidyr         1.2.1   2022-09-08 [1] CRAN (R 4.2.1)
#>    tidyselect    1.2.0   2022-10-10 [1] CRAN (R 4.2.1)
#>    tidyterra   * 0.3.1   2022-11-09 [1] CRAN (R 4.2.2)
#>    units         0.8-1   2022-12-10 [1] CRAN (R 4.2.2)
#>    utf8          1.2.2   2021-07-24 [1] CRAN (R 4.2.0)
#>    vctrs         0.5.1   2022-11-16 [1] CRAN (R 4.2.2)
#>    withr         2.5.0   2022-03-03 [1] CRAN (R 4.2.1)
#>    xfun          0.36    2022-12-21 [1] CRAN (R 4.2.2)
#>    xml2          1.3.3   2021-11-30 [1] CRAN (R 4.2.0)
#>    yaml          2.3.6   2022-10-18 [1] CRAN (R 4.2.1)
#> 
#>  [1] C:/Users/diego/AppData/Local/R/win-library/4.2
#>  [2] C:/Program Files/R/R-4.2.2/library
#> 
#>  D ── DLL MD5 mismatch, broken installation.
#> 
#> ──────────────────────────────────────────────────────────────────────────────