Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
S3method(tinyplot,default)
S3method(tinyplot,density)
S3method(tinyplot,formula)
S3method(tinyplot,ts)
export(draw_legend)
export(get_saved_par)
export(plt)
Expand Down Expand Up @@ -122,6 +123,7 @@ importFrom(stats,quantile)
importFrom(stats,setNames)
importFrom(stats,spline)
importFrom(stats,terms)
importFrom(stats,time)
importFrom(stats,weighted.mean)
importFrom(tools,file_ext)
importFrom(utils,globalVariables)
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ where the formatting is also better._

## Development version

### New features

- New dedicated `tinyplot()` method for `ts` time series. Internally, this sets
up a long data frame with columns `Time`, `Value`, and `Series` and then calls
the formula method with different possible specifications for the `by` and `facet`
variables. (#558 @zeileis)

### Aesthetic changes

- The legend plot characters for the `"pointrange"` and `"errorbar"` types now
Expand Down
70 changes: 70 additions & 0 deletions R/tinyplot.ts.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#' tinyplot Method for Plotting ts Objects (Time Series)
#'
#' @description Convenience interface for visualizing ts objects
#' (time sereis with tinyplot.
#'
#' @details Internally the time series object is converted to a long
#' data frame with columns `Time` (time index), `Value` (observations),
#' and `Series` (factor with column labels). Depending on the settings
#' of `facet` this data frame is visualized either with the formula
#' `Value ~ Time` or `Value ~ Time | Series`. See the `facet` argument
#' description for more details and the examples for some illustrations.
#'
#' @param x an object of class `"ts"`.
#' @param facet specification of `facet` for `tinyplot.formula`. The
#' default in the `tinyplot` method is to use `facet = NULL` for univariate
#' series and `facet = ~ Series` (equivalent to `facet = "by"`) for multivariate series.
#' @param type,facet.args,ylab,... further arguments passed to `tinyplot`.
#'
#' @examples
#' tinytheme("clean2")
#'
#' ## univariate series
#' tinyplot(Nile)
#'
#' ## multivariate
#' tinyplot(EuStockMarkets) ## multiple, same color, free scales
#' tinyplot(EuStockMarkets, facet.args = NULL) ## multiple, same color, same scale
#' tinyplot(EuStockMarkets, facet = "by") ## multiple, separate colors, free scales
#' tinyplot(EuStockMarkets, facet = NULL) ## single, separate colors
#'
#' ## further variations
#' tinyplot(EuStockMarkets, facet = "by", facet.args = NULL)
#' tinyplot(EuStockMarkets, facet.args = list(free = TRUE, ncol = 1))
#'
#' tinytheme() ## reset
#'
#' @importFrom stats time
#' @export
tinyplot.ts = function(x, facet, type = "l", facet.args = list(free = TRUE), ylab = "", ...) {
## basic object properties
n = NROW(x)
k = NCOL(x)
lab = deparse(substitute(x))
if (k > 1L) lab = paste(lab, 1L:k, sep = ".")
if (!is.null(colnames(x))) lab = colnames(x)

## convert to long data.frame
df = data.frame(
Time = rep.int(as.numeric(time(x)), k),
Value = as.numeric(x),
Series = factor(rep(1L:k, each = n), labels = lab)
)

## default for facet
single = k == 1L
if(missing(facet)) {
auto = TRUE
facet = if(single) NULL else ~ Series
} else {
auto = FALSE
}
if (is.null(facet)) facet.args = NULL

## call tinyplot
if(single | (!is.null(facet) & auto)) {
tinyplot(Value ~ Time, data = df, facet = facet, facet.args = facet.args, type = type, ylab = ylab, ...)
} else {
tinyplot(Value ~ Time | Series, data = df, facet = facet, facet.args = facet.args, type = type, ylab = ylab, ...)
}
}
260 changes: 260 additions & 0 deletions inst/tinytest/_tinysnapshot/ts-multivariate-by-free.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
242 changes: 242 additions & 0 deletions inst/tinytest/_tinysnapshot/ts-multivariate-by-same.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
228 changes: 228 additions & 0 deletions inst/tinytest/_tinysnapshot/ts-multivariate-column.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
242 changes: 242 additions & 0 deletions inst/tinytest/_tinysnapshot/ts-multivariate-free.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
224 changes: 224 additions & 0 deletions inst/tinytest/_tinysnapshot/ts-multivariate-same.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
87 changes: 87 additions & 0 deletions inst/tinytest/_tinysnapshot/ts-multivariate-single.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading