Description Usage Arguments Details
This function is a wrapper for plot.zoo and plot.xts by providing a simple, unified way to plot time series to the screen or to a png file. It includes nice defaults for colors and legend formatting.
1 2 3 4 5 6 7 8 9 | xtsplot(data, datarange = NA, method = c("zoo", "xts", "custom"),
ptype = c("equity_curve", "performance"), legend = "topleft",
norm = TRUE, mode = "portfolio", bench = 0, log = "",
fname = NULL, pngsize = c(1280, 720), col = "auto", lwd = 1,
lty = 1, shaded = NULL, shaded_inv = TRUE,
shaded_col = c("green", "red", "orange", "grey", "blue", "yellow",
"purple", "brown", "pink"), vline = NA, vlabel = NA, hline = NA,
xlab = NA, ylab = NA, cex.legend = 0.7, cex.lab = 1.15,
mgp = c(1.8, 0.6, 0), return_xts = FALSE, tie_recent = NA, ...)
|
data |
An xts matrix containing one or multiple columns to plot. |
datarange |
An xts matrix of similar size as data, used only for the purpose of setting a plot labeling range that differs from data. This is normally used to scale the y axis to allow easy side-by-side comparison of two similar plots. Default is NA, which means it is ignored. |
method |
The plot method to use. Must be one of "zoo", "xts", or "custom" |
ptype |
The type of plot to display. Must be one of "equity_curve" or "performance". An equity curve plot displays a single plot region with all equity curves. A performance plot displays 3 regions: an equity curve plot, a 12 month rolling return plot and a drawdown plot. (NOT IMPLEMENTED) |
legend |
Location of the legend on the plot, or "none" if no legend is desired. Must be one of "none", "bottomright", "bottom", "bottomleft", "left", "topleft", "top", "topright", "right" or "center". Default is "topleft". This argument is passed to the plot method. |
norm |
Logical. Flag specifying whether to normalize multiple curves to a common starting date. When set to FALSE, nothing is normalized and rows with all NAs (including leading rows) are shown on the time scale (as blank curves). When set to TRUE, then rows with all NAs are removed as a first step. Then, if tie_recent is NA, all rows with some NAs are also removed so that all curves can start on the same date. On the other hand, if tie_recent is set to a given column, then all curves starting later than the earliest curve are plotted on their starting date (the earliest curve is not truncated), and these curves are tied to the benchmark or column 1 if no benchmark is specified. |
mode |
Defines the mode used to plot the information. When mode = "gain", the percentage gain is shown starting at 0 but only a linear scale is allowed. When mode = "portfolio" (the default), then the value of the portfolio is shown starting at 1.0 if normalized is true. When mode = "growthof100", then all is normalized at 100. |
bench |
Specifies the column (number or name) to highlight as the benchmark. |
log |
Log argument passed to methods. Currently supported by the zoo method, and its value can be "y" (for y log scale), "x" (for x log scale) or "xy" for both. |
fname |
If NULL (default), then only plot to the screen. If fname is provided, then create a png file at the given path in addition to plotting on the screen. The output file name is fname with a date-time stamp appended. |
pngsize |
A length 2 vector containing the size of the png plot file, specified in pixels as c(x, y). |
col |
Vector of color names to use when plotting lines. The colors should be part of the set: c('black', 'blue', 'green', 'red', 'orange', 'purple', 'brown', 'darkpink', 'grey', 'turquoise', 'mauve', 'lightblue', 'lightgreen', 'pink', 'lightorange', 'lightpurple', 'yellow'). This is normally used to change the order of colors for plots with multiple lines. Base R colors can also be used if specified. Automatically recycled. |
lwd |
Vector specifying the line width. Default is 1. Recycled as needed. |
lty |
Vector specifying the line type. Default is "line" or 1. Recycled. See par() for additional details on lwd and lty. |
shaded |
An logical (or 0, 1) xts matrix specifying the shaded regions. Each column is associated with a region type (color). When a row contains true, that date is shaded. |
shaded_inv |
Logical flag indicating whether to invert the xts matrix shaded before plotting the regions. This is helpful when plotting market timers where normally true means a bull market but we want to highlight false (the bear markets). |
shaded_col |
A vector specifying the shaded regions colors. This vector is recycled if there are more types of regions than colors. The colors must be part of the set: c('green', 'red', 'orange', 'grey', 'blue', 'yellow', 'purple', 'brown', 'pink'). These colors are not the same as the line colors as they are purposely made pastel-like and transparent to highlight overlapping regions. Recycled as needed. |
vline |
Adds a vertical line to the plot. May be a vector of length 1 or a list of length 2 or 3. NA = no line plotted (default). First list element (or length one vector) contains the X coordinate of the vertical line, and it is either a numeric for a zoo plot, or a string representing a date for an xts plot. Second list element (optional) is the color of the line in either numeric form or a string with the color name, which must correspond to a color in argument cols. The third list element specifies the line width or, if not specified, the line width defaults to lwd = 1. |
vlabel |
Adds a vertical text label on the vline, if specified. vlabel is specified as a list of two items. Item 1 specifies the y value where the label will be placed (label ends at that y value). The size of the text follows cex.legend. Item 2 specifies the actual label text. The label is placed on the left side of the vline, reading from bottom to the top. |
hline |
Adds a horizontal line to the plot. Arguments follow a similar format as vline. |
xlab |
X-axis label as a character string. If omitted, then default is the plotted timeframe, possibly adjusted by omitting leading NAs. |
ylab |
Y-axis label as a character string. If omitted, then default is "Prices" or, if data is normalized, then "Norm. Prices". |
cex.legend |
The relative size for the legend and the vlabel. Default is 0.7. |
cex.lab |
The relative size for the axis labels. Default is 1.15. |
mgp |
The margin line for the axis title, axis lables and axis line. See help for par(). |
return_xts |
Logical. When TRUE, the normalized and scaled xts matrix plotted is returned. Default is FALSE. |
tie_recent |
Default is NA. When set to a column number or name in the data matrix, all curves that have a starting date more recent than the benchmark will be tied (that is, attached) to the equity curve represented by that column on their first trading day. This allows easy visualizations of recent performance by direct overlap of these curves. |
... |
Additional arguments passed to the plot method. |
In addition, xtsplot provides the following facilities:
Normalization of curves so they all start at the same time and the same level.
Shaded regions in the plot when argument shaded contains an xts matrix. See shaded and shaded_col for details.
A horizontal and/or a vertical line can be added to the plot to show a crosshair or highlight a given level or time using vline and hline.
Plot can be automatically saved to a file using a time stamp, and is designed to look essentially identical to the screen version.
A legend can be automatically added and placed at the best location on the plot.
For multiple line plots, one curve may be selected as the benchmark and appropriately plotted last (last on the legend also) so it's on top of the others. By default, its color is black and lwd = 3.
Log scale are allowed for all plot options.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.