R quantmod chartSeries newTA chob - 修改图例和轴(主要和次要) [英] R quantmod chartSeries newTA chob - modify legend and axis (primary and secundary)

查看:60
本文介绍了R quantmod chartSeries newTA chob - 修改图例和轴(主要和次要)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个高级问题.

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

I use my own layout for the chartSeries quantmod function, and I can even create my own newTA. Everything works fine. But ...

我想做但我做不到的:

a) 操作 3 个图表中每一个的图例:- 移动到另一个角落,(从左上角"到右上角")- 更改内容- 如果需要,完全删除...

a) Manipulate the legend of each of the 3 charts: - move to other corner, (from "topleft" to "topright") - change the content - remove completely if needed ...

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

b) My indicator generates 2 legends: value1 value2 same as above ... how could I modify them? how could I delete them?

c) 控制 yaxis 的位置和范围(将其放在左侧/右侧甚至删除它们当图形上有次轴时相同

c) control position and range of yaxis (place it on the left / right or even remove them same when there is a secundary axis on the graph

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

d) Modify main legend (the one in the top right where is written the range of dates

工作示例代码:

# 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.name(对输出的控制非常有限).我查看了chartSeries返回的chob对象,但不知道下一步要做什么...

I have tried using legend command, and also the option legend.name (with very limited control of the output). I have had a look at the chob object returned by chartSeries, but I can't figure out what to do next ...

下图:

推荐答案

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

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) 如果图例属于二级指标窗口:

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

  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

B) In any other case, it is possible to modify "chartSeries.chob", "chartTA", "chartBBands", etc and then call chartSeries.chob

就我而言:

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

在与legend()相关的行的开头添加#"就足够了.

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

就是这样.

这篇关于R quantmod chartSeries newTA chob - 修改图例和轴(主要和次要)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