默认显示最后一个值(是的,很烦人)。您可能需要修改源代码以删除 addTA 中显示的最后一个数字。
我不使用addTA,而是使用add_TA 和chart_Series,因为我认为它们看起来好多了(quantmod 的第二代图表)。这是一个解决方案,它从 add_TA 版本的显示中删除了最后一个数字。但你必须愿意修改源代码。
在 add_TA 中,您需要修改源代码的大约 56-60 行:
替换text.exp,就是这样:
# this is inside add_TA:
if (is.na(on)) {
plot_object$add_frame(ylim = c(0, 1), asp = 0.15)
plot_object$next_frame()
text.exp <- expression(text(x = c(1, 1 + strwidth(name)),
y = 0.3, labels = c(name, round(last(xdata[xsubset]),
5)), col = c(1, col), adj = c(0, 0), cex = 0.9,
offset = 0, pos = 4))
plot_object$add(text.exp, env = c(lenv, plot_object$Env),
进行这些修改:
if (is.na(on)) {
plot_object$add_frame(ylim = c(0, 1), asp = 0.15)
plot_object$next_frame()
text.exp <- expression(text(x = c(strwidth(name)), # <- affects label on the subchart
y = 0.3, labels = name, col = c(col), adj = c(0), cex = 0.9,
offset = 1, pos = 4))
plot_object$add(text.exp, env = c(lenv, plot_object$Env),
expr = TRUE)
...
并将修改后的代码分配给一个新变量,称为add_TA.mine:
add_TA.mine <- function (x, order = NULL, on = NA, legend = "auto", yaxis = list(NULL,
NULL), col = 1, taType = NULL, ...)
{
lenv <- new.env()
lenv$name <- deparse(substitute(x))
lenv$plot_ta <- function(x, ta, on, taType, col = col, ...) {
xdata <- x$Env$xdata
....
[all the code for the rest of the function with modifications]....
}
}
plot_object
}
现在,用修改后的函数运行代码
library(quantmod)
getSymbols("AAPL")
environment(add_TA.mine) <- environment(get("add_TA", envir = asNamespace("quantmod")))
assignInNamespace(x = "add_TA", value = add_TA.mine, ns = "quantmod")
chart_Series(AAPL, subset = "2017")
add_TA(RSI(Cl(AAPL)))
quantmod:::add_TA(RSI(Cl(AAPL)))
你可以看到最后一个值不再打印了:
(您可以在旧的addTA 代码中进行相同类型的更改(如果您真的想坚持旧的情节,可以通过chartSeries)
如果您对更改感到满意,并希望在 add_TA 中永久保留它们,您可以自己重新编译 quantmod 源代码并进行修改(即您需要下载 quantmod 源代码并重新编译包) .如果你把事情弄得一团糟,你总是可以重新下载原始的quandmod源代码。