基于 x 轴值的 plot() 中的动态背景? [英] Dynamic background in plot() based on x-axis values?

查看:46
本文介绍了基于 x 轴值的 plot() 中的动态背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求实施正是这样,但使用 plot() 而不是 ggplot2.甚至有人在博客上回复说他们用情节做了这个,但他们代码的链接已经死了.我实际上是在尝试做完全相同的事情.我已经使用 quantmod 的 getSymbols() 从 FRED 下载了数据,并且我在 df 中有这些数据,我正在用 plot() 绘制它.x 轴是日期,我想根据一组特定的日期更改背景颜色.任何想法或提示我如何用情节做到这一点?

I'm looking to implement EXACTLY this, but using plot() instead of ggplot2. There is even a reply on the blog by someone saying they did this with plot, but the link to their code is dead. I'm literally trying to do the exact same thing. I've downloaded data from FRED using quantmod's getSymbols(), and I have that data in a df, which I'm plotting with plot(). The x-axis are dates, and I want to change the background color based on a specific set of dates. Any ideas or hints how I can do this with plot?

谢谢!

推荐答案

基于这个问题:R:仅更改特定区域的绘图背景颜色(基于 x 值)

## plotting area with no axes
plot(unrate.df, type = "n")
lim <- par("usr")
## adding one rectangle
for (i in 1:nrow(recessions.trim)) {
     rect(recessions.trim[i, 1], lim[3], 
          recessions.trim[i, 2], lim[4], border = "pink", col = "pink")
}
## adding the data
lines(unrate.df)
box()

这篇关于基于 x 轴值的 plot() 中的动态背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