ggplot_stats: Create a New ggplot with Statistics

View source: R/Plot_eddy.R

ggplot_statsR Documentation

Create a New ggplot with Statistics

Description

Create a ggplot object representing a scatter plot with statistics for given amount of intervals along x-axis. Each interval contains comparable amount of data points, thus can have unequal width.

Usage

ggplot_stats(
  data,
  x,
  y,
  breaks = 20,
  circular = FALSE,
  ylim = NULL,
  qrange = c(0.005, 0.995),
  center = c("median", "mean"),
  deviation = c("mad", "sd"),
  header = TRUE
)

Arguments

data

A data frame with required timestamp column (data$timestamp) of class "POSIXt".

x, y

A character string. A data column name of the variable to plot on x-axis (y-axis).

breaks

An integer. Number of breakpoints separating variable x to breaks - 1 intervals.

circular

A logical value. Is x a circular variable?

ylim

Either NULL, "band" or a numeric vector of length 2. If NULL (default), y-axis limits are taken from the original data, potentially modified by qrange. If "band", y-axis limits are chosen to fit only the computed uncertainty band. If numeric vector, manually specified limits are applied. In latter two cases qrange is ignored.

qrange

A numeric vector of length 2, giving the quantile range of y-axis.

center, deviation

A character string. Statistics applied to each x-axis interval for computation of its center (deviation) along y-axis.

header

A logical value. Should automated plot title and subtitle be included?

Details

circular = TRUE effectively sets the last interval as neighboring to the first interval. This allows to interpolate the statistics also for the edge cases.

qrange reduces y-axis limits to reduce the impact of outliers on plot readability. It does not affect computed statistics. If you do not want to limit y-axis, set qrange = NULL or qrange = c(0, 1).

See Also

aggregate, as.POSIXlt, cut.POSIXt, mean, regexp, strftime, sum, timezones, varnames

Examples

set.seed(123)
n <- 17520 # number of half-hourly records in one non-leap year
tstamp <- seq(c(ISOdate(2021,3,20)), by = "30 mins", length.out = n)
x <- data.frame(timestamp = tstamp,
                wd = seq(0,360, length.out = n),
                H = rf(n, 1, 2, 1))
openeddy::units(x) <- c("", "deg", "W m-2")
ggplot_stats(x, x = "wd", y = "H", qrange = c(0.005, 0.9))
ggplot_stats(x, x = "wd", y = "H", circular = TRUE, qrange = c(0.005, 0.9))
ggplot_stats(x, x = "wd", y = "H", ylim = "band")


lsigut/openeddy documentation built on Aug. 5, 2023, 12:25 a.m.