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は、経験的モードの分解の出力です。データは月単位なので、たとえば1950年1月1日から1950年12月までをハイライトしたいと思います。

4

3 に答える 3

14

アルファ透明度の使用:

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 に答える