Skip to contents

Extracts start and end points of the lines and calls geom_segment to plot lines between them. Requires the ggplot2 package.

Usage

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

Arguments

data

A SpatialLines or SpatialLinesDataFrame object.

mapping

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

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])]])

crs

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

...

Arguments passed on to ggplot2::geom_segment.

Value

A `geom_segment“ return value.

Examples

# \donttest{
  if (require("ggplot2", quietly = TRUE) &&
      requireNamespace("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 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))
  }

# }