根据指标对股票/日期图进行着色
我的问题是我有一些根据日期(字符串)绘制的股票价格(数字)。现在我有一个指示变量。我想对图表上指标为 1 的日期区域进行着色,而不是在指标为 0 时着色。我正在 R 中编程。我知道如何在预先指定的时间间隔内执行此操作,例如 2000-01-01至 2001 年 1 月 1 日。但我有很多指示变量显示 1 的“区间”,我不想自己计算它们。关于如何克服这个障碍有什么想法吗?如果可能的话,我希望通过 ggplot2 进行。
我的数据示例:
data <- data.frame(
date = as.Date(c("2001-01-01", "2001-01-02", "2001-01-03",
"2001-01-04","2001-01-05","2001-01-06", "2001-01-07",
"2001-01-08","2001-01-09","2001-01-10")),
price = c(623.3,515.2,611.0,729.0,843.25,623.3,515.2,611.0,729.0,843.25),
indicator = c(1,1,1,0,0,0,1,1,1,0)
)
注意:我之前曾问过这个问题,但它被标记为重复。我不这么认为,因为我找不到任何其他帖子真正回答了我的问题。我见过其他人有同样的问题,但答案总是指定您自己的阴影范围,这不是这里的目的。
My problem is that I have some stock prices(numbers) that are plotted against dates(strings). Now I have an indicator variable. I would like to shade the area on the graph for those dates where the indicator is 1 and not shade when it is 0. I'm programming in R. I know how to do it for a prespecified interval for example 2000-01-01 to 2001-01-01. But I have many "intervals" where the indicator variable shows 1 and I would prefer not to count them myself. Any ideas on how to overcome this obstacle? I would like it to be through ggplot2 if possible.
Example of my data:
data <- data.frame(
date = as.Date(c("2001-01-01", "2001-01-02", "2001-01-03",
"2001-01-04","2001-01-05","2001-01-06", "2001-01-07",
"2001-01-08","2001-01-09","2001-01-10")),
price = c(623.3,515.2,611.0,729.0,843.25,623.3,515.2,611.0,729.0,843.25),
indicator = c(1,1,1,0,0,0,1,1,1,0)
)
NOTE: I've asked this question before, but it got marked as a duplicate. I do not consider it so, since I cannot find any other post that actually answers my question. I've seen others with the same question, but the answers are always to specify your own range to shade, which it is not the purpose here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案是您需要使用您的数据来创建阴影背景区域。
由 reprex 包 (v2.0.1)
The answer is that you need to use your data to create the shaded background areas.
Created on 2022-03-06 by the reprex package (v2.0.1)