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 columnsvariable,period,year,anomaly, optionallyanomaly_type,unitandlong_name. Also works withcd_extract()output (usesvaluecolumn). 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 whosetrend_onnames the plotted column (anomalyorvalue) 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 notrend_on, orNA, are drawn whatever the plotted column (unlikecd_summary(), which reads them as anomaly trends). The earliesttrend_startis 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)
} # }
