向 Chart_Series 和 add_TA 图中添加垂直线

发布于 2025-01-03 19:16:46 字数 2146 浏览 0 评论 0原文

在约书亚在我之前关于这个“问题”的帖子中的友善回答之后( Quantmod add_TA 和 Chart_Series 的问题 - 调用下一个 add_TA 后线条和文本消失 )我添加了一些“扩展” Quantmod 可以绘制线条/线段和文本。这样做时,我遇到了一个无法解决的问题:

require(quantmod)

add_VerticalLine<-function(xCoordinatesOfLines, on=1, ...) {
    lenv <- new.env()
    lenv$add_verticalline <- function(x, xCoordinatesOfLines, ...) {
        xdata <- x$Env$xdata
        xsubset <- x$Env$xsubset
        xcoords <- seq(1:NROW(xdata[xsubset]))[index(xdata[xsubset]) %in% index(xCoordinatesOfLines[xsubset])]
        abline(v=xcoords, ...)
    }
    mapply(function(name, value) {assign(name,value,envir=lenv)}, names(list(xCoordinatesOfLines=xCoordinatesOfLines, ...)), list(xCoordinatesOfLines=xCoordinatesOfLines, ...))
    exp <- parse(text=gsub("list","add_verticalline", as.expression(substitute(list(x=current.chob(),
                                            xCoordinatesOfLines=xCoordinatesOfLines, ...)))), srcfile=NULL)
    plot_object <- current.chob()
    lenv$xdata <- plot_object$Env$xdata
    plot_object$set_frame(sign(on)*abs(on)+1L)
    plot_object$add(exp,env=c(lenv, plot_object$Env),expr=TRUE)
    plot_object
}

datesForLines <- c("2012-02-06", "2012-02-07")

verticalLines.xts <- xts(rep(0, length(datesForLines)), order.by=as.Date(datesForLines))

SPX <- getSymbols("^GSPC", from="2012-01-01", auto.assign=FALSE)
chart_Series(SPX, subset="2012")
add_VerticalLine(verticalLines.xts, on=1, col=c('red', 'blue'), lwd=2)
add_TA(SMA(SPX))
# Everything is fine up to this point, but, when you execute next line (adding vertical line also to second segment of the graph):
add_VerticalLine(verticalLines.xts, on=2, col=c('blue', 'red'), lwd=2)
# You can see that vertical lines are drawn below the SMA and not visible. There seems to be some layering mechanism I do not understand...

任何有关如何以仅在图形的第二段(在 add_TA 部分,第二段)上可见垂直线的方式执行此操作的想法都非常感谢。

如果仅使用图形基元,我还创建了其他函数来以相同的方式添加文本和线段,以避免重新绘制问题(问题在链接的问题中解释)。如果有人感兴趣我可以分享。

最好的, 萨摩岛

After kind answer from Joshua on my previous post about this "issue" ( Issue with quantmod add_TA and chart_Series - lines and text disappear after next add_TA is called ) I have added some "extensions" to quantmod that enable drawing lines/segments and texts. When doing so I encountered a problem I can not resolve:

require(quantmod)

add_VerticalLine<-function(xCoordinatesOfLines, on=1, ...) {
    lenv <- new.env()
    lenv$add_verticalline <- function(x, xCoordinatesOfLines, ...) {
        xdata <- x$Env$xdata
        xsubset <- x$Env$xsubset
        xcoords <- seq(1:NROW(xdata[xsubset]))[index(xdata[xsubset]) %in% index(xCoordinatesOfLines[xsubset])]
        abline(v=xcoords, ...)
    }
    mapply(function(name, value) {assign(name,value,envir=lenv)}, names(list(xCoordinatesOfLines=xCoordinatesOfLines, ...)), list(xCoordinatesOfLines=xCoordinatesOfLines, ...))
    exp <- parse(text=gsub("list","add_verticalline", as.expression(substitute(list(x=current.chob(),
                                            xCoordinatesOfLines=xCoordinatesOfLines, ...)))), srcfile=NULL)
    plot_object <- current.chob()
    lenv$xdata <- plot_object$Env$xdata
    plot_object$set_frame(sign(on)*abs(on)+1L)
    plot_object$add(exp,env=c(lenv, plot_object$Env),expr=TRUE)
    plot_object
}

datesForLines <- c("2012-02-06", "2012-02-07")

verticalLines.xts <- xts(rep(0, length(datesForLines)), order.by=as.Date(datesForLines))

SPX <- getSymbols("^GSPC", from="2012-01-01", auto.assign=FALSE)
chart_Series(SPX, subset="2012")
add_VerticalLine(verticalLines.xts, on=1, col=c('red', 'blue'), lwd=2)
add_TA(SMA(SPX))
# Everything is fine up to this point, but, when you execute next line (adding vertical line also to second segment of the graph):
add_VerticalLine(verticalLines.xts, on=2, col=c('blue', 'red'), lwd=2)
# You can see that vertical lines are drawn below the SMA and not visible. There seems to be some layering mechanism I do not understand...

Any ideas on how to do this in a way that vertical lines are visible only on second segment of the graph (in the add_TA part, second segment) highly appreciated.

I have also created other functions for adding text and line segments in the same way to avoid replot issues (issue explained in linked question) if only graphics primitives are used. If anybody is interested I can share.

Best,
Samo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文