R chartSeries用点添加额外的图表

时间:2017-02-10 15:37:01

标签: r charts quantmod candlestick-chart

我对chartSeries中的addTApoints.default / R函数有疑问。我试图在chartSeries下添加一个额外的图表 - 不同颜色的点(颜色从函数中获取并使用points.default),但不幸的是我无法添加这些点addTA。我能够在现有图表中添加一行(两者都不是我想要的)。 points.default函数只是将这些点添加到chartSeries中,这不是我想要的。我正在寻找一个简单的直线点,我可以使用color_fct进行不同的着色,并添加到chartSeries下面。我感谢任何帮助,并提前感谢您!

示例代码:

getSymbols("YHOO")
data <- YHOO
chartSeries(data, type = c("auto", "candlesticks", "matchsticks","bars","line"))
hero<-rep(1,length(data$Close))
c(data, xts(hero))
#addTA provides me with a straight line and the coloring fct is not working
plot(addTA(data$hero,pch = 15,cex = 1.5, on = 2, col = color_fct))
#points.default provides me with perfect coloring, but the points are plotted in the middle of the chart
help<- rep(1, length(data$Close))
points.default(x=(1:length(data$High)),y=help+1, col= color_fct,pch = 15,cex = 1.5)

1 个答案:

答案 0 :(得分:2)

如果您愿意使用较新的chart_Series代替chartSeries

x_ti <- xts(rnorm(NROW(data)), order.by = index(data))
x_ti2 <- xts(rep(1, NROW(data)), order.by = index(data))
x_ti2[1, ] <- 0.5 # work around to get an xts object with all the same values (of 1) to plot if points are not visible on the subplot

chart_Series(data["2017"])
add_TA(x_ti, col = "purple", pch = 15, type = 'p', cex = .7)
# plot straight line in subplot:
add_TA(x_ti2, col = "orange", pch = 9, type = 'p', cex = .7)

enter image description here