使用plot.xts的开发版本的自定义绘图功能

时间:2016-04-03 23:15:04

标签: r xts

我正在构建一个自定义函数,可以自动将图例添加到plot.xts对象中。

代码在这里:

library(xts)
library(PerformanceAnalytics)
data(edhec)
R <- edhec[,1:4]
chartS <- function(R, 
                   y = NULL, 
                   multi.panel = FALSE, 
                   type = "l", 
                   yaxis.same = TRUE, 
                   event.lines = NULL, 
                   event.labels = NULL, 
                   event.col = 1,
                   event.offset = 1.2, 
                   event.pos = 2, 
                   event.srt = 90, 
                   event.cex = 1.5,
                   lty = 1,
                   lwd = 2,
                   legend.loc = NULL, 
                   legend.names = NULL, ...) {

  plot.xts(R, y = y, multi.panel = multi.panel, 
           type = type, yaxis.same = yaxis.same, lty = lty, lwd = lwd, ...)

  plot_object <- xts:::current.xts_chob()

  columns <- plot_object$Env$xdata
  columnnames <- plot_object$Env$column_names

  if(!is.null(event.lines)) {
    # error occurred
    addEventLines(xts(event.labels, as.Date(event.lines)), 
                  offset = event.offset, pos = event.pos, 
                  srt = event.srt, cex = event.cex, col = event.col, ...)
  }

  if(is.null(legend.loc))
    legend.loc <- "topright"  
  if(is.null(legend.names))
    legend.names <- columnnames

  if(!multi.panel)
    addLegend(legend.loc, legend.names, lty = lty, lwd = lwd, ...)
}


# chartS(R)
# chartS(R, lty = 1:4)
chartS(R, multi.panel = TRUE)

当我设置multi.panel = TRUE时,我无法绘制多个没有消息的窗口。但是,如果我删除plot.xts下方的代码或将其移至plot.xts以上,则会再次有效。

删除plot.xts

下面的代码
library(xts)
library(PerformanceAnalytics)
data(edhec)
R <- edhec[,1:4]
chartS <- function(R, 
                   y = NULL, 
                   multi.panel = FALSE, 
                   type = "l", 
                   yaxis.same = TRUE, 
                   event.lines = NULL, 
                   event.labels = NULL, 
                   event.col = 1,
                   event.offset = 1.2, 
                   event.pos = 2, 
                   event.srt = 90, 
                   event.cex = 1.5,
                   lty = 1,
                   lwd = 2,
                   legend.loc = NULL, 
                   legend.names = NULL, ...) {

  plot.xts(R, y = y, multi.panel = multi.panel, 
           type = type, yaxis.same = yaxis.same, lty = lty, lwd = lwd, ...)


}


# chartS(R)
# chartS(R, lty = 1:4)
chartS(R, multi.panel = TRUE)

将代码移至plot.xts

以上
library(xts)
library(PerformanceAnalytics)
data(edhec)
R <- edhec[,1:4]
chartS <- function(R, 
                   y = NULL, 
                   multi.panel = FALSE, 
                   type = "l", 
                   yaxis.same = TRUE, 
                   event.lines = NULL, 
                   event.labels = NULL, 
                   event.col = 1,
                   event.offset = 1.2, 
                   event.pos = 2, 
                   event.srt = 90, 
                   event.cex = 1.5,
                   lty = 1,
                   lwd = 2,
                   legend.loc = NULL, 
                   legend.names = NULL, ...) {


  columns <- ncol(R)
  columnnames <- colnames(R)

  if(!is.null(event.lines)) {
    # error occurred
    addEventLines(xts(event.labels, as.Date(event.lines)), 
                  offset = event.offset, pos = event.pos, 
                  srt = event.srt, cex = event.cex, col = event.col, ...)
  }

  if(is.null(legend.loc))
    legend.loc <- "topright"  
  if(is.null(legend.names))
    legend.names <- columnnames

  if(!multi.panel)
    addLegend(legend.loc, legend.names, lty = lty, lwd = lwd, ...)

  plot.xts(R, y = y, multi.panel = multi.panel, 
           type = type, yaxis.same = yaxis.same, lty = lty, lwd = lwd, ...)
}


# chartS(R)
# chartS(R, lty = 1:4)
chartS(R, multi.panel = TRUE)

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

您需要跟踪正在构建的绘图对象,并将其返回以便自动打印。您也不应该访问未导出的对象(chartS <- function(R, y = NULL, multi.panel = FALSE, type = "l", yaxis.same = TRUE, event.lines = NULL, event.labels = NULL, event.col = 1, event.offset = 1.2, event.pos = 2, event.srt = 90, event.cex = 1.5, lty = 1, lwd = 2, legend.loc = NULL, legend.names = NULL, ...) { plot_object <- plot.xts(R, y = y, multi.panel = multi.panel, type = type, yaxis.same = yaxis.same, lty = lty, lwd = lwd, ...) columns <- plot_object$Env$xdata columnnames <- plot_object$Env$column_names if(!is.null(event.lines)) { plot_object <- addEventLines(xts(event.labels, as.Date(event.lines)), offset = event.offset, pos = event.pos, srt = event.srt, cex = event.cex, col = event.col, ...) } if(is.null(legend.loc)) legend.loc <- "topright" if(is.null(legend.names)) legend.names <- columnnames if(!multi.panel) plot_object <- addLegend(legend.loc, legend.names, lty = lty, lwd = lwd, ...) return(plot_object) } ),因为不能保证它们在不同版本中保持一致。

{{1}}