plot.xts: Plotting xts Objects

Description Usage Arguments Details Value Author(s) Examples

Description

Plotting methods for xts objects.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
## S3 method for class 'xts'
plot(x, y = NULL, 
            screens = 'auto', layout.screens = 'auto', ...,
            yax.loc = c("none","out","in","flip", "left", "right", "top"),
            auto.grid=TRUE, major.ticks='auto', minor.ticks=TRUE, 
            major.format=TRUE, bar.col.up = 'white',
            bar.col.dn ='red', candle.col='black',
            xy.labels = FALSE, xy.lines = NULL, 
            ylim = 'auto', panel = default.panel, 
            auto.legend = FALSE, legend.names = colnames(x), 
            legend.loc = "topleft", legend.pars = NULL, 
            events, blocks, nc, nr)

Arguments

x

an xts object

y

an xts object or NULL

screens

factor (or coerced to factor) whose levels specify which graph each series is to be plotted in. If not specified, then defaults to a single series per screen for type not "candles" or "bars" See examples.

layout.screens

Matrix (in a form that could be passed to layout) which arranges screens.

yax.loc

Where to put y-axis labels if they are to be drawn? 'none', 'left','right' are rather self-evident. 'in' and 'out' currently only work for two column layouts. 'flip' attemps to arrange the axes to alternate between the left and right hand sides of the plot to avoid label collision. 'top' is something of a misnomer: it places the y-axis labels above each panel while leaving all plots with their own x- and y-axes.

auto.grid

should grid lines be drawn

major.ticks

should major tickmarks be drawn and labeled

minor.ticks

should minor tickmarks be drawn

major.format

passed along to axTicksByTime.

bar.col.dn

the color of the “down” bars when type is ‘bars’ or ‘candles’

bar.col.up

the color of the “up” bars when type is ‘bars’ or ‘candles’

candle.col

the color of the candles when type is ‘candles’. Also the outside of the bars.

xy.labels

label points in scatterplot?

xy.lines

connect points in scatterplot?

ylim

How to handle ylim for plots. If 'fixed' all panels share ylim = range(x); if 'auto' panels sharing a y axis have the same limits. If a numeric matrix, rows are recycled panel-wise as ylim.

panel

A panel function for plotting; by default, something analogous to lines.xts. Currently, is passed col, pch, type, lwd, cex as calculated internally, so be prepared to handle these arguments, perhaps by receiving them via ... and ignoring.

If panel != default.panel, that is, if the user supplies a panel function, the first two arguments passed will be as.POSIXct(index(z)) and z itself, where z is the series being plotted in that panel; as a result, note that any plotting inside panel requires POSIXct, regardless of the index class of x. User supplied panel functions will often wish to make use of default.panel.

Note further that panel is called for each panel, so the second argument (z) passed may well be a multi-column xts object; see xts::default.panel for how this is handled by default. If a list of panel functions is passed, they are recycled panelwise.

auto.legend

Should a legend be added automatically?

legend.names

Names of series to be passed to the legend writing code. Should be one name per series plotted.

legend.loc

Location of legend: will be recycled panelwise. If NA, a legend is ommitted for that panel

legend.pars

Additional arguments, passed as a list, to be passed to legend. Will not recycle

events

A list with mandatory elements time and label and optional elements col,lty giving the events to be highlighted. See examples.

For further control, list elements pos and offset can be passed to place the text as well; even further control is allowed by y.adj providing the size of a downwards shift in the label.

blocks

A list with mandatory elements start.time and end.time and optional element col giving time-blocks to be highlighted. See examples.

nc

Number of columns to be used if layout is set to 'auto'.

nr

Number of rows to be used if layout is set to 'auto'.

...

additional graphical arguments

Details

Mainly used to draw time-series plots with sensible x-axis labels, it can also plot basic OHLC series using type='candles' or type='bars'.

Care has been taken to make plotting arguments passed by ... behave smartly by classifying them as 'global', 'screen-wise', or 'column-wise' arguments and treating them as such. A partial listing includes:

Arguments that are not set to recycle are applied to all the plots via par internally; par is reset to the user's previous state upon exiting the function.

Better financial plots can be found in the quantmod package, though these are generally incompatible with standard R graphics tools.

Value

Invisibly returns the plotted object (or the merged object in the scatterplot case). Also assigns .plot.xts to .GlobalEnv which can be used to "replay" the plot.

Author(s)

Jeffrey A. Ryan with extensions by Michael Weylandt

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
data(sample_matrix, package = "xts")
sample_xts <- as.xts(sample_matrix)

# A very basic time series plot
plot(sample_xts[,1]) 

# A candle chart. quantmod does this much better
plot(sample_xts, type='candles') 

plot(sample_xts[1:30, ], type = "candles")

plot(sample_xts[1:30, ], type = "candles", bar.col.up = "blue", bar.col.dn = "violet", candle.col = "green4")

# By default prefers panels
plot(sample_xts[,1:2]) 

# But this can be too much for many-columned data
plot(sample_xts[,rep(1:4, each = 3)]) 

# Can customize screen layout
plot(sample_xts, layout.screens = matrix(1:4, ncol = 2)) 

