R / quantmod: all diagrams using the same y axis

I am trying to build 6 days of intraday data like 6 charts. The Quantmod diagram_Series () function works with the par () parameters. I preloaded the data in bars (XTS object vector), so my code looks like this:

 par(mfrow=c(3,2)) #3 rows, 2 columns for(d in bars){ print(chart_Series(d, type = "candlesticks") ) } 

This works, but each chart has its own Y axis scale. I wanted to set a y range that covers all 6 days, but cannot find a way to do this. I tried this:

 ylim=c(18000,20000) print(chart_Series(d, type = "candlesticks",ylim=ylim) ) 

but it fails with the error "unused arguments (arguments)". yrange = ylim also fails.

I can use chartSeries (d, yrange = ylim) and it works. But as far as I know, I can’t place several diagrams on one display (?). (This may be strictly irrelevant, but suggestions for alternative R packages that can draw beautiful candlestick diagrams allow you to control the y axis and can draw multiple diagrams on the same image are also very welcome.)

+8
r charts quantmod
source share
2 answers

With chartSeries you can set the layout argument to NULL so you don't have to call the layout() command: this is what disables the mfrow parameter.

 library(quantmod) getSymbols("AA") op <- par(mfrow=c(3,2)) for(i in 1:6) { chartSeries( AA["2011-01"], "candlesticks", TA=NULL, # No volume plot layout=NULL, yrange=c(15,18) ) } par(op) 

If you want to save the volume, you can call layout instead of installing mfrow : it does basically the same thing, but allows you to have graphs of different sizes and choose the order in which they are displayed.

 layout( matrix( c( 1, 3, 2, 4, 5, 7, 6, 8, 9, 11, 10, 12 ), nc=2, byrow=TRUE), heights = rep( c(2,1), 3 ) ) #layout.show(12) # To check that the order is as desired for(i in 1:6) { chartSeries( AA[sprintf("2011-%02d",i)], "candlesticks", layout=NULL, yrange=c(15,19) ) } 
+8
source share

Googling, to understand Vincent's answer, led me to the layout () command. This seems incompatible with par (mfrow), but several more experiments have shown that it can be used as an alternative.

 ylim=c(18000,20000) layout(matrix(1:12,nrow=6,ncol=2), height=c(4,2,4,2,4,2)) for(d in bars){ chartSeries(d,layout=NULL,TA=c(addVo(),addBBands()),yrange=ylim) } 

(You will notice that I have added belling bands to make sure that the overlays still work.)

+1
source share

All Articles