Title: | Time Series Plot |
---|---|
Description: | A fast and elegant time series visualization package. In addition to the standard R plot types, this package supports candle sticks, open-high-low-close, and volume plots. Useful for visualizing any time series data, e.g., stock prices and technical indicators. |
Authors: | RTSVizTeam [aut, cph], Irina Kapler [cre] |
Maintainer: | Irina Kapler <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.5 |
Built: | 2024-12-17 06:31:58 UTC |
Source: | CRAN |
Setup theme
register.theme( grid.color = "gray90", colors = "Set1", col.border = "black", col.up = "green", col.dn = "red", col.x.highlight = "orange", col.y.highlight = "orange", cex = 1, legend.bg.col = grDevices::adjustcolor("white", 200/255) ) rtsplot.theme() rtsplot.theme.set(...) rtsplot.colors(n)
register.theme( grid.color = "gray90", colors = "Set1", col.border = "black", col.up = "green", col.dn = "red", col.x.highlight = "orange", col.y.highlight = "orange", cex = 1, legend.bg.col = grDevices::adjustcolor("white", 200/255) ) rtsplot.theme() rtsplot.theme.set(...) rtsplot.colors(n)
grid.color |
color for grid lines, defaults to 'gray90' |
colors |
RColorBrewer set to generate colors, defaults to "Set1" in RColorBrewer |
col.border |
border color for drawing candles, defaults to 'black' |
col.up |
up color for drawing candles, defaults to 'green' |
col.dn |
down color for drawing candles, defaults to 'red' |
col.x.highlight |
color for highlighting along x axis, defaults to 'orange' |
col.y.highlight |
color for highlighting along y axis, defaults to 'orange' |
cex |
font size, defaults to 1 |
legend.bg.col |
background legend color, defaults to grDevices::adjustcolor('white', 200/255) |
... |
additional settings |
n |
number of colors to generate |
None
Plot time series data with base R Graphics.
The 'rtsplot' package is **fast** time series plot package with base R Graphics.
rtsplot( y, main = NULL, plotX = TRUE, LeftMargin = 0, grid = "xy", x.highlight = NULL, y.highlight = NULL, y.highlight.col = NULL, las = 1, type = "l", xlab = "", ylab = "", ylim = NULL, log = "", skip.breaks = FALSE, xaxis.map = rtsplot.create.xaxis.map, ... )
rtsplot( y, main = NULL, plotX = TRUE, LeftMargin = 0, grid = "xy", x.highlight = NULL, y.highlight = NULL, y.highlight.col = NULL, las = 1, type = "l", xlab = "", ylab = "", ylim = NULL, log = "", skip.breaks = FALSE, xaxis.map = rtsplot.create.xaxis.map, ... )
y |
|
main |
plot title |
plotX |
flag to display X axis |
LeftMargin |
to plot second Y axis, set LeftMargin=3, defaults to 0 |
grid |
which grid lines to draw, defaults to 'xy' |
x.highlight |
segments to highlight along X axis, defaults to NULL |
y.highlight |
segments to highlight along Y axis, defaults to NULL |
y.highlight.col |
color to highlight segments Y axis, defaults to NULL |
las |
rotation of Y axis labels, defaults to 1, for more info see |
type |
plot type, defaults to 'l', for more info see |
xlab |
X label, defaults to ”, for more info see |
ylab |
Y label, defaults to ”, for more info see |
ylim |
range on Y values, defaults to NULL |
log |
log scale x, y, xy axes, defaults to ” |
skip.breaks |
flag to skip plotting missing date/times (i.e. nights and weekends), defaults to FALSE |
xaxis.map |
xaxis map function used if skip.breaks is TRUE, defaults to rtsplot.create.xaxis.map |
... |
additional parameters to the |
nothing
Maintainer: Irina Kapler [email protected]
Authors:
RTSVizTeam [email protected] [copyright holder]
Useful links:
Report bugs at https://bitbucket.org/rtsvizteam/rtsplot/issues
# generate time series data y = rtsplot.fake.stock.data(1000) symbol = 'Test' sma = TTR::SMA(y, 250) rsi = TTR::RSI(y, 20) # plot candles and RSI charts layout(c(1,1,1,2)) cols = rtsplot.colors(2) rtsplot(y, type = 'l', plotX = FALSE, col=cols[1],lwd=1.5) rtsplot.lines(sma, col=cols[2], lwd=1.5) rtsplot.legend(c(symbol, 'SMA(250)'), cols[1:2], list(y,sma)) # plot rsi rtsplot(rsi, type = 'l', ylim=c(0,100), y.highlight = c(c(0,30), c(70,100)), y.highlight.col = grDevices::adjustcolor(c('green','red'), 50/255) ) rtsplot.legend('RSI(20)', 'black', rsi) y = rtsplot.fake.stock.data(1000) symbol = 'SPY' # simple example highlight = which(y < 10) # plot layout(1) rtsplot.theme.set(col.x.highlight=grDevices::adjustcolor('orange', 200/255)) rtsplot(y, type = 'l', main = symbol, x.highlight = highlight) # 'skip.breaks' example with daily data y = rtsplot.fake.stock.data(10, remove.non.trading = TRUE) layout(1:2) rtsplot(y, type='b') rtsplot.legend('skip.breaks=FALSE', text.col='red') rtsplot(y, type='b', skip.breaks=TRUE) rtsplot.legend('skip.breaks=TRUE', text.col='red') # 'skip.breaks' example with intra-day data y = rtsplot.fake.stock.data(5*24*60, period = 'minute', remove.non.trading = TRUE) layout(1:2) rtsplot(y, type='l') rtsplot.legend('skip.breaks=FALSE', text.col='red') rtsplot(y, type='l', skip.breaks=TRUE) rtsplot.legend('skip.breaks=TRUE', text.col='red')
# generate time series data y = rtsplot.fake.stock.data(1000) symbol = 'Test' sma = TTR::SMA(y, 250) rsi = TTR::RSI(y, 20) # plot candles and RSI charts layout(c(1,1,1,2)) cols = rtsplot.colors(2) rtsplot(y, type = 'l', plotX = FALSE, col=cols[1],lwd=1.5) rtsplot.lines(sma, col=cols[2], lwd=1.5) rtsplot.legend(c(symbol, 'SMA(250)'), cols[1:2], list(y,sma)) # plot rsi rtsplot(rsi, type = 'l', ylim=c(0,100), y.highlight = c(c(0,30), c(70,100)), y.highlight.col = grDevices::adjustcolor(c('green','red'), 50/255) ) rtsplot.legend('RSI(20)', 'black', rsi) y = rtsplot.fake.stock.data(1000) symbol = 'SPY' # simple example highlight = which(y < 10) # plot layout(1) rtsplot.theme.set(col.x.highlight=grDevices::adjustcolor('orange', 200/255)) rtsplot(y, type = 'l', main = symbol, x.highlight = highlight) # 'skip.breaks' example with daily data y = rtsplot.fake.stock.data(10, remove.non.trading = TRUE) layout(1:2) rtsplot(y, type='b') rtsplot.legend('skip.breaks=FALSE', text.col='red') rtsplot(y, type='b', skip.breaks=TRUE) rtsplot.legend('skip.breaks=TRUE', text.col='red') # 'skip.breaks' example with intra-day data y = rtsplot.fake.stock.data(5*24*60, period = 'minute', remove.non.trading = TRUE) layout(1:2) rtsplot(y, type='l') rtsplot.legend('skip.breaks=FALSE', text.col='red') rtsplot(y, type='l', skip.breaks=TRUE) rtsplot.legend('skip.breaks=TRUE', text.col='red')
Plot candles if dx is sufficient otherwise ohlc or bars
rtsplot.candle( y, col = rtsplot.candle.col(y), border = rtsplot.theme()$col.border )
rtsplot.candle( y, col = rtsplot.candle.col(y), border = rtsplot.theme()$col.border )
y |
|
col |
color for bars, defaults to rtsplot.candle.col |
border |
border color, defaults to rtsplot.theme()$col.border |
nothing
y = rtsplot.fake.stock.data(50, ohlc=TRUE) symbol = 'SPY' # plot layout(1) rtsplot(y, type = 'n') rtsplot.candle(y) rtsplot.legend(symbol, 'black', y)
y = rtsplot.fake.stock.data(50, ohlc=TRUE) symbol = 'SPY' # plot layout(1) rtsplot(y, type = 'n') rtsplot.candle(y) rtsplot.legend(symbol, 'black', y)
Bar Colors for Candle and Volume plots
rtsplot.candle.col(y) rtsplot.volume.col(y)
rtsplot.candle.col(y) rtsplot.volume.col(y)
y |
|
colors
Plot corner label, based on the [text at the upper left corner outside of the plot region](http://r.789695.n4.nabble.com/text-at-the-upper-left-corner-outside-of-the-plot-region-td885675.html)
rtsplot.corner.label( label = NULL, col = "black", x = -1, y = 1, xoffset = NA, yoffset = NA, space = c("plot", "figure"), cex = 1, border = NA )
rtsplot.corner.label( label = NULL, col = "black", x = -1, y = 1, xoffset = NA, yoffset = NA, space = c("plot", "figure"), cex = 1, border = NA )
label |
label |
col |
label color |
x |
x location, defaults to -1 |
y |
y location, defaults to 1 |
xoffset |
x offset, defaults to NA |
yoffset |
y offset, defaults to NA |
space |
coordinate space, can be "plot" or "figure", defaults to "plot" |
cex |
font size, defaults to 1 |
border |
border color, defaults to NA - no color |
nothing
rtsplot.theme.set(legend.bg.col=grDevices::adjustcolor('orange', 200/255)) plot(rnorm(20), rnorm(20)) rtsplot.corner.label('test1', y=-1, space='figure') rtsplot.corner.label('test2', y=1, space='figure') rtsplot.corner.label('test3', x=1, space='figure') rtsplot.corner.label('test4', x=1, y=-1, space='figure') rtsplot.theme.set(legend.bg.col=grDevices::adjustcolor('white', 50/255))
rtsplot.theme.set(legend.bg.col=grDevices::adjustcolor('orange', 200/255)) plot(rnorm(20), rnorm(20)) rtsplot.corner.label('test1', y=-1, space='figure') rtsplot.corner.label('test2', y=1, space='figure') rtsplot.corner.label('test3', x=1, space='figure') rtsplot.corner.label('test4', x=1, y=-1, space='figure') rtsplot.theme.set(legend.bg.col=grDevices::adjustcolor('white', 50/255))
Generate fake stock data for use in rtsplot examples
rtsplot.fake.stock.data( n, y0 = 10, stdev = 0.1, ohlc = FALSE, method = c("normal", "adhoc"), period = c("day", "minute"), remove.non.trading = FALSE )
rtsplot.fake.stock.data( n, y0 = 10, stdev = 0.1, ohlc = FALSE, method = c("normal", "adhoc"), period = c("day", "minute"), remove.non.trading = FALSE )
n |
number of points to generate |
y0 |
starting price, defaults to 10 |
stdev |
standard deviation, defaults to 0.1 |
ohlc |
generate ohlc data, defaults to FALSE |
method |
method to generate fake stock data, defaults to 'normal' two methods are implemented: * 'normal' - generate fake stock data assuming returns are normally distributed with zero drift * 'uniform' - generate fake stock data assuming returns are uniformly distributed with zero drift |
period |
frequency to generate fake stock data, (possible values: "day", "minute"), defaults to "day" |
remove.non.trading |
flag to remove non trading periods(i.e. weekends and non-trading hours). Note, this flag likely will cause function return less than 'n' observation, defaults to FALSE |
xts
object with fake stock data
rtsplot.fake.stock.data(10)
rtsplot.fake.stock.data(10)
Format numbers using 1000 separator
rtsplot.format(temp, nround = 2, sprefix = "", eprefix = "")
rtsplot.format(temp, nround = 2, sprefix = "", eprefix = "")
temp |
numbers |
nround |
number of rounding digits, defaults to '2' |
sprefix |
start prefix string, defaults to ” |
eprefix |
end postfix string, defaults to ” |
numbers formatted using 1000 separator
Add grid to time series plot
rtsplot.grid(grid, xaxis.ticks, col = rtsplot.theme()$grid.color)
rtsplot.grid(grid, xaxis.ticks, col = rtsplot.theme()$grid.color)
grid |
which grid lines to draw, defaults to 'xy' |
xaxis.ticks |
location of x axis ticks |
col |
grid color, defaults to rtsplot.theme()$grid.color |
nothing
Create HL Plot
rtsplot.hl(y, col = rtsplot.volume.col(y), border = rtsplot.theme()$col.border)
rtsplot.hl(y, col = rtsplot.volume.col(y), border = rtsplot.theme()$col.border)
y |
|
col |
color for bars, defaults to rtsplot.volume.col |
border |
border color, defaults to rtsplot.theme()$col.border |
nothing
y = rtsplot.fake.stock.data(50, ohlc=TRUE) symbol = 'SPY' # plot layout(1) rtsplot(y, type = 'n') rtsplot.hl(y) rtsplot.legend(symbol, 'black', y)
y = rtsplot.fake.stock.data(50, ohlc=TRUE) symbol = 'SPY' # plot layout(1) rtsplot(y, type = 'n') rtsplot.hl(y) rtsplot.legend(symbol, 'black', y)
Create layout
rtsplot.layout(ilayout, delim = ",")
rtsplot.layout(ilayout, delim = ",")
ilayout |
matrix stored as a string |
delim |
delimiter, defaults to ',' |
nothing
legend
functionPlot legend - shortcut to the legend
function
rtsplot.legend( labels, fill = NULL, lastobs = NULL, x = "topleft", merge = FALSE, bty = "n", border = NA, yformat = rtsplot.format, cex = 1, ... )
rtsplot.legend( labels, fill = NULL, lastobs = NULL, x = "topleft", merge = FALSE, bty = "n", border = NA, yformat = rtsplot.format, cex = 1, ... )
labels |
legend labels |
fill |
fill colors, defaults to NULL |
lastobs |
list of last observations, defaults to NULL |
x |
location of legend, defaults to 'topleft' |
merge |
merge, defaults to FALSE, see |
bty |
box, defaults to 'n', see |
border |
border color, defaults to NA - no color |
yformat |
format Y values function, defaults to |
cex |
font size, defaults to 1 |
... |
other parameters to legend, see |
nothing
y = rtsplot.fake.stock.data(1000) symbol = 'SPY' # plot layout(1) rtsplot(y, type = 'l', col='black') rtsplot.legend(symbol, 'black', y)
y = rtsplot.fake.stock.data(1000) symbol = 'SPY' # plot layout(1) rtsplot(y, type = 'l', col='black') rtsplot.legend(symbol, 'black', y)
Add lines to time series plot
rtsplot.lines(y, type = "l", col = graphics::par("col"), ...)
rtsplot.lines(y, type = "l", col = graphics::par("col"), ...)
y |
|
type |
line type, defaults to 'l', for more info see |
col |
color, defaults to par('col') |
... |
additional parameters to the |
nothing
y = rtsplot.fake.stock.data(1000) symbol = 'SPY' # moving average sma = TTR::SMA(y, 250) # plot layout(1) rtsplot(y, type = 'l', col='black') rtsplot.lines(sma, col='blue', lwd=1.5) rtsplot.legend(c(symbol, 'SMA(250)'), 'black,blue', list(y,sma))
y = rtsplot.fake.stock.data(1000) symbol = 'SPY' # moving average sma = TTR::SMA(y, 250) # plot layout(1) rtsplot(y, type = 'l', col='black') rtsplot.lines(sma, col='blue', lwd=1.5) rtsplot.legend(c(symbol, 'SMA(250)'), 'black,blue', list(y,sma))
matplot
version for xts
objectmatplot
version for xts
object
rtsplot.matplot( y, dates = NULL, ylim = NULL, type = "l", cols = rtsplot.colors(ncol(y)), ... )
rtsplot.matplot( y, dates = NULL, ylim = NULL, type = "l", cols = rtsplot.colors(ncol(y)), ... )
y |
|
dates |
subset of datesdefaults to NULL |
ylim |
range on Y values, defaults to NULL |
type |
plot type, defaults to 'l', see |
cols |
colors |
... |
additional parameters to the |
nothing
Plot ohlc if dx is sufficient otherwise bars
rtsplot.ohlc(y, col = rtsplot.theme()$col.border)
rtsplot.ohlc(y, col = rtsplot.theme()$col.border)
y |
|
col |
color for bars, defaults to rtsplot.theme()$col.border |
nothing
y = rtsplot.fake.stock.data(50, ohlc=TRUE) symbol = 'SPY' # plot layout(1) rtsplot(y, type = 'n') rtsplot.ohlc(y) rtsplot.legend(symbol, 'black', y) rtsplot.theme.set(legend.bg.col=grDevices::adjustcolor('blue', 25/255)) rtsplot.corner.label('Logo \uA9', x=1, y=-1, cex = 0.7, space='figure', col='blue') rtsplot.theme.set(legend.bg.col = grDevices::adjustcolor('white', 200/255))
y = rtsplot.fake.stock.data(50, ohlc=TRUE) symbol = 'SPY' # plot layout(1) rtsplot(y, type = 'n') rtsplot.ohlc(y) rtsplot.legend(symbol, 'black', y) rtsplot.theme.set(legend.bg.col=grDevices::adjustcolor('blue', 25/255)) rtsplot.corner.label('Logo \uA9', x=1, y=-1, cex = 0.7, space='figure', col='blue') rtsplot.theme.set(legend.bg.col = grDevices::adjustcolor('white', 200/255))
Add polygon to time series plot
rtsplot.polygon(y, col = graphics::par("col"), ...)
rtsplot.polygon(y, col = graphics::par("col"), ...)
y |
|
col |
color, defaults to par('col') |
... |
additional parameters to the |
nothing
y = rtsplot.fake.stock.data(1000, ohlc=TRUE) symbol = 'SPY' # moving average bbands = TTR::BBands(quantmod::HLC(y), n=200, sd=1)[,c('up','dn')] # plot layout(1) rtsplot(y, type = 'l', col='black') col = grDevices::adjustcolor('green', 50/255) rtsplot.polygon(bbands, col = col) rtsplot.legend(c(symbol, 'BBands'), c('black', col), list(y,bbands))
y = rtsplot.fake.stock.data(1000, ohlc=TRUE) symbol = 'SPY' # moving average bbands = TTR::BBands(quantmod::HLC(y), n=200, sd=1)[,c('up','dn')] # plot layout(1) rtsplot(y, type = 'l', col='black') col = grDevices::adjustcolor('green', 50/255) rtsplot.polygon(bbands, col = col) rtsplot.legend(c(symbol, 'BBands'), c('black', col), list(y,bbands))
Scale volume
rtsplot.scale.volume(y)
rtsplot.scale.volume(y)
y |
|
adjusted y object
Create Stacked plot
rtsplot.stacked( x, y, xlab = "", cols = rtsplot.colors(ncol(y)), type = c("l", "s"), flip.legend = FALSE, ... )
rtsplot.stacked( x, y, xlab = "", cols = rtsplot.colors(ncol(y)), type = c("l", "s"), flip.legend = FALSE, ... )
x |
dates object |
y |
matrix with weights |
xlab |
X label, defaults to ”, for more info see |
cols |
colors, defaults to colors |
type |
plot type: lines, step stairs c('l','s') |
flip.legend |
flag to reverse legend order, defaults to FALSE |
... |
additional parameters to the |
nothing
Add text to time series plot
rtsplot.text(y, ...)
rtsplot.text(y, ...)
y |
|
... |
additional parameters to the |
nothing
y = rtsplot.fake.stock.data(1000) symbol = 'SPY' # plot layout(1) rtsplot(y, type = 'l', col='black') rtsplot.text(y[100], 'Text', col='red') rtsplot.legend(symbol, 'black', y)
y = rtsplot.fake.stock.data(1000) symbol = 'SPY' # plot layout(1) rtsplot(y, type = 'l', col='black') rtsplot.text(y[100], 'Text', col='red') rtsplot.legend(symbol, 'black', y)
Plot volume
rtsplot.volume( y, col = rtsplot.volume.col(y), border = rtsplot.theme()$col.border )
rtsplot.volume( y, col = rtsplot.volume.col(y), border = rtsplot.theme()$col.border )
y |
|
col |
color for volume bars |
border |
color for volume bars border |
nothing
Highlight vertical segments
rtsplot.x.highlight(y, highlight, col = rtsplot.theme()$col.x.highlight)
rtsplot.x.highlight(y, highlight, col = rtsplot.theme()$col.x.highlight)
y |
|
highlight |
segments to highlight along X axis |
col |
highlight color, defaults to rtsplot.control$col.x.highlight |
nothing
Highlight horizontal segments
rtsplot.y.highlight(highlight, col = rtsplot.theme()$col.y.highlight)
rtsplot.y.highlight(highlight, col = rtsplot.theme()$col.y.highlight)
highlight |
segments to highlight along Y axis |
col |
highlight color, defaults to rtsplot.control$col.y.highlight |
nothing
# generate time series data y = rtsplot.fake.stock.data(1000) rsi = TTR::RSI(y, 20) #set up two regions for graphs candlestick price data on top 2/3 of the plot #and rsi on the bottom 1/3 of the plot layout(c(1,1,2)) rtsplot(y, type = 'line', plotX = FALSE) rtsplot.legend('SPY', 'grey70', y) rtsplot(rsi, type = 'l') col = grDevices::adjustcolor(c('green','red'), 80/255) rtsplot.y.highlight(col=col[1], highlight=c(50,100)) rtsplot.y.highlight(col=col[2], highlight=c(0,50)) abline(h = 50, col = 'gray20') rtsplot.legend('RSI(20)', 'black', rsi)
# generate time series data y = rtsplot.fake.stock.data(1000) rsi = TTR::RSI(y, 20) #set up two regions for graphs candlestick price data on top 2/3 of the plot #and rsi on the bottom 1/3 of the plot layout(c(1,1,2)) rtsplot(y, type = 'line', plotX = FALSE) rtsplot.legend('SPY', 'grey70', y) rtsplot(rsi, type = 'l') col = grDevices::adjustcolor(c('green','red'), 80/255) rtsplot.y.highlight(col=col[1], highlight=c(50,100)) rtsplot.y.highlight(col=col[2], highlight=c(0,50)) abline(h = 50, col = 'gray20') rtsplot.legend('RSI(20)', 'black', rsi)
Detailed discussion for validity of dual Y axis at [Dual axes time series plots may be ok sometimes after all](http://freerangestats.info/blog/2016/08/18/dualaxes)
rtsplot2Y(y, las = 1, type = "l", col.axis = "red", ylim = NULL, log = "", ...)
rtsplot2Y(y, las = 1, type = "l", col.axis = "red", ylim = NULL, log = "", ...)
y |
|
las |
rotation of Y axis labels, defaults to 1, for more info see |
type |
plot type, defaults to 'l', for more info see |
col.axis |
axis color, defaults to 'red' |
ylim |
range on Y values, defaults to NULL |
log |
log scale x, y, xy axes, defaults to ” |
... |
additional parameters to the |
nothing
# generate time series data y = rtsplot.fake.stock.data(1000) symbol = 'SPY' y1 = rtsplot.fake.stock.data(1000, 100) symbol = 'IBM' # two Y axis example # to plot second Y axis, free some space on left side, set LeftMargin=3 layout(1) cols = c('black', 'red') rtsplot(y, type = 'l', LeftMargin=3, col=cols[1]) rtsplot2Y(y1, type='l', las=1, col=cols[2], col.axis=cols[2]) rtsplot.legend('SPY(rhs),IBM(lhs)', cols, list(y,y1))
# generate time series data y = rtsplot.fake.stock.data(1000) symbol = 'SPY' y1 = rtsplot.fake.stock.data(1000, 100) symbol = 'IBM' # two Y axis example # to plot second Y axis, free some space on left side, set LeftMargin=3 layout(1) cols = c('black', 'red') rtsplot(y, type = 'l', LeftMargin=3, col=cols[1]) rtsplot2Y(y1, type='l', las=1, col=cols[2], col.axis=cols[2]) rtsplot.legend('SPY(rhs),IBM(lhs)', cols, list(y,y1))