Type: Package
Title: Time-Series Plots
Version: 0.2.1
Date: 2024-02-23
Maintainer: Enrico Schumann <es@enricoschumann.net>
Description: Plot financial time-series, such as a portfolio value or stock price. The package provides a single function, plotseries(), that creates high-quality, informative, but uncluttered graphics. Multiple series can be aggregated into quantile and fan plots, the code for which is based on chapter 15 of "Numerical Methods and Optimization in Finance", second edition, by M. Gilli, D. Maringer and E. Schumann (2019, ISBN:978-0128150658). Also supported is plotting streaks, i.e. periods of uninterrupted up or down movement.
Imports: PMwR, datetimeutils, grDevices, graphics, neighbours, utils, zoo
Suggests: NMOF, bundesbank, BISdata
License: GPL-3
URL: http://enricoschumann.net/R/packages/plotseries/ , https://git.sr.ht/~enricoschumann/plotseries/ , https://github.com/enricoschumann/plotseries
NeedsCompilation: no
Packaged: 2024-04-07 16:49:37 UTC; es19
Author: Enrico Schumann ORCID iD [aut, cre]
Built: R 4.5.0; ; 2024-04-07 16:49:37 UTC; unix

Plot a Financial Time-Series

Description

Plot a financial time-series and add annotation, such as total return.

Usage

plotseries(series, ...)

## Default S3 method:
plotseries(series, t, col, log.scale = FALSE,
           ##
           labels = NA, labels.show = TRUE, labels.cex = 0.75,
           labels.pos = 4, labels.col = NULL, labels.at = NULL,
           labels.at.offset = NULL, labels.min.height = 0.05,
           ##
           returns.show = TRUE, returns.period = "ann",
           ##
           dollars.show = FALSE, dollars.arrow = "\u2192",
           dollars.currency = "USD",
           ##
           last.show = FALSE, last.format = NULL,
           ##
           ylab = "", ylim = NULL, lwd = 1, type = "l",
           ##
           main = "", main.cex = 0.7, main.col = grey(0.5),
           y.axis = TRUE, y.axis.pos = "left",
           y.grid = TRUE, y.grid.at = NULL, y.grid.col = grey(0.8),
           y.labels = TRUE, y.labels.at = NULL, y.labels.at.add = 1,
           y.labels.at.remove = 0,
           ##
           time.axis = TRUE, time.grid = TRUE,
           time.grid.at = NULL, time.grid.col = grey(0.8),
           time.labels = TRUE,
           time.labels.at = NULL, time.labels.format = NULL,
           add.yearly.grid = FALSE,
           ##
           par.list = list(),
           reset.par = TRUE,
           axis.cex = 1, axis.col = grey(0.5),
           white.underlay = FALSE, white.underlay.width = 2,
           font.family = "", colon = ": ", percent = "%", big.mark = "'",
           bm = NULL, bm.returns = FALSE,
           xpd.hlines = FALSE, xpd.vlines = FALSE, series.type = "level",
           lines = FALSE, do.scale1 = series.type != "streaks", probs = NULL,
           ##
           streaks.up = 0.2, streaks.down = -streaks.up, streaks.vlines = FALSE,
           streaks.relative = TRUE, streaks.up.labels.y.mult = 1.1,
           streaks.up.labels.pos = NULL, streaks.up.labels.srt = 90,
           streaks.up.labels.col = NULL,
           streaks.up.labels.cex = 0.6,
           ##
           streaks.down.labels.y.mult = if (streaks.relative) 0.9 else 1.1,
           streaks.down.labels.pos = NULL, streaks.down.labels.srt = 90,
           streaks.down.labels.col = NULL,
           streaks.down.labels.cex = streaks.up.labels.cex,
           ##
           median.show = TRUE, median.col = grey(0.4),
           ...)

## S3 method for class 'zoo'
plotseries(series, ..., bm = NULL)

Arguments

series

numeric or a zoo series; may be a matrix, with series arranged in columns

t

timestamp, typically of class Date

series.type

string: ‘level’ (the default), ‘quantile’, ‘fan’, or ‘streaks

col

