Search code examples
rggplot2terra

How to add basemaps to spatraster plot with ggplot 2?


I want to add this basemap using to raster using ggplot2 and terra packages and I am stuck. Can I have some help figuring this out?

library(basemaps)
library(terra)
Library(rasterVis)

set_defaults(Yiel_total , map_service = "esri" , map_type = "world_hillshade")
x <- basemap_magic(Yiel_total , map_service = "esri" , map_type = "world_hillshade")
#Yiel_total was used for the extent 

enter image description here

SpatRaster Data for Yiel_total

The ggplot code:

Yield <-rast("Yield_total")
Yield_Tot_6 <- gplot(Yield ) + geom_tile(aes(fill = value)) + ggtitle("")+
  theme(panel.spacing.x=unit(0,"lines"), axis.title.x = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(),
        axis.title.y = element_blank(),plot.title = element_text(hjust = 0.5, size = 18, face = "bold"),strip.background = element_blank(),
        strip.text = element_text(size = 14, color = "black", face = "bold"),legend.text = element_text(size = 14, face = "bold"),
        legend.title = element_text(hjust = 0.1, size = 16, face = "bold"), panel.background = element_blank()) +
  guides(fill = guide_colourbar(title = "Relative yield",title.hjust = 0.5, barheight = 15, barwidth = 2))+ 
  #basemap_magic(Yield_0.5_6, map_service = "esri" , map_type = "world_hillshade")+
  
  facet_wrap(~ variable, ncol = 3 ) +
  scale_fill_gradient(limits = c(0.01,0.88),low = 'red', high = 'dark green', na.value = "grey93", breaks = c(0.02,0.20, 0.40, 0.60,0.75, 0.87))

Yield_Tot_6

enter image description here


Solution

  • You can use tidyterra to work easily with ggplot2 and SpatRasters. I modified the call to basemap_raster and converted it also to SpatRaster:

    library(basemaps)
    library(terra)
    
    Yiel_total <- rast("Yiel_total.tif")
    
    
    set_defaults(Yiel_total, map_service = "esri", map_type = "world_hillshade")
    x <- basemap_raster(Yiel_total, map_service = "esri", map_type = "world_hillshade")
    x_terr <- rast(x)
    
    
    library(ggplot2)
    library(tidyterra)
    
    ggplot() +
      geom_spatraster_rgb(data = x_terr) +
      geom_spatraster(data = Yiel_total) +
      # Faceting with tidyterra
      facet_wrap(~lyr, ncol = 3) +
      scale_fill_gradient(
        limits = c(0.01, 0.88), low = "red", high = "dark green",
        na.value = NA, breaks = c(0.02, 0.20, 0.40, 0.60, 0.75, 0.87)
      ) +
      ggtitle("") +
      theme(
        panel.spacing.x = unit(0, "lines"), axis.title.x = element_blank(),
        axis.text = element_blank(), axis.ticks = element_blank(),
        axis.title.y = element_blank(), plot.title = element_text(
          hjust = 0.5,
          size = 18, face = "bold"
        ), strip.background = element_blank(),
        strip.text = element_text(size = 14, color = "black", face = "bold"),
        legend.text = element_text(size = 14, face = "bold"),
        legend.title = element_text(hjust = 0.1, size = 16, face = "bold"), 
        panel.background = element_blank()
      ) +
      guides(fill = guide_colourbar(
        title = "Relative yield", title.hjust = 0.5,
        barheight = 15, barwidth = 2
      ))
    
    

    enter image description here