# Or even be fancy with it
plot(sample_xts[,1:3], layout.screens = matrix(c(1,1,2,3),ncol = 2, byrow = TRUE))

plot(sample_xts[,1:4], layout.screens = matrix(c(1,1,1,1,2,3,4,4),ncol = 2, byrow = TRUE))

# Or assign multiple series per screen (screens gets recycled as necessary)
# Note smart assignment of colors
plot(sample_xts, screens = 1:2) 

# Example of how columnwise args are treated 
# Note here that col gets recycled alongside screens so it's not helpful here
plot(sample_xts, screens = 1:2, col = 1:2)

# Best practice is to be specific in setting arguments and not use recycling
plot(sample_xts, screens = c(1,2,1,2), col = c(1,3,2,2))

# Example of how panelwise args are treated
plot(10^sample_xts, screens = 1:2, log= c("","y"))

# Global arguments play nice as well
plot(sample_xts[,1:3], main = "A Truly Remarkble Time Series Plot")

# Type = "h" nicely illustrates independent points
plot(sample_xts[1:75,1:2] - 50.5, type = c("l","h"), lwd = c(1,2))

# Arrangement of axis-labels
plot(sample_xts[,1:3], layout = matrix(c(1,1,2,3), ncol = 2, byrow = TRUE), yax.loc = "none")
plot(sample_xts[,1:3], layout = matrix(c(1,1,2,3), ncol = 2, byrow = TRUE), yax.loc = "left")
plot(sample_xts[,1:3], layout = matrix(c(1,1,2,3), ncol = 2, byrow = TRUE), yax.loc = "right")

plot(sample_xts, layout = matrix(1:4, ncol = 2), yax.loc = "in")
plot(sample_xts, layout = matrix(1:4, ncol = 2), yax.loc = "out")
plot(sample_xts, layout = matrix(1:4, ncol = 2), yax.loc = "flip")

plot(sample_xts[,c(1:4, 3:4)], layout = matrix(c(1,1,1,1,2,2,3,4,5,6), ncol = 2, byrow = TRUE), yax.loc = "flip")

plot(sample_xts[,c(1:4, 3:4)], layout = matrix(c(1,1,1,1,2,2,3,4,5,6), ncol = 2, byrow = TRUE), yax.loc = "left")

plot(sample_xts[,c(1:4, 3:4)], layout = matrix(c(1,1,1,1,2,2,3,4,5,6), ncol = 2, byrow = TRUE), yax.loc = "top")

# Type = "p" allows for transparent handling of the pch= argument
x <- xts(1:5, Sys.Date() + 1:5)
plot(x, type = "p")
plot(x, type = "p", pch = 1:5, col = 1:5)

# Multivariate recycles over columns
plot(merge(x,x), type = "p", pch = 1:5, col = 1:2)

# Unless you use a list
plot(merge(x,x), type = "p", pch = list(1:5), col = 1:2)
plot(merge(x,x,x), type = "p", pch = list(1:5, 1:3), col = 1:3)

# Same for col= and cex= arguments
plot(x, type = "p", col = 1:5, pch = 1:5, cex = 1:5)
plot(merge(x,x), type = "p", col = 1:5, pch = 1:5, cex = 1:5)
plot(merge(x,x), type = "p", col = list(1:5, 1:3), pch = list(1:5, 1:3), cex = list(1:5, 1:3))

# Using the events argument
plot(sample_xts[,1], events = list(time = c("2007-03-15", "2007-05-01"), label = "bad days"))

plot(sample_xts[,1], events = list(time = c("2007-03-15","2007-05-01"), label = "bad days"), blocks = list(start.time = c("2007-03-05", "2007-04-15"), end.time = c("2007-03-20","2007-05-30"), col = c("lightblue1", "lightgreen")))

plot(sample_xts, layout.screens = matrix(c(1,1,2,3,4,4), ncol = 2, byrow = TRUE), events = list(time = c("2007-03-15","2007-05-01"), label = "bad days"), blocks = list(start.time = c("2007-03-05", "2007-04-15"), end.time = c("2007-03-20","2007-05-30"), col = c("lightblue1", "lightgreen")))

# Makes a scatterplot if we pass two series
plot(sample_xts[,1],sample_xts[,2])

# With some options
plot(sample_xts[1:100,1],sample_xts[1:100,2], xy.labels = TRUE)

# Use of color gradient
cr <- colorRampPalette(c("#00FF00","#FF0000"))
plot(sample_xts[,1],sample_xts[,2], xy.labels = FALSE, xy.lines = TRUE, col = cr(NROW(sample_xts)), type = "l")

# Can also use non-xts time series classes if called explicitly
tser <- ts(cumsum(rnorm(50, 0.05, 0.15)), start = 2007, frequency = 12)
plot(tser)
plot.xts(tser)

tser <- cbind(tser, ts(cumsum(rnorm(50, 0.05, 0.15)), start = 2007, frequency = 12))
plot(tser)
plot.xts(tser)


## Not run: 
# Some of these will fail because they try to access the 
# parent frame in ways not congruent with plot.xts
plot.zoo <- plot.xts
example(plot.zoo)
rm(plot.zoo)

## End(Not run)

R-Finance/xtsExtra documentation built on May 8, 2019, 4:51 a.m.