Description Usage Arguments Details Value Author(s) Examples
Plotting methods for xts objects.
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)
|
x |
an |
y |
an |
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 |
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? |
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 |
bar.col.up |
the color of the “up” bars when |
candle.col |
the color of the candles when |
xy.labels |
label points in scatterplot? |
xy.lines |
connect points in scatterplot? |
ylim |
How to handle |
panel |
A panel function for plotting; by default, something analogous to If Note further that |
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 |
legend.pars |
Additional arguments, passed as a list, to be passed to |
events |
A list with mandatory elements For further control, list elements |
blocks |
A list with mandatory elements |
nc |
Number of columns to be used if |
nr |
Number of rows to be used if |
... |
additional graphical arguments |
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:
Global parameters: main
Screen-wise parameters: log
, ylab
Column-wise parameters: col
,type
,lwd
, pch
, cex
, lty
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.
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.
Jeffrey A. Ryan with extensions by Michael Weylandt
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.