I’m trying to highlight the areas in a time series plot where the river level is greater than 28.3m and in a separate plot where the SpCond varies between 335 and 390uS and then stack these plots on top of each other in a similar fashion to the plots in this post. Automatic way to highlight parts of a time series plot that have values higher than a certain threshold? . The greyed areas may not align in the two plots for my data (not a problem) but I need the time series on the x axis to align for both plots. Unfortunately I’ve been unable to apply the code in the post to my dataset and would be very grateful for any assistance.
library(tidyverse)
library(ggplot)
head(Level_Bgrania_HR_V2)
Level
2023-09-14 18:15:00 27.923
2023-09-14 18:30:00 27.923
2023-09-14 18:45:00 27.923
2023-09-14 19:00:00 27.924
2023-09-14 19:15:00 27.923
2023-09-14 19:30:00 27.923
str(Level_Bgrania_HR_V2)
An xts object on 2023-09-14 18:15:00 / 2024-03-01 17:15:00 containing:
Data: double [16220, 1]
Columns: Level
Index: POSIXct,POSIXt [16220] (TZ: "GMT")
#> Error: <text>:3:12: unexpected numeric constant
#> 2: Level
#> 3: 2023-09-14 18
#> ^
Created on 2024-05-10 with reprex v2.1.0
type here
head(UN140923EDV6)
# A tibble: 6 × 11
DateTime Temp SpCond Cond Sal Press Depth pH...8 pH...9 Turbidity Battery
<dttm> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2023-09-14 20:15:00 14.7 399 0.321 0.19 -31.7 0.147 6.15 120. 1.5 12.1
2 2023-09-14 20:30:00 14.7 400 0.321 0.19 -31.5 0.314 6.29 112. 1.5 12.1
3 2023-09-14 20:45:00 14.8 400 0.322 0.19 -31.4 0.371 6.34 110 1.5 12.1
4 2023-09-14 21:00:00 14.8 400 0.322 0.19 -31.3 0.401 6.36 109. 1.5 12.2
5 2023-09-14 21:15:00 14.8 400 0.322 0.19 -31.3 0.417 6.38 108. 1.7 12.1
6 2023-09-14 21:30:00 14.9 400 0.322 0.19 -31.3 0.426 6.39 107. 1.8 12.1
str(UN140923EDV6)
tibble [14,400 × 11] (S3: tbl_df/tbl/data.frame)
$ DateTime : POSIXct[1:14400], format: "2023-09-14 20:15:00" "2023-09-14 20:30:00" "2023-09-14 20:45:00" "2023-09-14 21:00:00" ...
$ Temp : num [1:14400] 14.7 14.7 14.8 14.8 14.8 ...
$ SpCond : num [1:14400] 399 400 400 400 400 400 400 400 400 401 ...
$ Cond : num [1:14400] 0.321 0.321 0.322 0.322 0.322 0.322 0.323 0.323 0.323 0.323 ...
$ Sal : num [1:14400] 0.19 0.19 0.19 0.19 0.19 0.19 0.19 0.19 0.19 0.19 ...
$ Press : num [1:14400] -31.7 -31.5 -31.4 -31.3 -31.3 ...
$ Depth : num [1:14400] 0.147 0.314 0.371 0.401 0.417 0.426 0.429 0.43 0.431 0.433 ...
$ pH...8 : num [1:14400] 6.15 6.29 6.34 6.36 6.38 6.39 6.4 6.41 6.4 6.41 ...
$ pH...9 : num [1:14400] 120 112 110 109 108 ...
$ Turbidity: num [1:14400] 1.5 1.5 1.5 1.5 1.7 1.8 2.1 1.5 1.3 1.5 ...
$ Battery : num [1:14400] 12.1 12.1 12.1 12.2 12.1 12.1 12.1 12.1 12.1 12.1 ...
#> Error: <text>:3:24: unexpected symbol
#> 2: # A tibble: 6 × 11
#> 3: DateTime Temp
#> ^
# Plot the level data
Unshin_LEVEL_PLOT_140923_010324V1<-autoplot(as.zoo(Level_Bgrania_HR_V2), geom = "line")+
scale_x_datetime(labels = date_format("%Y-%m-%d"))+
xlab("Date")+ylab("Level (m)")
# Highlight the data greater than 28.3 m #
Unshin_Level_highlighted<-
Unshin_LEVEL_PLOT_140923_010324V1+
geom_rect(data = Level_Bgrania_140923_010324,
aes(xmin = 28.3, xmax = 28.3, ymin = -Inf, ymax = Inf),
color = 'grey90',
alpha = 0.2)