Skip to contents

Creates a bar chart of anomalies over time with optional Theil-Sen trend lines. Positive and negative anomalies are colored differently.

Usage

cd_plot_timeseries(
  x,
  variable = NULL,
  period = "annual",
  trend = NULL,
  title = NULL,
  colors = c(pos = "#d73027", neg = "#4575b4")
)

Arguments

x

A tibble from cd_anomaly() with columns variable, period, year, anomaly, optionally anomaly_type, unit and long_name. Also works with cd_extract() output (uses value column). One row per year in the plotted series.

variable

Character. Which variable to plot. Default uses the first variable in x.

period

Character. Which period to plot. Default "annual".

trend

Optional tibble from cd_trend() to overlay trend lines. Only rows whose trend_on names the plotted column (anomaly or value) are drawn, so a table holding trends on both scales draws only the one that fits the bars, and warns when none does. Rows with no trend_on, or NA, are drawn whatever the plotted column (unlike cd_summary(), which reads them as anomaly trends). The earliest trend_start is drawn dashed, later ones solid.

title

Optional plot title.

colors

Named character vector of length 2 for positive/negative bar colors. Default c(pos = "#d73027", neg = "#4575b4").

Value

A ggplot2::ggplot object.

Details

The y-axis label is long_name and unit from x where present and not NA, otherwise from cd_variables() — resolved by the same rules as cd_summary() (see the input contract in cd_anomaly()) — else the plotted column's name. unit is the anomaly's unit, so on raw value input it is shown only where the anomaly type is absolute or pct_point_diff, whose anomaly unit is the unit of the values. Unlike cd_summary(), a long_name shared by several variables is not suffixed with the variable: the plot shows one variable, so name a station in title.

Examples

if (FALSE) { # \dontrun{
catalog <- cd_catalog()
aoi <- sf::st_read("my_aoi.gpkg")
ts <- cd_extract(catalog, aoi, variables = "tmean", periods = "annual")
bl <- cd_baseline(ts, baseline_years = 1951:1980)
ano <- cd_anomaly(ts, bl)
trn <- cd_trend(ano, trend_start = c(1951, 1981))
cd_plot_timeseries(ano, trend = trn)
} # }