R quantmod chartSeries newTA chob - modify legend and axis (primary and secundary)

谁说胖子不能爱 提交于 2019-11-30 10:21:20

After some time learning a little bit more about R internals, S3 and S4 objects, and quantmod package, I've come up with the solution. It can be used to change anything in the graph.

A) If the legend belongs to a secundary indicator window:

  1. Do not print the chartSeries (type option plot = FALSE) and get the returned "chob" object.
  2. In one of the slots of the "chob" object there is a "chobTA" object with 2 params related to legend. Set them to NULL.
  3. Finally, call the hidden function chartSeries.chob

In my case:

#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) In any other case, it is possible to modify "chartSeries.chob", "chartTA", "chartBBands", etc and then call chartSeries.chob

In my case:

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

It is just enough with adding "#" at the beginning of the lines related to legend().

That's it.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!