Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

sts_ggplot

Time-Series Plots for "sts" Objects Using ggplot2


Description

A simple ggplot2 variant of stsplot_time, based on a “tidy” version of the "sts" object via tidy.sts. It uses a date axis and thus only works for time series indexed by dates or with a standard frequency (daily, weekly, or monthly).

Usage

autoplot.sts(object, population = FALSE, units = NULL,
             as.one = FALSE, scales = "fixed", width = NULL, ...)

Arguments

object

an object of class "sts".

population

logical indicating whether observed(object) should be divided by population(object). The population argument can also be a scalar, which is used to scale the denominator population(object), i.e., observed(object) is divided by population(object) / population. For instance, if population(object) contains raw population numbers, population = 1000 could be used to plot the incidence per 1000 inhabitants.

units

optional integer or character vector to select the units (=columns of object) to plot. The default (NULL) is to plot all time series.

as.one

logical indicating if all time series should be plotted in one panel with geom_line. By default, the time series are plotted in separate panels (using geom_col).

scales

passed to facet_wrap (for as.one=FALSE). By default, all panels use a common ylim (and xlim).

width

bar width, passed to geom_col. Defaults to 7 for weekly time series.

...

unused (argument of the generic).

Value

a "ggplot" object.

Author(s)

Sebastian Meyer

See Also

stsplot_time for the traditional plots.

Examples

## compare traditional plot() with ggplot2-based autoplot.sts()
if (requireNamespace("ggplot2")) {
    data("measlesDE")
    plot(measlesDE)
    autoplot.sts(measlesDE)
}

## weekly incidence: population(measlesDE) gives population fractions,
## which we need to multiply by the total population
if (surveillance.options("allExamples") && require("ggplot2")) {
    autoplot.sts(measlesDE, population = 1000000/82314906) +
        ylab("Weekly incidence [per 1'000'000 inhabitants]")
}

surveillance

Temporal and Spatio-Temporal Modeling and Monitoring of Epidemic Phenomena

v1.19.1
GPL-2
Authors
Michael H<f6>hle [aut, ths] (<https://orcid.org/0000-0002-0423-6702>), Sebastian Meyer [aut, cre] (<https://orcid.org/0000-0002-1791-9449>), Michaela Paul [aut], Leonhard Held [ctb, ths], Howard Burkom [ctb], Thais Correa [ctb], Mathias Hofmann [ctb], Christian Lang [ctb], Juliane Manitz [ctb], Andrea Riebler [ctb], Daniel Saban<e9>s Bov<e9> [ctb], Ma<eb>lle Salmon [ctb], Dirk Schumacher [ctb], Stefan Steiner [ctb], Mikko Virtanen [ctb], Wei Wei [ctb], Valentin Wimmer [ctb], R Core Team [ctb] (A few code segments are modified versions of code from base R)
Initial release
2021-03-30

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.