Tidy time series: ‘tsibble’ and ‘feasts’

This post is not to announce something related to my own packages, but to highlight Rob Hyndman’s new packages for working with time-series data using a tidy approach. If you have to deal with time series in R, you should have a look at these packages and read the posts with examples of their use at the Hyndsight blog.

I suspect more is to come soon, but for the time being have a look at what Rob Hyndman wrote yesterday and today in his blog.

 

ggpmisc 0.3.1

After the previous major update to ‘ggpmisc’ (0.3.0), a follow up (0.3.1) with multiple new features and smoothed rough edges for some of the features added in version 3.0.0. Package documentation is available at https://docs.r4photobiology.info/ggpmisc/ as a web site.

The enhancements in this and the previous update to ‘ggpmisc’ are made possible by changes to ‘ggplot2’ (>= 3.0.0) made while adding support for sf (simple features).

The functions in ‘ggpmisc’ (>= 0.3.1) are related to different kinds of annotations and insets in ggplots. Annotations of plots with fitted-model equations, fit diagnosis, ANOVA and summary tables, highlighting and labelling of peaks and valleys in curves, and local density-based highlighting or labelling in scatter plots. Additionally specializations of the ggplot() constructor allow on-the-fly conversion of time-series. Two new geoms join geom_table() added in version 0.3.0, geom_plot() and geom_grob(). These three geoms make it possible to add tables, ggplots and arbitrary graphical objects (grobs) as insets to plots respecting the the Grammar of Graphics paradigm.

Inset plot showing means per group.
Inset grobs

A set of new geometries produce marginal annotations: geom_x_margin_point(), geom_y_margin_point(), geom_x_margin_arrow(), geom_y_margin_arrow(), geom_x_margin_grob() and geom_y_margin_grob().

Marginal “points” showing means per group.

Another novel feature is based on the addition of two new aesthetics npcx and npcy, the corresponding scales scale_npcx_continuous() and scale_npcy_continuous(), and several new geometries that make use of then: geom_text_npc(), geom_label_npc(), geom_table_npc(), geom_plot_npc() and geom_grob_npc(). These allow to position insets and annotations relative to the dimensions of the plotting area instead of using native data units. Using "npc" units is more natural for labels or insets that are not directly related to data but that look better if positioned consistently across multiple panels or multiple separately produced plots. One improvement to the stats from earlier versions of ‘ggpmisc’ is the use of these new geoms to achieve more consistent location for insets and labels.

Quadrant counts with panels.
Fitted equations in panels with free y limits.
Inset tables at different positions in different panels.

As in the announcement of the previous version, I have included some example plots taken from the documentation of the package. In all cases annotations are generated automatically, but formatting is flexible.

NOTE: The new version of ‘ggpmisc’ will be soon submitted to CRAN .

Package ‘patchwork’

Operators defined in package ‘patchwork’ can be used to combine multiple plots created with package ‘ggplot2’ or extensions to it, such as my own ‘ggpmisc’ and ‘ggspectra’. (The site ggplot2 extensions showcases at the moment more than 40 extensions to ‘ggplot2’.)

Package ‘patchwork’ has been developed by Thomas Lin Pedersen, and being relatively new, it is not yet in CRAN. It can be installed from the public Git repository at Github.

# install.packages("devtools")
devtools::install_github("thomasp85/patchwork")

The ‘ggplot2’ package provides a strong API for sequentially building up a plot, but does not concern itself with composition of multiple plots. ‘patchwork’ is a package that expands the API to allow for arbitrarily complex composition of plots by providing mathmatical operators for combining multiple plots. Other packages that address this need (but with a different approach) are ‘gridExtra’ and ‘cowplot’.

From the package’s DESCRIPTION.

Package ‘ggspectra’ provides function multiplot() for this purpose, but this function is minimalistic, with its most important handicap in its inability to independently align the plotting areas and axis labels of the composed plots.

An example using the operators / and & from ‘patchwork’ follows.

library(ggspectra)
library(patchwork)
p <- autoplot(sun.spct) / 
  autoplot(polyester.spct, range = c(280, 800)) / 
  autoplot(sun.spct * polyester.spct)
png("three-plots.png", height = 800, width = 600)
print(p) & theme_bw()
dev.off()