【问题标题】:R quantmod chartSeries newTA chob - modify legend and axis (primary and secundary)R quantmod chartSeries newTA chob - 修改图例和轴(主要和次要)
【发布时间】:2014-04-15 17:52:22
【问题描述】:

这是一个高级问题。

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

我想做却做不到的事情:

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

b) 我的指标生成 2 个图例: 价值1 价值2 和上面一样......我怎么能修改它们?我怎样才能删除它们?

c) 控制y轴的位置和范围(放在左/右 甚至删除它们 当图上有第二轴时也一样

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 对象,但我不知道下一步该做什么......

下图:

【问题讨论】:

  • @JoshuaUlrich Quant.SE 已删除。不同的论坛覆盖了不同的用户群,因此我试图最大限度地提高获得回复的机会。
  • 我能理解;但是提及您已交叉发布是有礼貌的,因此一个论坛中的人们不会花时间回答已在另一个论坛中回答的问题。
  • 这真的与Highcharts有关吗? Legend(s?) 看起来不像 Highcharts 之一。无论如何,您无法实时删除图例。但是在 Highcharts 中,您可以使用 yAxis.setExtremes(min, max) 更改 yAxis 上的范围。不幸的是,我不知道如何在 quantmod 中更改它..
  • @PawełFus 删除了 Highcharts 和图例标签。无论如何,我认为这个问题最终会成为深入研究 R 内部结构的邀请......这需要我一段时间,但我会弄清楚的。

标签: r quantmod


【解决方案1】:

在学习了更多关于 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()相关的行的开头加上“#”就足够了。

就是这样。

【讨论】:

  • 我按照你的例子,但是在我执行fixInNamespace("chartSeries.chob", ns = "quantmod") 后跟quantmod:::chartSeries.chob(my.chob) R 返回错误could not find function "chartVo"。即使我对 fixInNamespace 函数没有任何更改,也会发生这种情况。您能否提供任何线索说明为什么会发生这种情况?
猜你喜欢
  • 2013-07-21
  • 1970-01-01
  • 2021-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多