gpt4 book ai didi

R quantmod chartSeries newTA chob - 修改图例和轴(主要和次要)

转载 作者:行者123 更新时间:2023-12-01 16:15:37 24 4
gpt4 key购买 nike

这是一个高级问题。

我为 chartSeries quantmod 函数使用我自己的布局,我什至可以创建我自己的 newTA。一切正常。但是……

我想做却做不到的事:

a) 处理 3 个图表中每个图表的图例: - 移动到另一个角落,(从“topleft”到“topright”) - 更改内容 - 如果需要完全删除......

b) 我的指标生成 2 个图例: 值(value)1 值2 同上...我该如何修改它们?我怎样才能删除它们?

c) 控制yaxis的位置和范围(放在左边/右边 甚至删除它们 图上有副轴时也是如此

d) 修改主图例(右上角那个 日期范围写在哪里

一个有效的示例代码:

# Load Library
library(quantmod)

# Get Data
getSymbols("SPY", src="yahoo", from = "2010-01-01")

# Create my indicator (30 values)
value1 <- rnorm(30, mean = 50, sd = 25)
value2 <- rnorm(30, mean = 50, sd = 25)

# merge with the first 30 rows of SPY
dataset <- merge(first(SPY, n = 30),
value1,
value2)
# **** data has now 8 columns:
# - Open
# - High
# - Low
# - Close
# - Volume
# - Adjusted
# - a (my indicator value 1)
# - b (my indicator value 2)
#

# create my TA function - This could also be achieve using the preFUN option of newTA
myTAfun <- function(a){
# input: a: function will receive whole dataset
a[,7:8] # just return my indicator values
}

# create my indicator to add to chartSeries
newMyTA <- newTA(FUN = myTAfun, # chartSeries will pass whole dataset,
# I just want to process the last 2 columns
lty = c("solid", "dotted"),
legend.name = "My_TA",
col = c("red", "blue")
)

# define my layout
layout(matrix(c(1, 2, 3), 3, 1),
heights = c(2.5, 1, 1.5)
)

# create the chart
chartSeries(dataset,
type = "candlesticks",
main = "",
show.grid = FALSE,
name = "My_Indicator_Name",
layout = NULL, # bypass internal layout
up.col = "blue",
dn.col = "red",
TA = c(newMyTA(),
addVo()
),
plot = TRUE,
theme = chartTheme("wsj")
)

我尝试过使用 legend 命令和选项 legend.name(对输出的控制非常有限)。我查看了 chartSeries 返回的 chob 对象,但我不知道下一步该做什么......

下图:

Graph with indicators

最佳答案

在学习了一些关于 R 内部结构、S3 和 S4 对象以及 quantmod 包的一些时间后,我想出了解决方案。它可用于更改图中的任何内容。

A) 如果图例属于二级指标窗口:

  1. 不打印 chartSeries(类型选项 plot = FALSE)并获取返回的“chob”对象。
  2. 在“chob”对象的槽之一中有一个“chobTA”对象,其中包含 2 个与图例相关的参数。将它们设置为 NULL。
  3. 最后调用隐藏函数chartSeries.chob

以我为例:

#get the chob object
my.chob <- chartSeries(dataset,
type = "candlesticks",
main = "",
show.grid = FALSE,
name = "My_Indicator_Name",
layout = NULL, # bypass internal layout
up.col = "blue",
dn.col = "red",
TA = c(newMyTA(),
addVo()
),
plot = FALSE, # do not plot, just get the chob
#plot = TRUE,
theme = chartTheme("wsj")
)

#if the legend is in a secundary window, and represents
#an indicator created with newTA(), this will work:
my.chob@passed.args$TA[[1]]@params$legend <- NULL
my.chob@passed.args$TA[[1]]@params$legend.name <- NULL
quantmod:::chartSeries.chob(my.chob)

B) 在任何其他情况下,可以修改“chartSeries.chob”、“chartTA”、“chartBBands”等,然后调用 chartSeries.chob

以我为例:

fixInNamespace("chartSeries.chob", ns = "quantmod")
quantmod:::chartSeries.chob(my.chob)

在legend()相关行的开头加上“#”即可。

就是这样。

graph with modified legends on chartSeries graph (quantmod package)

关于R quantmod chartSeries newTA chob - 修改图例和轴(主要和次要),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23090963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com