fortify.Raster: Fortify methods for raster* objects

Description Usage Arguments Author(s) Examples

View source: R/fortify.Raster.R

Description

Fortify methods for raster* objects

Usage

1
fortify.Raster(x, maxPixel = 1e+06)

Arguments

x

raster object

maxPixel

Numeric Maximum number of pixels

Author(s)

Loic Dutrieux

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
library(raster)
library(ggplot2)

r <- raster(nrow = 10, ncol = 20)
r[] <- rnorm(ncell(r))

ggr <- fortify(r)

ggplot() +
    geom_raster(data = ggr, aes(x,y,fill = values)) +
    coord_equal() +
    scale_x_continuous(name = 'Lat', expand=c(0, 0)) +
    scale_y_continuous(name = 'Long', expand=c(0, 0))

# Also works for rasterStackBricks

b <- brick(nrow = 10, ncol = 20, nl = 3)
b[] <- rnorm(ncell(b) * nlayers(b))
names(b) <- c('a', 'b', 'c')
ggb <- fortify(b)

ggplot() +
    geom_raster(data = ggb, aes(x,y,fill = values.a)) +
    coord_equal() +
    scale_x_continuous(name = 'Lat', expand=c(0, 0)) +
    scale_y_continuous(name = 'Long', expand=c(0, 0))

# For facet grid/wrap, you can use tidyr to reshape the dataframe produced by the function
library(tidyr)

ggb2 <- gather(ggb, layer, values, -x, -y)

ggplot() +
    geom_raster(data = ggb2, aes(x,y,fill = values)) +
    coord_equal() +
    scale_x_continuous(name = 'Lat', expand=c(0, 0)) +
    scale_y_continuous(name = 'Long', expand=c(0, 0))+
    facet_wrap(~ layer)

loicdtx/ggSpatial documentation built on May 21, 2019, 7:37 a.m.