R 使用plot.xts开发版本的自定义绘图功能
我正在构建一个自定义函数,可以自动将图例添加到plot.xts对象中 代码如下:R 使用plot.xts开发版本的自定义绘图功能,r,xts,R,Xts,我正在构建一个自定义函数,可以自动将图例添加到plot.xts对象中 代码如下: library(xts) library(PerformanceAnalytics) data(edhec) R <- edhec[,1:4] chartS <- function(R, y = NULL, multi.panel = FALSE, type = "l",
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)
将代码移到绘图.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)
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)
库(性能分析)
数据(edhec)
R您需要跟踪正在构建的绘图对象,并将其返回以便自动打印。您也不应该访问未报告的对象(xts:::current.xts_chob()
),因为无法保证它们在不同版本之间保持一致
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)
}
chartS您需要跟踪正在构建的绘图对象,并将其返回以便自动打印。您也不应该访问未报告的对象(xts:::current.xts_chob()
),因为无法保证它们在不同版本之间保持一致
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)
}
chartS提供xts
version,我记得我在不久前的devel版本中看到了plot.xts
大量更改/重写(?)。它来自Github上的joshuaulrich/xts。提供xts
version,我记得我见过plot.xts
大量更改/重写(?)在不久前的devel版本中。它来自Github上的joshuaulrich/xts。谢谢Joshua。现在对我来说很好。新版本非常好:)谢谢约书亚。现在对我来说很好。新版本非常好:)