character: colors

...

other parameters

lines

logical: if TRUE, add to existing plot

Customizing labels:

labels

character vector

labels.show

logical: a vector with as many elements as series has columms, recycled if necessary

labels.at

an optional numeric vector to provide the vertical positions of the labels

labels.at.offset

an optional numeric vector to provide the vertical positions offsets of the labels, in particular for shifting overlapping labels

labels.min.height

numeric

labels.cex

numeric

labels.pos

numeric

labels.col

NULL, TRUE or character

last.show

logical

last.format

string

log.scale

logical

y.labels

logical or character

y.labels.at

numeric

y.labels.at.add

numeric

y.labels.at.remove

numeric

ylab

logical

ylim

numeric

main

a string

main.cex

numeric

main.col

character

y.axis

logical

y.axis.pos

logical

time.axis

logical

time.labels.at

..

time.labels.format

..

time.labels

..

time.grid

logical

time.grid.at

..

time.grid.col

a colour

add.yearly.grid

logical

y.grid

logical

y.grid.at

numeric

y.grid.col

a colour

white.underlay

logical

white.underlay.width

numeric

axis.cex

numeric

axis.col

a colour (see colours)

lwd

numeric; see plot

type

numeric; see plot

font.family

logical

dollars.show

logical

dollars.arrow

character. A safe choice (because it is ASCII) is ->.

dollars.currency

string

par.list

a list with named elements, such as list(las = 1, mar = c(3, 3, 3, 3))

reset.par

logical: if TRUE, all changed settings for par are restored

percent

character

big.mark

character

bm

a zoo series

bm.returns

logical

do.scale1

logical

probs

a vector probabilities, used for fan and quantile plots. Quantiles are always chosen symmetrically, and values below 0.5 are mirrored; e.g. 0.1 implies 0.9.

streaks.up

numeric

streaks.down

numeric

streaks.vlines

logical

streaks.relative

logical

streaks.up.labels.y.mult, streaks.down.labels.y.mult

numeric

streaks.up.labels.pos

numeric

streaks.up.labels.srt

numeric

streaks.up.labels.col

a colour

streaks.up.labels.cex, streaks.down.labels.cex

relative character size for streaks labels; default is 0.6

streaks.down.labels.pos

numeric

streaks.down.labels.srt

numeric

streaks.down.labels.col

a colour

xpd.hlines

logical

xpd.vlines

logical

Customizing returns:

returns.show

logical

returns.period

character: default is "ann"; but "itd" (inception to date) may be useful as well

colon

character: the separator between series name, and return or last value

Customizing medians (only for fan and quantile plot):

median.show

logical

median.col

a valid colour specification

Details

The function builds on plot and is experimental; its argument list is not stable. It is strongly suggested that you pass arguments by name, not by position.

Plot types

Settings to be passed with par

plotseries applies the following default settings via par:

        list(las = 1,
             bty = "n",
             mar = c(1.25, 4, 1.25, 4.5),
             tck = 0.01,
             ps = 9.5,
             mgp = c(2, 0.25, 0),
             col.axis = grey(0.5))

These parameters can be overwritten by specifying par.list.

Value

Called for its side effect.

Warning

The function is not stable yet.

Author(s)

Enrico Schumann

References

Gilli, M., Maringer, D. and Schumann, E. (2019) Numerical Methods and Optimization in Finance. 2nd edition. Elsevier. doi:10.1016/C2017-0-01621-X

Schumann, E. (2023) Portfolio Management with R. http://enricoschumann.net/PMwR/

Tufte, E.R.(2001) The Visual Display of Quantitative Information. 2nd edition. Graphics Press. (chapter 6, in particular)

See Also

plot

Examples


series <- French("~/Downloads/French/",
                   "10_Portfolios_Prior_12_2_CSV.zip",
                   frequency = "monthly",
                   weighting = "value",
                   price.series = TRUE)
series <- zoo(series, as.Date(row.names(series)))

plotseries(series,
           col = hcl.colors(n = 30, palette = "Greens"),
           add.dollars = FALSE,
           log.scale = TRUE,
           labels = colnames(series))