xts 图中的文本注释
Text annotation in xts plot
我正在绘制来自 xts 文件的预测和观察 returns。
我一直在尝试使用 text() 函数,但它失败了,因为我的 x 坐标需要一个基于时间的值来绘制它。我正在尝试将每个模型的 RMSE 和准确度值注释到每个图上。
我没有使用 autoplot.zoo() 或 ggplot2,而是使用 xts 的 S3 方法。
非常感谢任何可能的帮助。
情节示例:
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")
尝试使用函数mtext("Text")
。据我了解,您应该在绘图函数 (plotit
) 中使用它,以便在图形区域内获取文本。
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")
根据评论编辑:
在 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)
}
调整函数代码,使函数接受图例所需的变量,一次调用即可。
我正在绘制来自 xts 文件的预测和观察 returns。 我一直在尝试使用 text() 函数,但它失败了,因为我的 x 坐标需要一个基于时间的值来绘制它。我正在尝试将每个模型的 RMSE 和准确度值注释到每个图上。
我没有使用 autoplot.zoo() 或 ggplot2,而是使用 xts 的 S3 方法。
非常感谢任何可能的帮助。
情节示例:
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")
尝试使用函数mtext("Text")
。据我了解,您应该在绘图函数 (plotit
) 中使用它,以便在图形区域内获取文本。
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")
根据评论编辑:
在 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)
}
调整函数代码,使函数接受图例所需的变量,一次调用即可。