Learn R Programming

grainscape (version 0.4.0)

ggGS: Prepare data in MPG and grain objects for use with ggplot2

Description

This is an informal fortify-type method that prepares either RasterLayer or igraph objects contained as slots within MPG or grain objects for easy plotting with ggplot.

Usage

ggGS(x, type = NULL, ...)

# S4 method for RasterLayer ggGS(x, type = NULL, ...)

# S4 method for list ggGS(x, type = NULL, ...)

# S4 method for mpg ggGS(x, type = NULL, ...)

# S4 method for grain ggGS(x, type = NULL, ...)

# S4 method for goc ggGS(x, type = NULL, ...)

Arguments

x

A mpg, grain, or RasterLayer object.

type

If a mpg or grain object is supplied, this gives the name of the slot to prepare for plotting. Options are discussed below. Not required if a RasterLayer is supplied.

...

Additional arguments (not used).

Value

A data.frame suitable for plotting with ggplot.

Where type is a raster the data.frame will have the following columns:

value

the value of the raster cell

x

the x coordinate of the centre of the raster cell

y

the y coordinate of the centre of the raster cell

Where type = 'nodes' the data.frame will have the following columns:

x

the x coordinate of the node

y

the y coordinate of the node

...

other attributes associated with the network nodes

Where type = 'links' the data.frame will have the following columns:

x1

the x coordinate of the first node

y1

the y coordinate of the first node

x2

the x coordinate of the second node

y2

the y coordinate of the second node

x1p

the x coordinate at the perimeter of the first node

y1p

the y coordinate at the perimeter of the first node

x2p

the x coordinate at the perimeter of the second node

y2p

the y coordinate at the perimeter of the second node

...

other attributes associated with the network links

See Also

MPG, GOC

Examples

Run this code
# NOT RUN {
## Load raster landscape
tiny <- raster::raster(system.file("extdata/tiny.asc", package = "grainscape"))

## Create a resistance surface from a raster using an is-becomes reclassification
tinyCost <- raster::reclassify(tiny, rcl = cbind(c(1, 2, 3, 4), c(1, 5, 10, 12)))

## Produce a patch-based MPG where patches are resistance features=1
tinyPatchMPG <- MPG(cost = tinyCost, patch = tinyCost == 1)

## Extract a representative subset of 5 grains of connectivity
tinyPatchGOC <- GOC(tinyPatchMPG, nThresh = 5)

if (interactive()) {
  library(ggplot2)

  ## Plot the patches in a minimum planar graph
  theme_set(theme_grainscape())
  ggplot() +
    geom_tile(data = ggGS(tinyPatchMPG, "patchId"),
                aes(x = x, y = y, fill = value))

  ## Plot the grain polygons in a grain of connectivity
  ggplot() +
    geom_tile(data = ggGS(grain(tinyPatchGOC, 3), "voronoi"),
                aes(x = x, y = y, fill = value))

  ## Plot the grain polygon boundaries
  ggplot() +
    geom_tile(data = ggGS(grain(tinyPatchGOC, 3), "vorBound"),
                aes(x = x, y = y, fill = value))

  ## Plot the patches and perimeter links of a minimum planar graph
  ggplot() +
    geom_tile(data = ggGS(tinyPatchMPG, "patchId"),
                aes(x = x, y = y, fill = value)) +
    geom_segment(data = ggGS(tinyPatchMPG, "links"),
                 aes(x = x1p, y = y1p, xend = x2p, yend = y2p))

  ## Plot the patches and linear representations of the perimeter links
  ## of a minimum planar graph
  ggplot() +
    geom_tile(data = ggGS(tinyPatchMPG, "patchId"),
                 aes(x = x, y = y, fill = value)) +
    geom_segment(data = ggGS(tinyPatchMPG, "links"),
                 aes(x = x1p, y = y1p, xend = x2p, yend = y2p))

  ## Plot the nodes and links of a grains of connectivity network
  ## superimposed over the grain polygons
  focalGrain <- grain(tinyPatchGOC, 3)
  ggplot() +
    geom_tile(data = ggGS(focalGrain, "vorBound"),
                aes(x = x, y = y, fill = value)) +
    geom_point(data = ggGS(focalGrain, "nodes"), aes(x = x, y = y)) +
    geom_segment(data = ggGS(focalGrain, "links"),
                 aes(x = x1, y = y1, xend = x2, yend = y2))
}
# }

Run the code above in your browser using DataLab