使用 chartSeries 时,默认情况下它还会在图的左上角显示最后一个值。有什么办法可以防止它这样做吗?
使用 addTA 添加新 TA 时,您可以通过设置参数 legend = ""来避免图上的最后一个值,但前提是您要为 TA 创建新图。如果 TA 在先前绘制的图形上,则无论您在图例参数中放入什么,它都会显示最后一个值。
getSymbols ("AAPL", src = "google")
chartSeries(AAPL)
我可以在这里使用什么来防止它在绘图上打印最后一个值?
addTA(EMA(Cl(AAPL)), on = 1, legend = "")
这仍然会在图的左上角打印最后一个值。奇怪的是,如果您正在绘制这样的新图,它就不会这样做:
addTA(EMA(Cl(AAPL)), legend = "")
默认情况下是这样的,还是我可以做些什么来解决它?
最佳答案
默认情况下显示最后一个值(是的,很烦人)。您可能需要修改源代码以删除 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)
...
并将此修改后的代码分配给一个名为 say
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
源代码。关于R:获取 quantmod 的 chartSeries 和 AddTA 以不显示最后一个值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46980738/