Skip to contents

This article shows how to plot the SpatRaster produced by rasterpic_img() with several packages.

Base plots

The most straightforward option is to use the base plot() methods provided by the package terra (terra::plotRGB()):

library(rasterpic)
library(terra)

# The flag of the United Kingdom
img <- system.file("img/UK_flag.png",
  package = "rasterpic"
)

uk <- sf::st_read(
  system.file("gpkg/UK.gpkg",
    package = "rasterpic"
  ),
  quiet = TRUE
)

uk_img <- rasterpic_img(uk, img, mask = TRUE, inverse = TRUE)
plotRGB(uk_img)

With ggplot2 + tidyterra

tidyterra provides full support for terra SpatRaster objects:

With tmap

tmap can be also used to create great maps:

With mapsf

mapsf also provides this functionality:

library(mapsf)

mf_raster(uk_img)
mf_scale()
mf_inset_on(x = "worldmap", pos = "topright")
mf_worldmap(uk)
mf_inset_off()

With maptiles

maptiles is a interesting package that provides the ability of downloading map tiles from different providers. It also has a specific function for plotting terra SpatRaster objects:

library(maptiles)

other_tile <- get_tiles(uk, crop = TRUE, zoom = 6)

other_tile_crop <- terra::crop(other_tile, uk_img)

plot_tiles(other_tile_crop)
plot_tiles(uk_img, add = TRUE)

References

Tennekes M (2018). “tmap: Thematic Maps in R.” Journal of Statistical Software, 84(6), 1–39. doi:10.18637/jss.v084.i06.

Giraud T (2024). mapsf: Thematic Cartography. R package version 0.9.0, https://riatelab.github.io/mapsf/.

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

Hijmans R (2024). terra: Spatial Data Analysis. R package version 1.7-71, https://rspatial.github.io/terra/, https://rspatial.org/.

Wickham H (2016). ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. ISBN 978-3-319-24277-4, https://ggplot2.tidyverse.org.

Session info

