r - 如何在现有地块上叠加一个框?
问题描述
我试图在特定的 x = 日期和 y = 价格的图上绘制一个框。
我有多个 Date 条目存储在 specificDates 中,但即使代码可以运行并且不输出任何错误,该框也不会显示在图上。
dataDate <- as.Date(c("2015-01-01","2016-03-01","2018-06-01","2020-08-01"))
dataPrice <- c(170, 320, 7000,8000)
dummyData <- data.frame(dataDate, dataPrice)
specificDates <- as.Date(c("2016-07-15", "2020-05-20"))
plot_linPrice <- ggplot(data = dummyData,
mapping = aes(x = dataDate, y = dataPrice)) +
geom_line() +
scale_y_log10() +
geom_vline(xintercept = as.numeric(specificDates), color = "blue", lwd = .5) #+ #uncommenting + brings up error
geom_rect(aes(xmin = "2015-01-01", xmax = "2015-06-01", ymin = 5000, ymax = 8000), fill = "blue")
print(plot_linPrice)
解决方案
试试这个:
library(ggplot2)
#Data
dataDate <- as.Date(c("2015-01-01","2016-03-01","2018-06-01","2020-08-01"))
dataPrice <- c(170, 320, 7000,8000)
dummyData <- data.frame(dataDate, dataPrice)
specificDates <- as.Date(c("2016-07-15", "2020-05-20"))
#Code
ggplot(data = dummyData,
mapping = aes(x = dataDate, y = dataPrice)) +
geom_line() +
scale_y_log10() +
geom_vline(xintercept = as.numeric(specificDates), color = "blue", lwd = .5)+
geom_rect(aes(xmin = as.Date("2015-01-01"), xmax = as.Date("2015-06-01"), ymin = 5000, ymax = 8000), fill = "blue")
输出:
推荐阅读
- python - 无法将字符串列转换为浮点数
- c - 如何在内核模块中获取真实文件名
- debugging - 如何退出 LispWorks REPL 调试器,返回到顶层,只输入一个数字,比如 SBCL?
- angular - 扩展组件上的角注入器
- python - 求树的高度,输入树可以有任意数量的孩子
- html - CSS 底部属性不适用于百分比(仅像素)
- javascript - JavaScript:使用 lodash 在相同的嵌套键之间进行过滤
- java - appium 中的滚动功能以识别列表框/下拉列表中的特定对象
- webhooks - Microsoft Teams 传入 Webhook 无法与 BambooHR Webhook 一起使用
- waf - 将多个文件设置为单个任务生成器生成的源输入