Combining {ggmaps}, {terra} and {tidyterra} to produce raster maps.
Using {tidyverse} methods on images from Stadia Maps and Stamen Maps using {ggmap} by functions from {terra} and {tidyterra}
Background Map
{terra}
Raster Map
Author
Aditya Dahiya
Published
January 7, 2025
Background
……………………………….
Code
# Load spatial and environmental datasets.library(ggmap)# Handle, analyze, and visualize raster and vector data.library(terra)# Handling simple features in Rlibrary(sf)# Tidy data workflows with 'terra' objects.library(tidyterra)# Data manipulation, visualization, and wrangling.library(tidyverse)# Compiling plotslibrary(patchwork)sysfonts::font_add_google("Fira Sans", "body_font")showtext::showtext_auto()theme_set(theme_minimal(base_family ="body_font",base_size =14 ) +theme(plot.title =element_text(size =18 ) ))
Getting a raster map from {ggmap}
Code
# Register your Stadia Maps key and enter it here# register_stadiamaps("you-key-here")# Get basic data on Boroughs of Londonlondon_sf_map <- spData::lnd |> janitor::clean_names() |>select(name, geometry)g1 <- london_sf_map |>ggplot(aes(fill = name)) +geom_sf(colour ="white") +labs(title ="Basic Map of London Boroughs",subtitle ="From {spData} object `lnd`" ) +theme(legend.position ="none" )london_sf_boundary <- london_sf_map |>st_union()g2 <- london_sf_boundary |>ggplot() +geom_sf() +labs(title ="Outer boundary map of London",subtitle ="Using sf::st_union()" )g <- g1 + g2 +plot_annotation(tag_levels ="a" )ggsave(plot = g,filename = here::here("geocomputation", "images","ggmaps_with_tidyterra_1.png"),width =1200,height =600,units ="px")
Getting {ggmap} data on the boroughs of london
Code
# Bounding Box of London Boundary Maplondon_bbox <-st_bbox(london_sf_boundary)# Convert bbox to a format the {ggmap} understandsnames(london_bbox) <-c("left", "bottom", "right", "top")raw_raster_map <-get_stadiamap(bbox = london_bbox,zoom =10,maptype ="stamen_toner_lines")class(raw_raster_map)bbox_raster <- raw_raster_map |> terra::as.raster() |>rast()g1 <-ggmap(raw_raster_map) +labs(x =NULL, y =NULL,title ="{ggmap} raster: `Stamen Toner Lines` Maptype",subtitle ="Using ggmap::ggmap()" )g1ggplot() +geom_spatraster_rgb(data = bbox_raster)ggsave(plot = g,filename = here::here("geocomputation", "images","ggmaps_with_tidyterra_1.png"),width =1200,height =600,units ="px")