% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ggplot.R
\name{gg.Spatial}
\alias{gg.Spatial}
\alias{gg.SpatialPoints}
\alias{gg.SpatialLines}
\alias{gg.SpatialPolygons}
\alias{gg.SpatialGridDataFrame}
\alias{gg.SpatialPixelsDataFrame}
\alias{gg.SpatialPixels}
\title{Geoms for sp Spatial objects}
\usage{
\method{gg}{SpatialPoints}(data, mapping = NULL, crs = NULL, ...)

\method{gg}{SpatialLines}(data, mapping = NULL, crs = NULL, ...)

\method{gg}{SpatialPolygons}(data, mapping = NULL, crs = NULL, ...)

\method{gg}{SpatialGridDataFrame}(data, ...)

\method{gg}{SpatialPixelsDataFrame}(data, mapping = NULL, crs = NULL, mask = NULL, ...)

\method{gg}{SpatialPixels}(data, ...)
}
\arguments{
\item{data}{A \verb{Spatial*} object.}

\item{mapping}{Aesthetic mappings created by \code{aes} used to update the default
mapping. Uness specified otherwise below, the default mapping is

\if{html}{\out{<div class="sourceCode r">}}\preformatted{ggplot2::aes(
  x = .data[[sp::coordnames(data)[1]]],
  y = .data[[sp::coordnames(data)[2]]]
)
}\if{html}{\out{</div>}}}

\item{crs}{A \code{sp::CRS} object defining the coordinate system to project the
data to before plotting.}

\item{\dots}{Arguments passed on to \verb{geom_*}.}

\item{mask}{A \code{sp::SpatialPolygons} object defining the region that is
plotted.}
}
\value{
A \code{geom_point}, \code{geom_segment}, \code{geom_sf}, \code{geom_tile},
or a list of \code{ggplot} geomes
}
\description{
Methods for plotting sp spatial objects with \code{ggplot2}.
}
\section{Functions}{
\itemize{
\item \code{gg(SpatialPoints)}: Geom for \code{SpatialPoints} objects.
This function coerces the \code{SpatialPoints} into a \code{data.frame} and uses
\code{geom_point} to plot the points. Requires the \code{ggplot2} package.

\item \code{gg(SpatialLines)}: Geom for SpatialLines objects.

Extracts start and end points of the lines and calls \code{geom_segment} to plot
lines between them.

\code{mapping}: Aesthetic mappings created by \code{ggplot2::aes} or
\code{ggplot2::aes_} used to update the default mapping. The default mapping is

\if{html}{\out{<div class="sourceCode r">}}\preformatted{ggplot2::aes(
  x = .data[[sp::coordnames(data)[1]]],
  y = .data[[sp::coordnames(data)[2]]],
  xend = .data[[paste0("end.", sp::coordnames(data)[1])]],
  yend = .data[[paste0("end.", sp::coordnames(data)[2])]])
}\if{html}{\out{</div>}}

\item \code{gg(SpatialPolygons)}: Geom for SpatialPolygons objects.
Uses the \code{ggplot2::fortify()} function to turn the \code{SpatialPolygons} objects
into a \code{data.frame}. Then calls \code{geom_polygon} to plot the polygons.

Unless specified by the user,
the argument \code{alpha = 0.2} (alpha level for polygon filling) is added.

Up to version \verb{2.10.0}, the \code{ggpolypath} package was used to ensure proper
plotting for polygons, since the \code{ggplot2::geom_polygon} function doesn't
always handle geometries with holes properly. After \verb{2.10.0}, the object is
converted to \code{sf} format and passed on to \code{\link[=gg.sf]{gg.sf()}} instead, as \code{ggplot2}
version \verb{3.4.4} deprecated the internally used \code{ggplot2::fortify()} method
for \code{SpatialPolygons/DataFrame} objects.

\item \code{gg(SpatialGridDataFrame)}: Geom for SpatialGridDataFrame objects

Coerces input \code{SpatialGridDataFrame} to \code{SpatialPixelsDataFrame} and calls
\code{\link[=gg.SpatialPixelsDataFrame]{gg.SpatialPixelsDataFrame()}} to plot it.

\item \code{gg(SpatialPixelsDataFrame)}: Geom for SpatialPixelsDataFrame objects.

Coerces \code{SpatialPixelsDataFrame} input to \code{data.frame} and uses
\code{geom_tile} to plot it.

\code{mapping}: Aesthetic mappings created by \code{aes} used to update the default
mapping. The default mapping is

\if{html}{\out{<div class="sourceCode r">}}\preformatted{ggplot2::aes(
  x = .data[[sp::coordnames(data)[1]]],
  y = .data[[sp::coordnames(data)[2]]],
  fill = .data[[names(data)[[1]]]]
)
}\if{html}{\out{</div>}}

\item \code{gg(SpatialPixels)}: Geom for SpatialPixels objects

Converts the input to \code{SpatialPoints} and calls
[gg.SpatialPoints()` to plot it.

}}
\examples{
\donttest{
if (require("ggplot2", quietly = TRUE) &&
  bru_safe_terra(quietly = TRUE) &&
  bru_safe_sp() &&
  require("sp")) {
  # Load Gorilla data

  gorillas <- inlabru::gorillas_sf

  gcov <- gorillas_sf_gcov()
  elev <- terra::as.data.frame(gcov$elevation, xy = TRUE)
  elev <- sf::as_Spatial(sf::st_as_sf(elev, coords = c("x", "y")))

  # Turn elevation covariate into SpatialGridDataFrame
  elev <- sp::SpatialPixelsDataFrame(elev, data = as.data.frame(elev))

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

  ggplot() +
    gg(elev)

  # Add Gorilla survey boundary and nest sightings

  ggplot() +
    gg(elev) +
    gg(gorillas$boundary, alpha = 0.0, col = "red") +
    gg(gorillas$nests)

  # Load pantropical dolphin data

  mexdolphin <- inlabru::mexdolphin_sp()

  # 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; SpatialPolygon
    gg(mexdolphin$samplers, color = "red") + # ship transects; SpatialLines
    gg(mexdolphin$points, color = "blue") # dolphin sightings; 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))
}
}

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

  gcov <- gorillas_sf_gcov()
  elev <- terra::as.data.frame(gcov$elevation, xy = TRUE)
  pxl <- sf::as_Spatial(sf::st_as_sf(elev, coords = c("x", "y")))

  # Turn elevation covariate into SpatialPixels
  pxl <- sp::SpatialPixels(pxl)

  # Plot the pixel centers
  ggplot() +
    gg(pxl, size = 0.1)
}

}
\seealso{
Other geomes: 
\code{\link{gg}()},
\code{\link{gg.RasterLayer}()},
\code{\link{gg.SpatRaster}()},
\code{\link{gg.data.frame}()},
\code{\link{gg.fm_mesh_1d}()},
\code{\link{gg.fm_mesh_2d}()},
\code{\link{gg.matrix}()},
\code{\link{gg.sf}()}
}
\concept{geomes}
