gplot-methods: Use ggplot to plot a Raster* or a SpatRaster object.

gplot-methodsR Documentation

Use ggplot to plot a Raster* or a SpatRaster object.

Description

This function displays Raster* and SpatRaster objects with the ggplot2 system. Note that the function in the rasterVis package is called gplot with a single 'g'.

gplot uses geom_raster to display the data, sets the color map with scale_fill_viridis_c, and sets the coordinate reference system with coord_sf. For multilayer objects, it uses facet_wrap to display a matrix of panels.

Usage

## S4 method for signature 'Raster'
gplot(x, maxpixels=50000, palette = "magma",
show.legend = TRUE, ...) 
## S4 method for signature 'SpatRaster'
gplot(x, maxpixels=50000, palette = "magma",
show.legend = TRUE, ...) 

Arguments

x

A Raster* or SpatRaster object

maxpixels

Maximum number of pixels to use

palette

If character, name of the viridis color map to use. See scale_fill_viridis_c for details. Otherwise, a scale defined by a call to a function of the family scale_fill_*.

show.legend

logical, if TRUE a legend is shown.

...

Additional arguments for ggplot

Author(s)

Robert J. Hijmans and Oscar Perpiñán; based on an example by Paul Hiemstra

See Also

plot, spplot

Examples

 
## Not run: 
library(raster)
library(terra)
library(ggplot2)
library(sf)

theme_set(theme_bw())

r <- raster(system.file("external/test.grd", package="raster"))
r2 <- stack(r, r*2)
names(r2) <- c('meuse', 'meuse x 2')

## With raster
gplot(r)

gplot(r2)

## With terra
s <- rast(r)
s2 <- rast(r2)

gplot(s)

gplot(s2)

## End(Not run)

oscarperpinan/rastervis documentation built on March 28, 2024, 11:27 p.m.