plot_climate_data: Plot Climatological Time Series

View source: R/plot-series.R

plot_climate_dataR Documentation

Plot Climatological Time Series

Description

Readily displays annotated climate time series on a common plot with many configurable options.

Usage

plot_climate_data(
  x,
  series,
  start = NULL,
  end = NULL,
  ma = NULL,
  baseline = NULL,
  yearly = FALSE,
  make_yearly_data... = list(),
  ma_sides = 1L,
  interpolate = FALSE,
  plot_type = c("single", "multiple"),
  as_zoo = TRUE,
  type = "l",
  bg = scales::alpha("gray", 0.1),
  xlab = "Year",
  ylab = NULL,
  unit = NULL,
  main = NULL,
  col = NULL,
  col_fun = colorspace::rainbow_hcl,
  col_fun... = list(l = 65),
  alpha = 0.9,
  lwd = 2,
  legend... = list(),
  add = FALSE,
  conf_int = FALSE,
  ci_alpha = 0.3,
  polygon... = list(),
  trend = FALSE,
  trend_lwd = lwd,
  trend_legend_inset = c(0.2, 0.2),
  print_trend_ci = TRUE,
  trend_format = ifelse(print_trend_ci, "1.3f", "1.2f"),
  trend... = list(),
  extra_trends = list(),
  loess = FALSE,
  loess... = list(),
  loess_series = NULL,
  lines.loess... = list(),
  xaxt = "n",
  get_x_axis_ticks... = list(),
  segmented = FALSE,
  segmented... = list(),
  plot.segmented... = list(),
  mark_segments = c("none", "lines", "points"),
  vline... = list(),
  points.segmented... = list(),
  make_standardized_plot_filename... = list(),
  start_callback = NULL,
  end_callback = NULL,
  sign = TRUE,
  sign_callback = rlang::expr(text(graphics::par("usr")[2], graphics::par("usr")[3],
    labels = "@priscian", adj = c(1, -0.5))),
  save_png = FALSE,
  save_png_dir,
  png... = list(),
  ...
)

Arguments

x

A data set (down)loaded by function get_climate_data.

series

A vector containing the column names of the climate series to be plotted. If NULL, all the series in x are plotted.

start, end

Integer values of the starting and ending years of the plot, respectively; if either is given as NULL, the minimum or maximum date in x is used instead.

ma

The size of the ma-month moving average for smoothing the climate series.

baseline

An integer year or, more typically, range of years on which the climate-series anomalies will be centered. If NULL, no baseline centering is done, and the "baseline" attribute of x (if it exists) is used instead.

plot_type

Passed to plot.ts.

type

What type of plot should be drawn; see ?plot for details.

col

A vector of colors, each for plotting a member of series, which is passed to plot.ts and legend.

col_fun

If col = NULL, a color palette for series is generated from the function value of col_fun.

col_fun...

Takes a list of arguments to be passed to function col_fun(), possibly overriding default values.

alpha

Sets the color transparency of the climate series to a new level in [0, 1]. If alpha is NA, existing alpha values are preserved.

lwd

A vector of line widths, each for plotting a member of series, which is passed to plot.ts.

conf_int

Logical; if TRUE, plot 95% confidence intervals for those series with CI data.

ci_alpha

Sets the color transparency of the temperature-series CIs to a new level in [0, 1]. If ci_alpha is NA, existing alpha values are preserved.

...

Passed to plot.ts.

omit_series

A vector containing the column names of the climate series NOT to be plotted if series = NULL. This has been added to leave out the Keeling Curve data, but it could later include other regularly updated time-series data, related to climate, which wouldn't normally be plotted in the same window.

Examples

## Not run: 
inst <- get_climate_data(download=FALSE, baseline=TRUE)

series <- c("GISTEMP Global", "NCEI Global", "HadCRUT4 Global", "Cowtan & Way Krig. Global", "BEST Global (Water Ice Temp.)", "JMA Global", "RSS TLT 3.3 -70.0/82.5", "UAH TLT 6.0 Global", "RATPAC-A 850-300 mb Global")
plot_climate_data(inst, series=series, 1880, ma=12, lwd=2)

########################################
## Plot instrumental series with 95% confidence intervals.
########################################

series <- c("Cowtan & Way Krig. Global", "HadCRUT4 Global")
plot_climate_data(inst, series=series, 1880, ma=12, lwd=2, conf_int=TRUE)

########################################
## Plot instrumental series and trend line.
########################################

series <- c("GISTEMP Global", "NCEI Global", "HadCRUT4 Global", "RSS TLT 3.3 -70.0/82.5", "UAH TLT 6.0 Global")
plot_climate_data(inst, series=series, 1979, ma=12, lwd=2, show_trend=TRUE)

########################################
## Plot piecewise linear trends based on changepoint analysis (similar to Cahill et al. 2015, dx.doi.org/10.1088/1748-9326/10/8/084002).
########################################

## Prepare trend data.
baseline <- TRUE
d <- get_climate_data(download=FALSE, baseline=baseline)
m <- list()
m$series <- c("GISTEMP Global", "NCEI Global", "HadCRUT4 Global")
m$range <- list(start=1880, end=NULL)
m$col <- suppressWarnings(brewer.pal(length(m$series),"Paired"))
length(m$col) <- length(m$series); names(m$col) <- m$series
m$data <- d[d$year >= ifelse(!is.null(m$range$start), m$range$start, -Inf) & d$year <= ifelse(!is.null(m$range$end), m$range$end, Inf), ]
for (s in m$series) {
  m[[s]]$lm <- lm(eval(substitute(b ~ yr_part, list(b=as.symbol(s)))), data=m$data)
  summary(m[[s]]$lm)
  m[[s]]$warming <- coef(m[[s]]$lm)[2L] * diff(range(m[[s]]$lm$model[, 2L]))
  m[[s]]$rate <- coef(m[[s]]$lm)[2L] * 10
  m[[s]]$rateText <- eval(substitute(expression(paste(Delta, "T = ", r, phantom(l) * degree, "C/dec.", sep="")), list(r=sprintf(m[[s]]$rate, fmt="%+1.3f"))))
  m[[s]]$col <- m$col[s]
}
## Plot series.
plot_climate_data(d, m$series, m$range$start, m$range$end, ma=12, lwd=2, col=m$col, baseline=baseline)
## Create changepoint model and plot results.
library(segmented)
changepoints <- 3L
for (s in m$series) {
  sm <- segmented::segmented(m[[s]]$lm, ~ yr_part, NA, seg.control(stop.if.error=TRUE, K=changepoints))
  print(sm)
  plot(sm, add=TRUE, lwd=2, col=m[[s]]$col, rug=FALSE)
}

## End(Not run)


priscian/climeseries documentation built on March 9, 2024, 9:24 p.m.