12

在一个通用图上,时间在 x 轴上,我想强调一些特定年份的时期。

我怎样才能最好地做到这一点?例如,我的想法是在情节后面为突出显示的年份设置一个浅黄色条。

我现在拥有的情节代码:

pdf("temperature_imfs_big_interm5.pdf", width=6, height=8);
par(mfrow=c(temperature$bigEmdIm5$nimf+1,1), mar=c(2,1,2,1))
for(i in 1:temperature$bigEmdIm5$nimf) {
    plot(timeline$big, temperature$bigEmdIm5$imf[,i], type="l", xlab="", ylab="", ylim=range(temperature$bigEmdIm5$imf[,i]), axes=FALSE, main=paste(i, "-th IMF", sep=""))#; abline(h=0)
  axis.POSIXct(side=1, at=tickpos$big)
}
plot(timeline$big, temperature$bigEmdIm5$residue, xlab="", ylab="", axes=FALSE, main="residue", type="l")
axis.POSIXct(side=1, at=tickpos$big)
dev.off();

其中 temperature$bigEmdIm5 是经验模式分解的输出。数据以月为单位,因此我想突出显示 01/1950 到 12/1950 之间的时间。

4

3 回答 3

14

使用 alpha 透明度:

x <- seq(as.POSIXct("1949-01-01", tz="GMT"), length=36, by="months")
y <- rnorm(length(x))

plot(x, y, type="l", xaxt="n")
rect(xleft=as.POSIXct("1950-01-01", tz="GMT"),
     xright=as.POSIXct("1950-12-01", tz="GMT"),
     ybottom=-4, ytop=4, col="#123456A0") # use alpha value in col
idx <- seq(1, length(x), by=6)
axis(side=1, at=x[idx], labels=format(x[idx], "%Y-%m"))

在此处输入图像描述

或在线条后面绘制突出显示的区域:

plot(x, y, type="n", xaxt="n")
rect(xleft=as.POSIXct("1950-01-01", tz="GMT"),
     xright=as.POSIXct("1950-12-01", tz="GMT"),
     ybottom=-4, ytop=4, col="lightblue")
lines(x, y)
idx <- seq(1, length(x), by=6)
axis(side=1, at=x[idx], labels=format(x[idx], "%Y-%m"))
box()

在此处输入图像描述

于 2009-12-16T15:28:12.173 回答
6

这是一个使用zoo的解决方案,因为这使得子集变得容易。您也可以对标准索引执行相同操作:

## create a long monthly sequence and a sub-sequence
months <- seq( as.Date("1950-01-01"), as.Date("2009-12-12"), by="month")
subset <- seq( as.Date("1970-01-01"), as.Date("1979-12-31"), by="month")

## generate some random values
set.seed(42)
values <- cumsum(rnorm(length(months)))

## plot as a zoo object, overlay a gray background and overplot a line in red
library(zoo)
Z <- zoo(values, months)
plot(Z)
rect(xleft=head(subset,1), xright=tail(subset,1),
     ybottom=par("usr")[3], ytop=par("usr")[4],
     density=NA, col="lightgray")
lines(Z[subset], col='red')
box()

替代文字
(来源:eddelbuettel.com

通过使用par("usr"),我们避免了对上部和下部区域标记的显式值的需要。索引使zoo查找起点和终点变得容易。这对于不同时间分辨率的数据的工作方式相同。

于 2009-12-16T15:48:25.640 回答
1

您可以使用带有timeSeries 的chartSeries()函数和添加背景突出显示的函数:quantmodxtsaddTA()

addTA(xts(rep(TRUE,length(times)), times), on=-1, col="#333333", border=NA)
于 2009-12-16T14:40:16.700 回答