R / quantmod:多个图表都使用相同的y轴 [英] R/quantmod: multiple charts all using the same y-axis

查看:311
本文介绍了R / quantmod:多个图表都使用相同的y轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将6天的日内数据绘制为6张图表。 Quantmod的实验chart_Series()函数使用par()设置。我已经预先加载数据到 bars (一个XTS对象的向量),所以我的代码看起来像这样:

I'm trying to plot 6 days of intraday data as 6 charts. Quantmod's experimental chart_Series() function works with par() settings. I've pre-loaded the data into bars (a vector of XTS objects) 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") )
    }

有自己不同的y轴刻度。我想设置一个y范围涵盖所有6天,但是找不到一个方法来做到这一点。我试过这个:

This works, but each chart has its own different 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) )

但是失败并出现unused arguments yrange = ylim也失败。

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

我可以使用chartSeries(d,yrange = ylim),它工作。但据我所知,我不能把多个图表在一个显示器(?)。
(它可能严格地偏离主题,但是对于可以绘制漂亮的烛台图表,允许y轴控制并且可以在一个图像上绘制多个图表的替代R包的建议也将是非常受欢迎的。)


I can use chartSeries(d,yrange=ylim), and it works. But as far as I know I cannot put multiple charts in one display (?). (It might strictly be off-subject, but suggestions for alternative R packages that can draw nice-looking candlestick charts, allow y-axis control and can draw multiple charts on one image would also be very welcome.)

推荐答案

使用 chartSeries ,您可以设置布局参数 NULL 以防止调用 layout()禁用 mfrow 设置。

With chartSeries, you can set the layout argument to NULL to prevent the layout() command from being called: this is what disables the mfrow setting.

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)

volume,你可以调用 layout 而不是设置 mfrow :它基本上是一样的东西,但允许你有地块并选择它们的绘制顺序。

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

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) 
  )
}

这篇关于R / quantmod:多个图表都使用相同的y轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