library(raster)
library(terra)
f <- system.file("external/test.grd", package="raster")
## raster
r <- raster(f)
levelplot(r)
## terra
rt <- rast(f)
levelplot(rt)
## Change the color theme
levelplot(r, par.settings=GrTheme())
if (FALSE) {
myPal <- RColorBrewer::brewer.pal('Blues', n=9)
myTheme <- rasterTheme(region = myPal)
levelplot(r, par.settings = myTheme)
}
## Define the legend breaks
my.at <- seq(100, 1850, 500)
levelplot(rt, at=my.at)
myColorkey <- list(at=my.at, ## where the colors change
labels=list(
at=my.at ## where to print labels
))
levelplot(r, at=my.at, colorkey=myColorkey)
## Define the units of the color key
levelplot(r, margin = FALSE,
colorkey = list(title = list("[m]",
cex = 1,
fontface = "bold",
col = "red")
))
levelplot(r, margin = FALSE,
colorkey = list(title = "[m]",
space = "left",
title.control = list(side = "bottom")))
## shrink and border color
## raster
rCenter <- (maxValue(r) + minValue(r)) / 2
levelplot(r - rCenter, par.settings=RdBuTheme(), shrink=c(.8, 15), border='black')
if (FALSE) {
## terra
tCenter <- mean(minmax(rt))
levelplot(rt - tCenter, par.settings=RdBuTheme(), shrink=c(.8, 15), border='black')
}
## With subticks
levelplot(r, xscale.components=xscale.raster.subticks,
yscale.components=yscale.raster.subticks)
if (FALSE) {
levelplot(rt, xscale.components=xscale.raster.subticks,
yscale.components=yscale.raster.subticks,
scales=list(x=list(rot=30, cex=0.8)))
}
## log-scale
levelplot(r^2, zscaleLog=TRUE, contour=TRUE)
## Customizing axis and title labels
levelplot(rt, margin=FALSE,
main=list('My plot', col='red'),
xlab=c('This is the', 'X-Axis'),
ylab=list('Y-Axis', rot=30, fontface='bold')
)
## xlim and ylim to display a smaller region
levelplot(r, xlim=c(179000, 181000), ylim=c(329500, 334000))
## RasterStacks
s <- stack(r, r+500, r-500)
levelplot(s, contour=TRUE)
contourplot(s, labels=list(cex=0.4), cuts=12)
if (FALSE) {
## Use of layout
levelplot(s, layout=c(1, 3))
## SpatRaster with several layers
st <- c(rt, rt + 500, rt - 500)
## c() assign the same name for all the layers,
## but each layer needs a unique name for levelplot
set.names(st, c("r0", "rp500", "rm500"))
levelplot(st, contour=TRUE)
contourplot(st, labels=list(cex=0.4), cuts=12)
levelplot(st, layout=c(1, 1)) # useful for animations
}
## names.attr to change the labels of each panel
levelplot(s, names.attr=c('R', 'R + 500', 'R - 500'))
## Defining the scales for the marginal plot
levelplot(r, margin = list(axis = TRUE,
scales = list(x=c(100, 600),
y=c(100, 1000))))
## if a component of the list is null, it is internally calculated
levelplot(rt, margin=list(axis = TRUE, scales = list(x=c(100, 1000))))
## Add a layer of sampling points
## and change the theme
pts <- sampleRandom(r, size=20, sp=TRUE)
## Using +.trellis and layer from latticeExtra
library(latticeExtra)
levelplot(r, par.settings = BTCTheme) +
layer(sp.points(pts, col = 'red'))
contourplot(r, labels = FALSE) +
layer(sp.points(pts, col = 'red'))
## or with a custom panel function
levelplot(r, par.settings=BTCTheme,
panel=function(...){
panel.levelplot(...)
sp.points(pts, col='red')
})
## Categorical data
r <- raster(nrow=10, ncol=10)
r[] = 1
r[51:100] = 3
r[3:6, 1:5] = 5
r <- ratify(r)
rat <- levels(r)[[1]]
rat$landcover <- c('Pine', 'Oak', 'Meadow')
rat$class <- c('A1', 'B2', 'C3')
levels(r) <- rat
r
levelplot(r, col.regions=c('palegreen', 'midnightblue', 'indianred1'))
## with 'att' you can choose another variable from the RAT
levelplot(r, att=2, col.regions=c('palegreen', 'midnightblue', 'indianred1'))
levelplot(r, att='class', col.regions=c('palegreen', 'midnightblue', 'indianred1'))
r2 <- raster(r)
r2[] = 3
r2[51:100] = 1
r2[3:6, 1:5] = 5
r3 <- raster::init(r, function(n)sample(c(1, 3, 5), n, replace=TRUE))
## Multilayer categorical Raster* are displayed only if their RATs are the same
levels(r2) <- levels(r3) <- levels(r)
s <- stack(r, r2, r3)
names(s) <- c('A', 'B', 'C')
levelplot(s)
levelplot(s, att=2)
if (FALSE) {
dataURL <- "https://raw.github.com/oscarperpinan/bookvis/master/data/"
##Solar irradiation data from CMSAF http://dx.doi.org/10.5676/EUM_SAF_CM/RAD_MVIRI/V001
old <- setwd(tempdir())
download.file(paste0(dataURL, "SISmm2008_CMSAF.zip"),
"SISmm2008_CMSAF.zip", method='wget')
unzip("SISmm2008_CMSAF.zip")
listFich <- dir(pattern='\\.nc')
stackSIS <- stack(listFich)
stackSIS <- stackSIS*24 ##from irradiance (W/m2) to irradiation Wh/m2
idx <- seq(as.Date('2008-01-15'), as.Date('2008-12-15'), 'month')
SISmm <- setZ(stackSIS, idx)
names(SISmm) <- month.abb
levelplot(SISmm)
levelplot(SISmm, layers=1, margin = list(FUN = 'median'), contour=TRUE)
setwd(old)
}
Run the code above in your browser using DataLab