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
CodePudding user response:
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
))