Learn R Programming

mapmate (version 0.0.1)

save_ts: Save time series plots

Description

Save a time series plot to disk intended to be part of a as a still image sequence of a growing time series.

Usage

save_ts(data, x, y, i = 1, dir = getwd(), col, xlm, ylm,
  axes.only = FALSE, axes.space = TRUE, suffix = NULL,
  png.args = list(width = 1920, height = 1080, res = 300, bg = "transparent"),
  save.plot = TRUE, return.plot = FALSE, num.format = 4)

Arguments

data

data frame containing the x and y plotting variables.

x

character, the column name in data for the variable plotted along the x axis.

y

character, the column name in data for the variable plotted along the y axis.

i

current time index used to subset x.

dir

png output directory. Defaults to working directory.

col

color of the time series line or the axes lines, ticks, and text.

xlm

x axis limits.

ylm

y axis limits.

axes.only

only plot axis information, no data. Defaults to FALSE.

axes.space

if axes.only=TRUE, leave room for x and y axes in plot window when axes.space=TRUE. Remove this marginal area so that data are plotted over the full canvas when axes.space=FALSE. Defaults to TRUE. Ignored when axes.only=TRUE because of the explicit intent to draw axes.

suffix

character, optional suffix to be pasted onto output filename.

png.args

a list of arguments passed to png.

save.plot

save the plot to disk. Defaults to TRUE. Typically only set to FALSE for demonstrations and testing.

return.plot

return the ggplot object. Defaults to FALSE. Only intended for single-plot demonstrations and testing, not for still image sequence automation.

num.format

number of digits including any leading zeros for image sequence frame numbering. Defaults to 4, i.e. 0001, 0002, ....

Value

usually returns NULL after writing file to disk as a side effect. May return a ggplot object but be careful not to use this option if looping over many plots.

Details

i subsets x to rows with frameID <= i. i defaults to 1, which is sufficient and convenient for the axes_only=TRUE case but not required. Fixed axis limits must be established in advance by computing the max range or other desired range for the x and y variables that are to be plotted.

Examples

Run this code
# NOT RUN {
# not run
# }
# NOT RUN {
library(dplyr)
library(purrr)
data(annualtemps)
temps <- mutate(annualtemps, frameID = Year - min(Year) + 1) %>%
  group_by(Year, frameID) %>% summarise(z=mean(z))
xlm <- range(temps$Year)
ylm <- range(temps$z)
# should specify a dir or set working dir for file output
# consider running over a smaller subset of frame IDs
walk(temps$frameID, ~save_ts(temps, "Year", "z", i=.x, col="blue", xlm, ylm))
# }

Run the code above in your browser using DataLab