| ggGS | R Documentation |
MPG and grain objects for use with ggplot2This 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 ggplot2::ggplot().
ggGS(x, type = NULL, ...)
## S4 method for signature 'RasterLayer'
ggGS(x, type = NULL, ...)
## S4 method for signature 'list'
ggGS(x, type = NULL, ...)
## S4 method for signature 'mpg'
ggGS(x, type = NULL, ...)
## S4 method for signature 'grain'
ggGS(x, type = NULL, ...)
## S4 method for signature 'goc'
ggGS(x, type = NULL, ...)
x |
A |
type |
If a |
... |
Additional arguments (not used). |
A data.frame suitable for plotting with ggplot2::ggplot().
Where type is a raster the data.frame will have the following columns:
valuethe value of the raster cell
xthe x coordinate of the centre of the raster cell
ythe y coordinate of the centre of the raster cell
Where type = 'nodes' the data.frame will have the following columns:
xthe x coordinate of the node
ythe y coordinate of the node
...other attributes associated with the network nodes
Where type = 'links' the data.frame will have the following columns:
x1the x coordinate of the first node
y1the y coordinate of the first node
x2the x coordinate of the second node
y2the y coordinate of the second node
x1pthe x coordinate at the perimeter of the first node
y1pthe y coordinate at the perimeter of the first node
x2pthe x coordinate at the perimeter of the second node
y2pthe y coordinate at the perimeter of the second node
...other attributes associated with the network links
Options for type parameter
If a RasterLayer is supplied type is optional.
For mpg type options are "node" or "links".
This prepares the nodes and links of the minimum planar graph network for
plotting, Also "patchId", "voronoi", "lcpPerimWeight",
"lcpLinkId", "mpgPlot" will prepare rasters for plotting.
For grain objects type options are "nodes" or"links"
to prepare the nodes and links of the grains of connectivity network for
plotting. Also "voronoi" will prepare the grains of connectivity
Voronoi polygons raster for plotting.
For either mpg or grain objects type = "vorBound"
will identify the boundaries of the Voronoi polygons for plotting.
This is potentially time consuming for large rasters.
Paul Galpern and Alex Chubaty
MPG(), GOC()
## 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))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.