Details
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.3 (2024-02-29 ucrt)
#>  os       Windows Server 2022 x64 (build 20348)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language en
#>  collate  English_United States.utf8
#>  ctype    English_United States.utf8
#>  tz       UTC
#>  date     2024-04-19
#>  pandoc   3.1.11 @ C:/HOSTED~1/windows/pandoc/31F387~1.11/x64/PANDOC~1.11/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package      * version     date (UTC) lib source
#>  abind          1.4-5       2016-07-21 [1] RSPM
#>  base64enc      0.1-3       2015-07-28 [1] RSPM
#>  bslib          0.7.0       2024-03-29 [1] RSPM
#>  cachem         1.0.8       2023-05-01 [1] RSPM
#>  class          7.3-22      2023-05-03 [3] CRAN (R 4.3.3)
#>  classInt       0.4-10      2023-09-05 [1] RSPM
#>  cli            3.6.2       2023-12-11 [1] RSPM
#>  codetools      0.2-19      2023-02-01 [3] CRAN (R 4.3.3)
#>  colorspace     2.1-0       2023-01-23 [1] RSPM
#>  crosstalk      1.2.1       2023-11-23 [1] RSPM
#>  curl           5.2.1       2024-03-01 [1] RSPM
#>  DBI            1.2.2       2024-02-16 [1] RSPM
#>  desc           1.4.3       2023-12-10 [1] RSPM
#>  dichromat      2.0-0.1     2022-05-02 [1] RSPM
#>  digest         0.6.35      2024-03-11 [1] RSPM
#>  dplyr          1.1.4       2023-11-17 [1] RSPM
#>  e1071          1.7-14      2023-12-06 [1] RSPM
#>  evaluate       0.23        2023-11-01 [1] RSPM
#>  fansi          1.0.6       2023-12-08 [1] RSPM
#>  farver         2.1.1       2022-07-06 [1] RSPM
#>  fastmap        1.1.1       2023-02-24 [1] RSPM
#>  fs             1.6.3       2023-07-20 [1] RSPM
#>  generics       0.1.3       2022-07-05 [1] RSPM
#>  ggplot2      * 3.5.0       2024-02-23 [1] RSPM
#>  glue           1.7.0       2024-01-09 [1] RSPM
#>  gtable         0.3.4       2023-08-21 [1] RSPM
#>  highr          0.10        2022-12-22 [1] RSPM
#>  htmltools      0.5.8.1     2024-04-04 [1] RSPM
#>  htmlwidgets    1.6.4       2023-12-06 [1] RSPM
#>  jquerylib      0.1.4       2021-04-26 [1] RSPM
#>  jsonlite       1.8.8       2023-12-04 [1] RSPM
#>  KernSmooth     2.23-22     2023-07-10 [3] CRAN (R 4.3.3)
#>  knitr          1.46        2024-04-06 [1] RSPM
#>  lattice        0.22-5      2023-10-24 [3] CRAN (R 4.3.3)
#>  leafem         0.2.3       2023-09-17 [1] RSPM
#>  leaflet        2.2.2       2024-03-26 [1] RSPM
#>  leafsync       0.1.0       2019-03-05 [1] RSPM
#>  lifecycle      1.0.4       2023-11-07 [1] RSPM
#>  lwgeom         0.2-14      2024-02-21 [1] RSPM
#>  magrittr       2.0.3       2022-03-30 [1] RSPM
#>  maplegend      0.1.0       2023-10-18 [1] RSPM
#>  mapsf        * 0.9.0       2024-01-26 [1] RSPM
#>  maptiles     * 0.7.0       2024-01-11 [1] RSPM
#>  memoise        2.0.1       2021-11-26 [1] RSPM
#>  munsell        0.5.1       2024-04-01 [1] RSPM
#>  pillar         1.9.0       2023-03-22 [1] RSPM
#>  pkgconfig      2.0.3       2019-09-22 [1] RSPM
#>  pkgdown        2.0.8       2024-04-10 [1] any (@2.0.8)
#>  png            0.1-8       2022-11-29 [1] RSPM
#>  proxy          0.4-27      2022-06-09 [1] RSPM
#>  purrr          1.0.2       2023-08-10 [1] RSPM
#>  R6             2.5.1       2021-08-19 [1] RSPM
#>  ragg           1.3.0       2024-03-13 [1] RSPM
#>  raster         3.6-26      2023-10-14 [1] RSPM
#>  rasterpic    * 0.2.5       2024-04-19 [1] local
#>  RColorBrewer   1.1-3       2022-04-03 [1] RSPM
#>  Rcpp           1.0.12      2024-01-09 [1] RSPM
#>  rlang          1.1.3       2024-01-10 [1] RSPM
#>  rmarkdown      2.26        2024-03-05 [1] RSPM
#>  s2             1.1.6       2023-12-19 [1] RSPM
#>  sass           0.4.9       2024-03-15 [1] RSPM
#>  scales         1.3.0       2023-11-28 [1] RSPM
#>  sessioninfo  * 1.2.2       2021-12-06 [1] any (@1.2.2)
#>  sf             1.0-16      2024-03-24 [1] RSPM
#>  slippymath     0.3.1       2019-06-28 [1] RSPM
#>  sp             2.1-3       2024-01-30 [1] RSPM
#>  stars          0.6-5       2024-04-04 [1] RSPM
#>  systemfonts    1.0.6       2024-03-07 [1] RSPM
#>  terra        * 1.7-71      2024-01-31 [1] RSPM
#>  textshaping    0.3.7       2023-10-09 [1] RSPM
#>  tibble         3.2.1       2023-03-20 [1] RSPM
#>  tidyr          1.3.1       2024-01-24 [1] RSPM
#>  tidyselect     1.2.1       2024-03-11 [1] RSPM
#>  tidyterra    * 0.5.2       2024-01-19 [1] RSPM
#>  tmap         * 3.3-4       2023-09-12 [1] RSPM
#>  tmaptools      3.1-1       2021-01-19 [1] RSPM
#>  units          0.8-5       2023-11-28 [1] RSPM
#>  utf8           1.2.4       2023-10-22 [1] RSPM
#>  vctrs          0.6.5       2023-12-01 [1] RSPM
#>  viridisLite    0.4.2       2023-05-02 [1] RSPM
#>  withr          3.0.0       2024-01-16 [1] RSPM
#>  wk             0.9.1       2023-11-29 [1] RSPM
#>  xfun           0.43        2024-03-25 [1] RSPM
#>  XML            3.99-0.16.1 2024-01-22 [1] RSPM
#>  yaml           2.3.8       2023-12-11 [1] RSPM
#> 
#>  [1] D:/a/_temp/Library
#>  [2] C:/R/site-library
#>  [3] C:/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────