[英]Display a rectangle in ggplot with x axis in date format
如何在ggplot中以x轴显示日期格式的矩形?
我知道这段代码:
geom_rect(xmin = 0, xmax = 1, ymin = 0, ymax = 1, fill = "blue")
但是如果x轴是日期格式怎么办? xmin和xmax的语法是什么? 像"2008-05-03 UTC"
这样的东西似乎不起作用。
set.seed(4)
df <- data.frame(date=as.Date(paste0("2017-01-", sprintf("%02d", 1:31))),
val= sample(1:100, 31))
p <- ggplot(df, aes(date, val)) + geom_point()
p + annotate("rect",
xmin = as.Date("2017-01-15"), xmax = as.Date("2017-01-20"),
ymin = -Inf, ymax = Inf, fill = "blue", alpha=.3)
geom_rect
也可以工作,但你需要欺骗代码才能表现出alpha,例如
p + geom_rect(data=df[1,],
aes(xmin = as.Date("2017-01-15"), xmax = as.Date("2017-01-20"),
ymin = -Inf, ymax = Inf),
fill = "blue", alpha=.3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.