突出显示(阴影)特定时间范围内的绘图背景
在一般图上,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();
其中温度$bigEmdIm5 是经验模式分解的输出。数据以月为单位,因此我想突出显示 01/1950 到 12/1950 例如。
On a generic plot, with time on the x-axis, I would like to highlight a period of some specific years.
How can I bestly do this? My idea is for example a light yellow bar for the highlighted years, behind the plot of course.
The plot code I have now:
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();
Where temperature$bigEmdIm5 is the output of emperical mode decompostion. The data is in months, so I would like to higlight 01/1950 until 12/1950 for example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 Alpha 透明度:
或在行后面绘制突出显示的区域:
Using alpha transparency:
or plot highlighted region behind lines:
这是一个使用 zoo 的解决方案,只是因为这使得子集化变得容易。您也可以对标准索引执行相同的操作:
(来源:eddelbuettel.com)
通过使用
par("usr")
我们避免了对上部和下部区域标记的显式值的需要。zoo
索引使查找起点和终点变得容易。对于不同时间分辨率的数据,这将以相同的方式工作。Here is a solution that uses zoo simply because that makes the subsetting easy. You could do the same with standard indexing as well:
(source: eddelbuettel.com)
By using
par("usr")
we avoid the need for explicit values for upper and lower region marks. And thezoo
indexing makes finding the start- and endpoints easy. This would work the same way for data in different time resolutions.您可以将
quantmod
中的chartSeries()
函数与xts
timeSeries 一起使用,并使用addTA()
函数来添加背景突出显示:You can use the
chartSeries()
function inquantmod
with anxts
timeSeries and theaddTA()
function to add the background highlighting: