Reputation: 131
I have a raster stack that I want to plot in rasterVis:levelplot (only because there seems no simple alternative like using ggplot). However, I fail to increase the size of the text.
Ideally I would like to assign names to individual "layers" before plotting (see code below). However, this doesn't seem to allow me to change the text size. If I create an object with the layer names, the default names are still plotted (and it also replaces spaces with full stops.)
## Load libraries
library(raster)
library(rasterVis)
## make raster 1
r1 = raster(ext=extent(c(-69.5, -56, -56, -49)), res=c(0.5, 0.5)) #lat/long xmin, xmax, ymin, ymax # global fishing watch data
r1[] <- c(1,3,4,5)
projection(r1) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
crs(r1) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
## make raster 2
r2 = raster(ext=extent(c(-69.5, -56, -56, -49)), res=c(0.5, 0.5)) #lat/long xmin, xmax, ymin, ymax # global fishing watch data
r2[] <- c(2,5,7,8)
projection(r1) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
crs(r1) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
## make rasterStack
s <- stack(r1, r2)
## assign names to each raster layer
names(s) <- c("My raster 1", "My raster 2")
## plot rasterStack with rasterVis::levelplots
col <- rev(heat.colors(999))
p <- levelplot(s, col.regions=col, main=list(cex=0.5))
p
## increase size of the headings
p <- levelplot(s, col.regions=col, main=list(cex=2))
p
# no change...
## try this alternative
raster.names <- c("My raster 1", "My raster 2")
p <- levelplot(s, col.regions=col, main=list(raster.names, cex=2))
p
# nope... :-(
Upvotes: 0
Views: 658
Reputation: 3402
As you mentioned:
only because there seems no simple alternative like using ggplot
There is an alternative using tidyterra to work with ggplot2, but before you would need to convert your "Raster*" to an "SpatRaster" object with terra:
## Load libraries
library(raster)
#> Loading required package: sp
## make raster 1
r1 = raster(ext=extent(c(-69.5, -56, -56, -49)), res=c(0.5, 0.5)) #lat/long xmin, xmax, ymin, ymax # global fishing watch data
r1[] <- c(1,3,4,5)
projection(r1) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
crs(r1) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
## make raster 2
r2 = raster(ext=extent(c(-69.5, -56, -56, -49)), res=c(0.5, 0.5)) #lat/long xmin, xmax, ymin, ymax # global fishing watch data
r2[] <- c(2,5,7,8)
projection(r1) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
crs(r1) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
## make rasterStack
s <- stack(r1, r2)
## assign names to each raster layer
names(s) <- c("My raster 1", "My raster 2")
col <- rev(heat.colors(999))
# With tidyterra
library(tidyterra)
library(ggplot2)
s_terra <- terra::rast(s)
ggplot() +
geom_spatraster(data = s_terra) +
facet_wrap(~lyr) +
scale_fill_gradientn(colors = col, na.value = NA) +
# Control size
theme(strip.text = element_text(size = 20))
Created on 2022-07-26 by the reprex package (v2.0.1)
Upvotes: 1
Reputation: 8188
You can use the following code
## Load libraries
library(raster)
library(rasterVis)
## make raster 1
r1 = raster(ext=extent(c(-69.5, -56, -56, -49)), res=c(0.5, 0.5)) #lat/long xmin, xmax, ymin, ymax # global fishing watch data
r1[] <- c(1,3,4,5)
projection(r1) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
crs(r1) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
## make raster 2
r2 = raster(ext=extent(c(-69.5, -56, -56, -49)), res=c(0.5, 0.5)) #lat/long xmin, xmax, ymin, ymax # global fishing watch data
r2[] <- c(2,5,7,8)
projection(r1) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
crs(r1) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
## make rasterStack
s <- stack(r1, r2)
## plot rasterStack with rasterVis::levelplots
col <- rev(heat.colors(999))
raster.names <- c("My raster 1", "My raster 2")
p.strip <- list(cex=1.5, lines=1)
levelplot(s, col.regions=col, names.attr=raster.names, par.strip.text=p.strip)
Upvotes: 1