addPanel: Add a panel to an existing xts plot

Description Usage Arguments Author(s) Examples

View source: R/plot.R

Description

Apply a function to the data of an existing xts plot object and plot the result. FUN should have arguments x or R for the data of the existing xts plot object to be passed to. All other additional arguments for FUN are passed through ....

Usage

1
2
addPanel(FUN, main = "", on = NA, type = "l", col = NULL, lty = 1,
  lwd = 1, pch = 1, ...)

Arguments

FUN

an xts object to plot.

main

main title for a new panel if drawn.

on

panel number to draw on. A new panel will be drawn if on=NA.

type

the type of plot to be drawn, same as in plot.

col

color palette to use, set by default to rational choices.

lty

set the line type, same as in par.

lwd

set the line width, same as in par.

pch

the type of plot to be drawn, same as in par.

...

additional named arguments passed through to FUN and any other graphical passthrough parameters.

Author(s)

Ross Bennett

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
library(xts)
data(sample_matrix)
sample.xts <- as.xts(sample_matrix)

calcReturns <- function(price, method = c("discrete", "log")){
  px <- try.xts(price)
  method <- match.arg(method)[1L]
  returns <- switch(method,
    simple = ,
    discrete = px / lag(px) - 1,
    compound = ,
    log = diff(log(px)))
  reclass(returns, px)
}

# plot the Close
plot(sample.xts[,"Close"])
# calculate returns 
addPanel(calcReturns, method="discrete", type="h")
# Add simple moving average to panel 1
addPanel(rollmean, k=20, on=1)
addPanel(rollmean, k=40, col="blue", on=1)

Example output

Loading required package: zoo

Attaching package: 'zoo'

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric

xts documentation built on Sept. 13, 2020, 5:09 p.m.