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:
我试图将6天的日内数据绘制为6个图表。 Quantmod的实验chart_Series()函数与par()设置一起使用。我已经将数据预加载到条形图(XTS对象的向量)中,所以我的代码如下所示:
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 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:
这有效,但每个图表都有自己不同的y轴刻度。我想设置一个涵盖所有6天的y范围,但找不到办法。我试过这个:
ylim=c(18000,20000)
print(chart_Series(d, type = "candlesticks",ylim=ylim) )
but it fails with the "unused argument(s)" error. yrange=ylim also fails.
但它失败了“未使用的参数”错误。 yrange = ylim也失败了。
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(d,yrange = ylim),它可以工作。但据我所知,我不能在一个显示器中放置多个图表(?)。 (它可能完全是偏离主题的,但是对于可以绘制漂亮的烛台图表,允许y轴控制并且可以在一个图像上绘制多个图表的替代R包的建议也将非常受欢迎。)
2 个解决方案
#1
8
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.
使用chartSeries,可以将layout参数设置为NULL以防止调用layout()命令:这是禁用mfrow设置的原因。
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 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.
如果你想保持音量,你可以调用布局而不是设置mfrow:它基本上做同样的事情,但允许你有不同大小的图并选择它们的绘制顺序。
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)
)
}
#2
1
Googling to understand Vincent's answer led me to the layout() command. It seems incompatible with par(mfrow), but some more experimentation found it can be used as an alternative.
谷歌搜索理解文森特的答案导致我使用layout()命令。它似乎与par(mfrow)不兼容,但更多的实验发现它可以用作替代品。
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'll notice I added bollinger bands too, to be sure overlays still work too.)
(你会发现我也添加了布林带,以确保覆盖层仍然有用。)
#1
8
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.
使用chartSeries,可以将layout参数设置为NULL以防止调用layout()命令:这是禁用mfrow设置的原因。
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 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.
如果你想保持音量,你可以调用布局而不是设置mfrow:它基本上做同样的事情,但允许你有不同大小的图并选择它们的绘制顺序。
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)
)
}
#2
1
Googling to understand Vincent's answer led me to the layout() command. It seems incompatible with par(mfrow), but some more experimentation found it can be used as an alternative.
谷歌搜索理解文森特的答案导致我使用layout()命令。它似乎与par(mfrow)不兼容,但更多的实验发现它可以用作替代品。
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'll notice I added bollinger bands too, to be sure overlays still work too.)
(你会发现我也添加了布林带,以确保覆盖层仍然有用。)