XTS图中的文本注释

时间:2019-09-23 23:36:10

标签: r plot time-series xts

我正在绘制xts文件中的预测收益和观察收益。 我一直在尝试使用text()函数,但是由于我的x坐标需要基于时间的值来绘制它而失败了。我正在尝试在每个绘图上注释每个模型的RMSE和准确性值。

我不是使用autoplot.zoo()或ggplot2,而是使用x的S3方法。

任何可能的帮助都受到高度赞赏。

情节示例: more details on creating a CLI with TypeScript

30个观察结果的可复制示例:

df.predicted <- structure(list(sp500 = c(0.00308633599736579, -0.0102945228811291, 
0.00272233430531683, -0.0177826247734551, 0.00185037721178295, 
0.00729660312665625, 0.0035578290234174, 0.00332004914966584, 
0.00991542270601058, -0.00602436721304404, 0.0065746732040209, 
0.000327323231059243, 0.00916120966953751, 0.00261789271484347, 
-0.00195588307773482, 0.00300799720194303, -0.0102309926490593, 
0.000652487806356518, 0.00304855048996754, 0.00022826707346593, 
0.00610922625203322, 0.00521084483841704, 0.00692053716928683, 
0.00687297232034695, 0.00446858940132962, 0.0016906968757511, 
-0.00640887732008148, 0.010055598145355, -0.00824235478795256, 
-0.0119505384705456), SVM = c(0.0084652756742501, -0.00528251903114062, 
-0.00528058370312449, 0.0042162911593202, -0.00867227417970532, 
-0.00573439344139216, 0.00189984978464037, 0.0010723209869716, 
0.00602634583657307, 0.00195405519365384, 0.00799871931241332, 
-0.000381055826348525, 0.0086327120285207, 0.000236221915013065, 
-0.00119454332199379, 0.010644620802126, -0.000593540960202922, 
-0.00534767860559181, -0.00540458388523374, 0.00311027237695335, 
0.00174002505960733, -0.00188187724218377, 0.0103728117264802, 
0.00190612474961373, 0.00871609815852685, 9.79783217687676e-05, 
-0.00880817328548864, 0.00474787674193693, -0.00711663449138695, 
-0.00653882507699522)), row.names = c("2003-07-31", "2003-08-01", 
"2003-08-04", "2003-08-05", "2003-08-06", "2003-08-07", "2003-08-08", 
"2003-08-11", "2003-08-12", "2003-08-13", "2003-08-14", "2003-08-15", 
"2003-08-18", "2003-08-19", "2003-08-20", "2003-08-21", "2003-08-22", 
"2003-08-25", "2003-08-26", "2003-08-27", "2003-08-28", "2003-08-29", 
"2003-09-01", "2003-09-02", "2003-09-03", "2003-09-04", "2003-09-05", 
"2003-09-08", "2003-09-09", "2003-09-10"), class = "data.frame")

time <- c("2003-07-31", "2003-08-01", 
"2003-08-04", "2003-08-05", "2003-08-06", "2003-08-07", "2003-08-08", 
"2003-08-11", "2003-08-12", "2003-08-13", "2003-08-14", "2003-08-15", 
"2003-08-18", "2003-08-19", "2003-08-20", "2003-08-21", "2003-08-22", 
"2003-08-25", "2003-08-26", "2003-08-27", "2003-08-28", "2003-08-29", 
"2003-09-01", "2003-09-02", "2003-09-03", "2003-09-04", "2003-09-05", 
"2003-09-08", "2003-09-09", "2003-09-10")

plotit <- function(expression,activation){
  abc <- cbind(xts(expression, order.by = as.POSIXct(time)), xts(df.predicted$sp500, order.by = as.POSIXct(time)))
  plot(abc[,2], col=alpha("black",0.85), lwd=2, main=paste0(activation))
  lines(abc[,1], col=alpha("red",0.65))
}

plotit(df.predicted$SVM, "SVM")

2 个答案:

答案 0 :(得分:1)

plot.xts仍未完全开发。但是有一个叫做rtsplot的漂亮小程序包,可以处理时间序列(和ohlc时间序列)。这适用于基本图形,您可以修改所需的任何内容。我在下面提供了一个示例,其中所有内容都包含在您的plotit函数中。

library(xts)
library(scales)
library(rtsplot)


plotit <- function(expression, activation, textlocation, what_text){
  abc <- cbind(xts(df.predicted$SVM, order.by = as.POSIXct(time)), xts(df.predicted$sp500, order.by = as.POSIXct(time)))
  rtsplot(abc[,2], col=alpha("black",0.85), lwd=2, main=paste0(activation))
  rtsplot.lines(abc[,1], col=alpha("red",0.65)) # add second line
  rtsplot.text(abc[textlocation], what_text, col = "red") # add text based on rownumber.
}

plotit(df.predicted$SVM, "SVM", 25, "text goes here")

enter image description here

基于评论的修改:

在plotit函数中使用text(x = as.POSIXct("2003-08-28"), y = -0.01, labels = "RSquared = 0.5", cex=2)即可使用。该图形在函数内部创建,并且plot.new调用已在plot.it的环境而不是全局环境中调用。这就是为什么在使用plotit后调用text时它不起作用的原因。

plotit <- function(expression,activation){
  abc <- cbind(xts(expression, order.by = as.POSIXct(time)), xts(df.predicted$sp500, order.by = as.POSIXct(time)))
  plot(abc[,2], col=alpha("black",0.85), lwd=2, main=paste0(activation))
  lines(abc[,1], col=alpha("red",0.65))
  text(x = as.POSIXct("2003-08-28"), y = -0.01, labels = "RSquared = 0.5", cex=2)
}

调整功能代码以使功能接受图例所需的变量,您可以一次调用。

enter image description here

答案 1 :(得分:0)

尝试使用功能mtext("Text")。据我了解,您应该在绘图函数(plotit)中使用它,以便使文本进入图形区域。