简体   繁体   English

Rquantmod chart_Series:标题和字幕分配

[英]R quantmod chart_Series: Title and Subtitle allignment

I am having problems with aligning my subtitle in chart_Series . 我在chart_Series调整字幕时遇到问题。 At present it is just writing over the top of the x axis. 目前,它只是在x轴的顶部书写。 Also is it possible to switch off the text that is automatically written at the top of a chart_Series chart so I can replace it with my own 也可以关闭自动写在chart_Series图表顶部的文本,以便我可以用自己的文本替换

  library(quantmod)
  getSymbols("SPY", from="2013-01-01", to=Sys.Date())
  chart_Series(SPY)


  title("S&P Index", sub = "text1\n\text2\ntext3",
  cex.main = 2,   font.main= 4, col.main= "blue",
  cex.sub = 0.75, font.sub = 3, col.sub = "red")

I would be grateful for your help. 谢谢您的帮助。

The 'quantmod' graphics are object-oriented. “ quantmod”图形是面向对象的。 Data is stored in an environment (named 'Env') inside another environment (named whatever you name it, 'cspy' in this case). 数据存储在另一个环境(命名为任何名称,在这种情况下为“ cspy”)中的一个环境(名为“ Env”)中。 Special charting functions are stored with along with the data in a 'proto'-object. 特殊的图表功能与数据一起存储在“原型”对象中。 It is a more object-oriented approach than is used in either the S3 or S4 programming paradigms that are much more common in R. The 'proto'-package should be consulted for more details. 与R中更常见的S3或S4编程范例相比,它是一种更加面向对象的方法。有关更多详细信息,请参考'proto'程序包。 After nosing around the code in chartSeries and the object it creates, I can get the labeling at the top to go away with this: 围绕chartSeries的代码及其创建的对象之后,我可以在顶部获得标签,以消除此问题:

cspy <- chart_Series(SPY, name = NULL)
cspy$Env$actions[[4]] <- NULL
cspy

The 'quantmod' code has this: “ quantmod”代码具有以下内容:

    cs$Env$name <- name
    text.exp <- c(expression(text(1 - 1/3, 0.5, name, font = 2, 
        col = "#444444", offset = 0, cex = 1.1, pos = 4)), 
                  expression(text(NROW(xdata[xsubset]), 
        0.5, paste(start(xdata[xsubset]), end(xdata[xsubset]), 
            sep = " / "), col = 1, adj = c(0, 0), pos = 2)))
    cs$add(text.exp, env = cs$Env, expr = TRUE)

... but I wasn't able to figure out a name for that leaf so I looked at : ...但是我无法弄清楚那片叶子的名字,所以我看了看:

cspy$Env$actions

... and saw that the name and date-range were in the 4th item. ...并看到名称和日期范围在第4项中。 so I just deleted it. 所以我刚刚删除了它。 (To get rid of only the name it is trivial: chart_Series(SPY, name = NULL) . (I don't know if the location of that graphical item in the object will be consistent and I do not see a method for access that object-leaf, so this is possibly an unstable hack.) (要摆脱它的名称,这是微不足道的: chart_Series(SPY, name = NULL) 。(我不知道该图形项在对象中的位置是否一致,并且看不到用于访问该对象的方法)对象叶,因此这可能是不稳定的hack。)

To make room for the margin text (subtitle): 为边距文本(字幕)腾出空间:

 png("out.png")
 myoma <- par("oma")
 myoma[1] <- 3
 par("oma" =myoma)
 cspy
 title("S&P Index",  cex.main = 2,  font.main= 4, col.main= "blue")
   mtext(text= "text1\ntext2\ntext3", side=1, cex = 0.75, font = 3, col = "red",line=7)
 dev.off()

在此处输入图片说明

I am not familiar with chart_Series plot from before. 我不熟悉以前的chart_Series图。 Normally I would have used the plotting parameter mar to increase the margin at the bottom of the plot, to make some more room for the sub-title. 通常我会使用绘图参数mar来增加绘图底部的边距,以便为字幕留出更多空间。 However, I didn't manage to increase the margin that way. 但是,我没有设法以这种方式增加利润。 Instead I had to use oma , to increase the outer margins of the plot. 取而代之的是,我不得不使用oma来增加情节的外部边缘。 I added the sub-titles using mtext , instead of using the sub argument in title . 我使用mtext添加了字幕,而不是使用titlesub参数。 You set the distance from the plot with line . 您可以使用line设置距绘图的距离。 The default chart_Series title is turned off by setting name = NULL . 通过设置name = NULL关闭默认的chart_Series标题。 Please also note the 'Note' in ?chart_Series : "Highly experimental (read: alpha) use with caution.". 另请注意?chart_Series的“注释”:“高度实验性(请阅读:alpha),请谨慎使用。”。 Anyway, 无论如何,

par(oma = c(5, 0, 0, 0))
chart_Series(SPY, name = NULL)

title("S&P Index", cex.main = 2, font.main = 4, col.main = "blue")

mtext(text = "text1\n\text2\ntext3",
      side = 1, line = 9, cex = 0.75, font = 3, col = "red")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM