r - 如何在 geom_line 下添加区域?
问题描述
我想在同一张图上显示 geom_line 和状态(在向量中)。
以数据为例:
Timestamp;Value;State
20190618;1.2;UP
20190619;1.0;DOWN
20190620;1.1;UP
...
这是我想获得的一个例子:
我知道如何使用 geom_line,我已经尝试使用 geom_area 但这些都没有成功。有什么帮助吗?:-)
解决方案
这是您正在寻找的(替代geom_rect
)吗?
准备示例数据
x <- 1:5
y <- c(4,1,6,2,2)
plot.df <- data.frame(Timestamp=x, Value=y)
情节代码:
library(ggplot2)
ggplot(plot.df, aes(x=Timestamp,y=Value)) +
annotate("rect", xmin = 1, xmax = 2, ymin = -Inf, ymax = Inf,
alpha = .2, fill = "green") +
annotate("rect", xmin = 2, xmax = 3, ymin = -Inf, ymax = Inf,
alpha = .2, fill = "red") +
annotate("rect", xmin = 3, xmax = 4, ymin = -Inf, ymax = Inf,
alpha = .2, fill = "green") +
geom_step(direction = "h") +
theme_classic()
推荐阅读
- microcontroller - 处理 msp430fr5969 上的多优先级中断
- webpack - 是否可以在 JS 脚本中访问 [contenthash] 或 [hash] 或 [chunkhash]?
- google-apps-script - 自动导入html链接
- sql - 如何优化占用高 CPU 资源的 SQL 查询
- flutter - AppBarDesign 无法分配给参数类型“PreferredSizeWidget”
- angular - 为什么 IonicModule 不是每个延迟加载页面模块的一部分
- ios - 如何设置应用程序以从 url iOS 接收 utm 参数?
- javascript - 类似和简化的例子(新手问题)
- c# - C# Winforms:控制组框内控件高度的问题
- php - PhpUnit 当我使用 Mock Builder for Interface 时,我得到了正确的类