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

时间:2014-04-15 17:52:23

标签: r quantmod

这是一个高级问题。

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

我想做什么,但我不能:

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

b)我的指标生成2个图例:      值1      值2    与上述相同......我怎么能修改它们?我怎么能删除它们?

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

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

1 个答案:

答案 0 :(得分:2)

经过一段时间了解R内部,S3和S4对象以及quantmod包之后,我想出了解决方案。它可用于更改图表中的任何内容。

A)如果图例属于一个secundary指标窗口:

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

    #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)