R/plotSummary.R

Defines functions plotSummary

Documented in plotSummary

#' Summary plots of an animal's tracked path
#'
#' This function produces a summary plot of cumulative distance and velocity over time, and rose plots of absolute and relative bearings, based on a list object generated by the \code{trackPath} function.
#' @param path.list a list object created by the \code{trackPath} function
#' @details \code{plotSummary} uses \code{ggplot2} and \code{gridExtra} to produce a four panel summary plot.
#' @return A four panel summary plot.
#' @import ggplot2
#' @importFrom gridExtra grid.arrange
#' @export
plotSummary = function(path.list) {

  dat = as.data.frame(path.list$movement)
  cumDistance = cumsum(ifelse(is.na(dat$distance), 0, dat$distance)) + dat$distance * 0

  plot1 = ggplot(dat, aes(x = time, y = cumDistance)) +
    geom_line(color = "grey25") +
    theme_bw() +
    xlab("") +
    ylab("Distance (mm)") +
    ggtitle("Cumulative distance")

  plot2 = ggplot(dat, aes(x = time, y = velocity)) +
    geom_line(color = "grey85") +
    geom_line(aes(x = time, y = movingAverage(velocity, n = 13, centered = TRUE)), color = "grey25") +
    theme_bw() +
    xlab("Time (s)") +
    ylab("Velocity (mm/s)") +
    ggtitle("Velocity")

  dat = subset(dat, distance != 0)

  plot3 = ggplot(dat, aes(x = abs.angle)) +
    geom_histogram(aes(y = ..count../sum(..count..)), breaks = seq(0, 360, 15)) +
    coord_polar(start = 0) +
    theme_bw() +
    scale_fill_brewer() +
    ylab("Proportion") +
    ggtitle("Absolute bearing") +
    scale_x_continuous("", limits = c(0, 360), breaks = seq(0, 359, 45), labels = seq(0, 359, 45))

  plot4 = ggplot(dat, aes(x = rel.angle)) +
    geom_histogram(aes(y = ..count../sum(..count..)), breaks = seq(-180, 180, 15)) +
    coord_polar(start = pi) +
    theme_bw() +
    scale_fill_brewer() +
    ylab("Proportion") +
    ggtitle("Relative turn direction") +
    scale_x_continuous("", limits = c(-180, 180), breaks = seq(-180, 180, 45), labels = seq(-180, 180, 45))

  grid.arrange(plot1, plot3, plot2, plot4, ncol = 2)

}
aharmer/pathtrackr documentation built on June 3, 2023, 1:29 p.m.