Skip to content

Add tsggplot(): ggplot2 and plotly versions of tsplot() - #301

Open
TimothyMerlin wants to merge 2 commits into
KOF-ch:mainfrom
TimothyMerlin:tsggplot-upstream
Open

TimothyMerlin wants to merge 2 commits into
KOF-ch:mainfrom
TimothyMerlin:tsggplot-upstream

Conversation

@TimothyMerlin

Copy link
Copy Markdown
Contributor

Summary

This adds a ggplot2 version of tsplot() to tstools, along with an interactive plotly version.

  • tsggplot() takes the same inputs as tsplot() and uses the same defaults: legends, dual y-axes (tsr), stacked bars with sum_as_line, bands, confidence intervals, highlight windows and fill_year_with_nas. The difference is that it returns a regular ggplot object, so users can add their own layers, scales and themes. It accepts ts, mts, xts, zoo and lists of these, at monthly, quarterly, yearly, weekly, daily and hourly frequencies.
  • tsggplotly() turns a tsggplot() chart into an interactive Plotly widget. It keeps the static theme and the secondary y-axis, and adds hover text.
  • init_tsggplot_theme() is the theme for both. It is based on init_tsplot_theme(), so the defaults match.

Existing functions are unchanged. The diff to tsplot.R and import_helpers.R only removes blank lines (from styler).

Dependencies

Package Field Why
ggplot2 Imports tsggplot() builds and returns a ggplot object
ggnewscale Imports separate colour/fill scales for the left/right legends and for each confidence interval; it only depends on ggplot2
grid, grDevices Imports part of base R
plotly Suggests only tsggplotly() needs it, and it stops with an install hint when plotly is missing

plotly is optional on purpose: it pulls in about 46 packages beyond what ggplot2 needs. ggplot2 itself adds 17 packages to what tstools already installs. We kept it in Imports because the whole point of tsggplot() is to return a ggplot object. If you would rather keep tstools lighter, we can also move ggplot2 to Suggests or split tsggplot into its own package.

Also in this PR

  • A new vignette, tstools with ggplot2, and tests for the new functions and helpers.
  • NEWS entries under "Changes in development version". The version is bumped to 0.4.4.9000.
  • renv (a separate commit, so it is easy to drop): renv.lock, renv/activate.R and .Rprofile, all excluded from the package build. The lockfile only covers what the package, its tests and its vignette use.
  • .gitignore: adds *.pdf and .DS_Store.

The feature was developed on TimothyMerlin/tstools (branch tsggplot) and squashed into one commit here. Issue numbers in NEWS (#4, #14, #16) refer to issues there.

Testing

  • R CMD check --as-cran gives no errors, warnings or notes (R 4.6.1, macOS). The CRAN-incoming step was skipped because it needs network access.
  • testthat: 494 expectations pass with plotly installed. Without plotly, the plotly tests are skipped and the check still passes. The vignette builds in both cases and leaves out the plotly examples when plotly is missing.

New exports:
- tsggplot(): ggplot2 counterpart of tsplot() for ts, xts and zoo
  series (monthly, quarterly, yearly, weekly, daily and hourly), with
  left/right axes (tsr), stacked bars, bands, confidence intervals,
  highlight windows and fill_year_with_nas.
- tsggplotly(): interactive plotly version of a tsggplot() chart, with
  hover text, a secondary y-axis and the static theme carried over.
- init_tsggplot_theme(): theme for both, built on init_tsplot_theme().

Also adds the vignette "tstools with ggplot2", tests for the new
functions and helpers, and the NEWS entries. Version is bumped to
0.4.4.9000 for development. New imports: ggplot2, ggnewscale, grid
and grDevices; plotly is suggested and only needed for tsggplotly().

Squashed from the development history on TimothyMerlin/tstools
(branch tsggplot). Issue numbers in NEWS refer to issues there.
Adds renv.lock, renv/activate.R and an .Rprofile that activates it.
renv files are excluded from the package build in .Rbuildignore.
@TimothyMerlin
TimothyMerlin marked this pull request as ready for review October 19, 2026 12:59

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant