当 y 大于阈值和值范围时,突出显示时间序列中的区域

问题描述 投票:0回答:1

我需要突出显示时间序列图中变量 1 大于 28.3m 的区域以及变量 2 在 335 和 390 之间的单独图中的区域,然后以与图类似的方式将这些图堆叠在一起在这篇文章中。 (https://stackoverflow.com)。突出显示的区域可能在我的数据的两个图中不对齐(不是问题),但我需要 x 轴上的时间序列来对齐两个图。我的代码突出显示值小于指定阈值或范围的区域。我也无法应用附加链接中的代码。我将非常感谢任何帮助。

我的绘图中面积大于阈值(变量 1)且范围(变量 2)突出显示

# Load packages
library(xts)
#> Warning: package 'xts' was built under R version 4.3.3
#> Loading required package: zoo
#> Warning: package 'zoo' was built under R version 4.3.3
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.3.3
library(gridExtra)
#> Warning: package 'gridExtra' was built under R version 4.3.3

# Time series data every 15 minutes for 2 different variables
start_time <- as.POSIXct("2024-05-01 00:00:00")
end_time <- as.POSIXct("2024-05-02 00:00:00")
time_seq <- seq(from = start_time, to = end_time, by = "15 min")

# Variable 1
variable1 <- rnorm(length(time_seq), mean = 25, sd = 5)
variable1
#>  [1] 31.452204 34.229997 19.047123 26.844743 22.327691 12.682271 20.564659
#>  [8] 26.331974 24.893855 22.597634 24.314774 21.237276 18.785375 31.627823
#> [15] 25.954387 24.307384 21.102954 26.165051 30.292535 17.264886 22.785419
#> [22] 27.522335 26.295807 23.597723 23.657863 23.413000 22.775842 21.728302
#> [29] 26.022966 23.123091 26.817597 24.204606 18.777309 26.390069 26.925893
#> [36] 28.578040 15.606991 19.806388 19.559110 33.273247 19.491936 26.885432
#> [43] 21.011700 21.913628 26.396894 26.332012 25.791075  9.982174 28.319294
#> [50] 21.514951 25.207554 34.536646 31.824424 21.076760 23.427044 25.038913
#> [57] 17.130033 30.236732 28.399196 24.639311 31.297112 21.564490 20.871799
#> [64] 16.418709 21.426628 23.043023 23.958555 20.499409 19.211735 16.667424
#> [71] 30.939044 30.710873 27.214920 26.483750 24.466929 24.234939 22.637392
#> [78] 16.494583 21.445385 19.452421 27.686310 25.870387 23.787408 30.874698
#> [85] 22.071969 23.786988 28.491280 17.214366 27.889996 27.887324 26.264287
#> [92] 23.469014 21.981297 21.467441 30.862239 27.277739 18.249946

# Variable 2
variable2 <- rnorm(length(time_seq), mean = 350, sd = 20)

# dataframe of the 2 variables
df <- data.frame(DateTime = time_seq, Variable1 = variable1, Variable2 = variable2)
head(df)
#>              DateTime Variable1 Variable2
#> 1 2024-05-01 00:00:00  31.45220  353.8642
#> 2 2024-05-01 00:15:00  34.23000  356.6766
#> 3 2024-05-01 00:30:00  19.04712  357.6353
#> 4 2024-05-01 00:45:00  26.84474  361.7499
#> 5 2024-05-01 01:00:00  22.32769  351.2901
#> 6 2024-05-01 01:15:00  12.68227  363.9686

# Highlight areas on plot where Variable 1 > 28.3
plot_var1 <- ggplot(df, aes(x = DateTime, y = Variable1)) +
  geom_line(color = "blue") +
  geom_rect(data = subset(df, Variable1 > 28.3),
            aes(xmin = DateTime, xmax = lead(DateTime), ymin = -Inf, ymax = Inf),
            fill = "lightblue", alpha = 0.3) +
  labs(x = "Time", y = "Variable 1") +
  theme_minimal()

# Highlight areas on plot where Variable 2 is between 335 and 390
plot_var2 <- ggplot(df, aes(x = DateTime, y = Variable2)) +
  geom_line(color = "red") +
  geom_rect(data = subset(df, Variable2 > 335 & Variable2 < 390),
            aes(xmin = DateTime, xmax = lead(DateTime), ymin = -Inf, ymax = Inf),
            fill = "lightpink", alpha = 0.3) +
  labs(x = "Time", y = "Variable 2") +
  theme_minimal()

# Arrange plots in one column and align by x-axis
grid.arrange(plot_var1, plot_var2, ncol = 1)

于 2024 年 5 月 12 日使用 reprex v2.1.0 创建

r time-series highlight
1个回答
0
投票

使用

lead
功能在这里不起作用。您需要为每个日期时间添加和减去少量内容。您添加和减去的量是任意的,但我选择了每个连续时间点之间的中点,即 15 分钟 / 2 = 450 秒。

geom_rect(aes(xmin = DateTime-450, xmax = DateTime+450, ymin = -Inf, ymax = Inf),...

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.