r - 在特定时间范围内突出显示(阴影)绘图背景

标签 r plot highlight

在通用图上,时间在 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。

最佳答案

使用 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"))

enter image description here

或在线条后面绘制突出显示的区域:
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()

enter image description here

关于r - 在特定时间范围内突出显示(阴影)绘图背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1915001/

相关文章:

r - Paypal交易成功后如何下载excel文件 R Shiny

python - 提高在 matplotlib 中重绘等高线图的速度

Android ListView 选中项高亮显示

d3.js - 筛选 d3 时如何突出显示分组图表的一部分

c++ - 从 NumericVector 填充 C++ 数组的更好习惯用法

r - 通过新函数传递图形参数

python - 如何仅可视化高相关性以更好地表示巨大的相关矩阵?

PHP搜索文本高亮功能

r - 向数据框中添加多列

python - 使用 pandas dataframe 绘制误差线 matplotlib