我试图通过以下方式在绘图中添加一条线:
plotXts(my_xts_object$EUR_FX['2005:/'])
lines(my_xts_object$EUR_FX["2005:/"]*0.5)
发生错误:未找到.xts_chob。
在新版本的软件包中会发生这种情况,而在最新版本之前,我可以添加行而不会出现问题。
数据集为:
head(my_xts_object['2005:/'])
EUR_FX EUR_FX_ADJ
2005-01-01 1.3554 1.382508
2005-01-02 1.3554 1.382508
2005-01-03 1.3465 1.373430
2005-01-04 1.3279 1.354458
2005-01-05 1.3262 1.352724
2005-01-06 1.3173 1.343646
dput(head(my_xts_object['2005:/'], 20))
structure(c(1.3554, 1.3554, 1.3465, 1.3279, 1.3262, 1.3173, 1.3054,
1.3054, 1.3054, 1.3074, 1.3107, 1.3255, 1.3224, 1.3112, 1.3112,
1.3112, 1.3067, 1.302, 1.301, 1.2963, 1.382508, 1.382508, 1.37343,
1.354458, 1.352724, 1.343646, 1.331508, 1.331508, 1.331508, 1.333548,
1.336914, 1.35201, 1.348848, 1.337424, 1.337424, 1.337424, 1.332834,
1.32804, 1.32702, 1.322226), class = c("xts", "zoo"), .indexCLASS = c("POSIXct",
"POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "", index = structure(c(1104534000,
1104620400, 1104706800, 1104793200, 1104879600, 1104966000, 1105052400,
1105138800, 1105225200, 1105311600, 1105398000, 1105484400, 1105570800,
1105657200, 1105743600, 1105830000, 1105916400, 1106002800, 1106089200,
1106175600), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(20L,
2L), .Dimnames = list(NULL, c("EUR_FX", "EUR_FX_ADJ")))
我附上完整的代码:
library(xts)
df = read.table(file = "/home/joselucas/Documentos/Upwork/plot_xls/data.csv", sep = ",", header = TRUE)
## Save the date in a separate identifier as character
dates = as.character(df$X)
## Remove date values from table
df$X = NULL
## Create xts object with hyphens to account for your format
my_xts_object = xts:: as.xts(df, as.POSIXct(dates, format="%Y-%m-%d"))
plotXts <- function(xts1, xts2 = NULL, colXts1 = "blue", colXts2 = "red", titleName = NULL, addLegend = TRUE,
addPerc1 = FALSE, addPerc2 = FALSE, majorTick = 5, formatLab = "%Y", cexLabX = .7, colLab1 = "blue",
colLab2 = "red", colAxis1 = gray(.5), colAxis2 = gray(.5), ylim1 = NULL, ylim2 = NULL){
require(xts)
xycoords1 <- xy.coords(.index(xts1), xts1[, 1])
# custom ylim
if(is.null(ylim1)) {
yRange <- range(xycoords1$y, na.rm = TRUE)
} else {
yRange <- ylim1
}
yRange[2] <- 1.15 * diff(yRange) + yRange[1]
plot(xycoords1$x, xycoords1$y, ylim = yRange, bty = "n", type="l", xlab = "",
ylab = "", yaxt = "n", xaxt = "n", main = "", col = colXts1, lwd=1)
box(col = gray(.5))
tickTime <- axTicksByTime(xts1, "years", k = majorTick, format.labels = formatLab)
tickTime2 <- axTicksByTime(xts1, "years", format.labels = formatLab)
tickTime2Adj <- setdiff(tickTime2,tickTime)
#major ticks
axis(1, at = xycoords1$x[tickTime], labels = names(tickTime)[!is.na(names(tickTime))],
las = 1, lwd = 1, col = gray(.5), cex.axis = cexLabX)
#minor ticks
axis(1, at = xycoords1$x[tickTime2Adj], labels = FALSE, tcl = -0.25, col = gray(.5))
tickPos <- axTicks(side = 4)
if(addPerc1) {
labelN <- paste0(tickPos, "%")
} else { labelN <-TRUE}
axis(side = 4, at = tickPos, line = 0, las=1 , labels= labelN, tick=TRUE,
outer=FALSE, cex.axis = .7, font = 2, col.axis = colLab1, col = colAxis1)
if(!is.null(xts2)){
par(new=T)
xycoords2 <- xy.coords(.index(xts2), xts2[, 1])
if(is.null(ylim2)) {
yRange <- range(xycoords2$y, na.rm = TRUE)
} else {
yRange <- ylim2
}
yRange[2] <- 1.15 * diff(yRange) + yRange[1]
plot(xycoords2$x, xycoords2$y, ylim = yRange, bty = "n", type="l", xlab = "",
ylab = "", yaxt = "n", xaxt = "n", main = "", col = colXts2, lwd=1)
tickPos <- axTicks(side = 2)
if(addPerc2) {
labelN <- paste0(tickPos, "%")
} else { labelN <-TRUE}
axis(side = 2, at = tickPos, line = 0, las=1 , labels= labelN, tick=TRUE,
outer=FALSE, cex.axis = .7, font = 2, col.axis = colLab2, col = colAxis2)
# plot annotation
if(is.null(titleName)) titleName <- paste(colnames(xts1), sep=" vs ", colnames(xts2))
} else {
if(is.null(titleName)) titleName <- colnames(xts1)
}
if(addLegend){
legend("top", legend = c(colnames(xts1),colnames(xts2)), lty = 1, horiz = TRUE, col=c(colXts1,colXts2),
cex=.8, bty="n", xjust=0, yjust=0, seg.len=.5)
}
title(titleName, line = 0.5, outer = FALSE, cex.axis = .7, cex.main = 1)
return(NULL)
}
plotXts(my_xts_object$EUR_FX['2005:/'])
lines(my_xts_object$EUR_FX["2005:/"]*0.5)
abline(v=as.POSIXct("2015-01-01"), lty = 2)