ggspatial (version 0.2.1)

fortify.Raster: Turn a Raster into a data.frame

Description

Like others in the fortify family, this method coerces its input into a data.frame. Each band in the raster is a column in the data frame, alongside x and y coordinate columns. This is the format required for input to geom_raster, such that a Raster object can be passed directly to geom_raster without a conversion function. Band columns are named band1, band2, band3, etc., for use in creating a mapping.

Usage

# S3 method for Raster
fortify(model, data = NULL, format = c("wide", "long"),
  ...)

Arguments

model

A Raster object

data

Unused

format

Use 'long' to get values in a single column; otherwise values are in one column per band.

...

Not used by this method

Value

A data.frame with columns, x and y as coordinates in the projection of the Raster,

Examples

Run this code
# NOT RUN {
# use with ggplot()
df <- fortify(longlake_osm)
ggplot(df, aes(x, y, fill = band1)) + geom_raster() +
  coord_fixed()
# }
# NOT RUN {
# identical usage with ggraster()
ggraster(longlake_osm, aes(fill = band1))

# use long format to facet by band
dflong <- fortify(longlake_osm, format = "long")
ggplot(dflong, aes(x, y, fill = value)) +
  geom_raster() + facet_wrap(~band) +
  coord_fixed()


# can use on other raster types as well
x <- rosm::osm.raster("gatineau qc")
ggraster(x, aes(fill = band1))
df <- fortify(x, format = "long")
ggplot(df, aes(x, y, fill = value)) +
  geom_raster() + facet_wrap(~band) +
  coord_fixed()
# }
# NOT RUN {
# }

Run the code above in your browser using DataCamp Workspace