plot.xts | R Documentation |
Plotting for xts objects.
## S3 method for class 'xts'
plot(x, y = NULL, ..., subset = "",
panels = NULL, multi.panel = FALSE, col = 1:8, up.col = NULL,
dn.col = NULL, bg = "#FFFFFF", type = "l", lty = 1, lwd = 2, lend = 1,
main = deparse(substitute(x)), main.timespan = TRUE, observation.based = FALSE,
ylim = NULL, yaxis.same = TRUE, yaxis.left = TRUE, yaxis.right = TRUE,
yaxis.ticks = 5, major.ticks = "auto", minor.ticks = NULL,
grid.ticks.on = "auto", grid.ticks.lwd = 1, grid.ticks.lty = 1,
grid.col = "darkgray", labels.col = "#333333", format.labels = TRUE,
grid2 = "#F5F5F5", legend.loc = NULL, extend.xaxis = FALSE)
## S3 method for class 'xts'
lines(x, ..., main = "", on = 0, col = NULL, type = "l",
lty = 1, lwd = 1, pch = 1)
## S3 method for class 'xts'
points(x, ..., main = "", on = 0, col = NULL, pch = 1)
x |
xts object |
y |
NULL, not used |
... |
any passthrough graphical arguments for |
subset |
character vector of length one of the subset range using subsetting as in |
panels |
character vector of expressions to plot as panels |
multi.panel |
TRUE/FALSE or an integer less than or equal to the number
of columns in the data set. If TRUE, each column of the data is plotted in a
separate panel. For example, if |
col |
color palette to use, set by default to rational choices |
up.col |
color for positive bars if |
dn.col |
color for negative bars if |
bg |
background color of plotting area, same as in |
type |
the type of plot to be drawn, same as in |
lty |
set the line type, same as in |
lwd |
set the line width, same as in |
lend |
set the line end style, same as in |
main |
main title |
main.timespan |
include the timespan of the series on the plot? (default |
observation.based |
TRUE/FALSE (default FALSE). If |
ylim |
the range of the y axis |
yaxis.same |
TRUE/FALSE. If TRUE, the y axis is drawn with the same ylim for multiple panels |
yaxis.left |
if TRUE, draws the y axis on the left |
yaxis.right |
if TRUE, draws the y axis on the right |
yaxis.ticks |
desired number of y axis grid lines. The actual number
of grid lines is determined by the |
major.ticks |
period that specifies where tick marks and labels will be drawn on the x-axis. See Details for possible values. |
minor.ticks |
period that specifies where minor ticks on will be drawn on
the x-axis. If |
grid.ticks.on |
period that specifies where vertical grid lines will be drawn. See Details for possible values. |
grid.ticks.lwd |
line width of the grid |
grid.ticks.lty |
line type of the grid |
grid.col |
color of the grid |
labels.col |
color of the axis labels |
format.labels |
label format to draw lower frequency x-axis ticks and labels passed to |
grid2 |
color for secondary x axis grid |
legend.loc |
places a legend into one of nine locations on the chart: bottomright, bottom, bottomleft, left, topleft, top, topright, right, or center. Default NULL does not draw a legend. |
pch |
the plotting character to use, same as in |
on |
panel number to draw on. A new panel will be drawn if |
extend.xaxis |
TRUE/FALSE (default FALSE). If TRUE, extend the x-axis before and/or after the plot's existing time index range, so all of of the time index values of the new series are included in the plot. |
Possible values for arguments major.ticks
, minor.ticks
,
and grid.ticks.on
include ‘auto’, ‘minute’,
‘hours’, ‘days’, ‘weeks’, ‘months’,
‘quarters’, and ‘years’. The default is ‘auto’, which
attempts to determine sensible locations from the periodicity and locations
of observations. The other values are based on the possible values for the
ticks.on
argument of axTicksByTime
.
Ross Bennett
based on chart_Series
in the quantmod
package by Jeffrey A. Ryan
addSeries
, addPanel
## Not run:
data(sample_matrix)
sample.xts <- as.xts(sample_matrix)
# plot the Close
plot(sample.xts[,"Close"])
# plot a subset of the data
plot(sample.xts[,"Close"], subset="2007-04-01/2007-06-31")
# function to compute simple returns
simple.ret <- function(x, col.name){
x[,col.name] / lag(x[,col.name]) - 1
}
# plot the close and add a panel with the simple returns
plot(sample.xts[,"Close"])
R <- simple.ret(sample.xts, "Close")
lines(R, type="h", on=NA)
# add the 50 period simple moving average to panel 1 of the plot
library(TTR)
lines(SMA(sample.xts[,"Close"], n = 50), on=1, col="blue")
# add month end points to the chart
points(sample.xts[endpoints(sample.xts[,"Close"], on = "months"), "Close"],
col="red", pch=17, on=1)
# add legend to panel 1
addLegend("topright", on=1,
legend.names = c("Close", "SMA(50)"),
lty=c(1, 1), lwd=c(2, 1),
col=c("black", "blue", "red"))
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.