0
我正在構建一個自動將圖例添加到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, ...)
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)
任何建議?
提供'xts'版本,我記得我前面看過'plot.xts'在devel版本中大量更改/重寫(?)。 – jangorecki
它是xts_0.10-0。 –
它來自Github上的joshuaulrich/xts。 –