Skip to contents

This article shows how to plot the results of 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 rasters:

With tmap

tmap can be also used to create great maps:

With mapsf

mapsf, the replacement of cartography, 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 rasters:


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)