gg.SpatialPolygons: Geom for SpatialPolygons objects

View source: R/ggplot.R

gg.SpatialPolygonsR Documentation

Geom for SpatialPolygons objects

Description

Uses the ggplot2::fortify() function to turn the SpatialPolygons objects into a data.frame. Then calls geom_polygon to plot the polygons. Requires the ggplot2 package.

Usage

## S3 method for class 'SpatialPolygons'
gg(data, mapping = NULL, crs = NULL, ...)

Arguments

data

A SpatialPolygons or SpatialPolygonsDataFrame object.

mapping

Aesthetic mappings created by aes used to update the default mapping. The default mapping is ggplot2::aes(x = long, y = lat, group = group).

crs

A CRS object defining the coordinate system to project the data to before plotting.

...

Arguments passed on to geom_polypath. Unless specified by the user, the arguments colour = "black" (polygon colour) and alpha = 0.2 (Alpha level for polygon filling).

Details

Requires the ggpolypath package to ensure proper plotting, since the ggplot::geom_polygon function doesn't always handle geometries with holes properly.

Value

A ggpolypath::geom_polypath object.

See Also

Other geomes for spatial data: gg.SpatRaster(), gg.SpatialGridDataFrame(), gg.SpatialLines(), gg.SpatialPixelsDataFrame(), gg.SpatialPixels(), gg.SpatialPoints(), gg.sf(), gg(), gm()

Examples


  if (require(ggplot2, quietly = TRUE) &&
      require(ggpolypath, quietly = TRUE) &&
      bru_safe_sp() &&
      require("sp")) {
    # Load Gorilla data

    data("gorillas", package = "inlabru")

    # Plot Gorilla elevation covariate provided as SpatialPixelsDataFrame.
    # The same syntax applies to SpatialGridDataFrame objects.

    ggplot() +
      gg(gorillas$gcov$elevation)

    # Add Gorilla survey boundary and nest sightings

    ggplot() +
      gg(gorillas$gcov$elevation) +
      gg(gorillas$boundary) +
      gg(gorillas$nests)

    # Load pantropical dolphin data

    data("mexdolphin", package = "inlabru")

    # Plot the pantropical survey boundary, ship transects and dolphin sightings

    ggplot() +
      gg(mexdolphin$ppoly) + # survey boundary as SpatialPolygon
      gg(mexdolphin$samplers) + # ship transects as SpatialLines
      gg(mexdolphin$points) # dolphin sightings as SpatialPoints

    # Change color

    ggplot() +
      gg(mexdolphin$ppoly, color = "green") + # survey boundary as SpatialPolygon
      gg(mexdolphin$samplers, color = "red") + # ship transects as SpatialLines
      gg(mexdolphin$points, color = "blue") # dolphin sightings as SpatialPoints


    # Visualize data annotations: line width by segment number

    names(mexdolphin$samplers) # 'seg' holds the segment number
    ggplot() +
      gg(mexdolphin$samplers, aes(color = seg))

    # Visualize data annotations: point size by dolphin group size

    names(mexdolphin$points) # 'size' holds the group size
    ggplot() +
      gg(mexdolphin$points, aes(size = size))
  }


inlabru documentation built on Nov. 2, 2023, 6:07 p.m.