OK, here is a plot that uses @Argyris_Venidis's most recent data, which is a partial version of an intermediate result from the code I posted. (A full reprex that can be copied all at once is at the bottom of this post.)
data stored in depth_plus_temp_long (click to open)
structure(list(Date = structure(c(1696428000, 1696428000, 1696449600,
1696449600, 1696471200, 1696471200, 1696492800, 1696492800, 1696514400,
1696514400, 1696536000, 1696536000, 1696557600, 1696557600, 1696579200,
1696579200, 1696600800, 1696600800, 1696622400, 1696622400, 1696644000,
1696644000, 1696665600, 1696665600, 1696687200, 1696687200, 1696708800,
1696708800, 1696730400, 1696730400, 1696752000, 1696752000, 1696773600,
1696773600, 1696795200, 1696795200, 1696816800, 1696816800, 1696838400,
1696838400, 1696860000, 1696860000, 1696881600, 1696881600, 1696903200,
1696903200, 1696924800, 1696924800, 1696946400, 1696946400, 1696471200,
1696471200, 1696471200, 1696471200, 1696471200, 1696471200, 1696471200,
1696471200, 1696471200, 1696471200, 1696471200, 1696471200, 1696471200,
1696471200, 1696471200, 1696471200, 1696471200, 1696471200, 1696471200,
1696471200, 1696471200, 1696471200, 1696471200, 1696471200, 1696471200,
1696471200, 1696471200, 1696471200, 1696471200, 1696471200, 1696471200,
1696471200, 1696492800, 1696492800, 1696492800, 1696492800, 1696492800,
1696492800, 1696492800, 1696492800, 1696492800, 1696492800, 1696492800,
1696492800, 1696492800, 1696492800, 1696492800, 1696492800, 1696492800,
1696492800), tzone = "", class = c("POSIXct", "POSIXt")), measure = c("depth",
"depth", "depth", "depth", "depth", "depth", "depth", "depth",
"depth", "depth", "depth", "depth", "depth", "depth", "depth",
"depth", "depth", "depth", "depth", "depth", "depth", "depth",
"depth", "depth", "depth", "depth", "depth", "depth", "depth",
"depth", "depth", "depth", "depth", "depth", "depth", "depth",
"depth", "depth", "depth", "depth", "depth", "depth", "depth",
"depth", "depth", "depth", "depth", "depth", "depth", "depth",
"temp", "temp", "temp", "temp", "temp", "temp", "temp", "temp",
"temp", "temp", "temp", "temp", "temp", "temp", "temp", "temp",
"temp", "temp", "temp", "temp", "temp", "temp", "temp", "temp",
"temp", "temp", "temp", "temp", "temp", "temp", "temp", "temp",
"temp", "temp", "temp", "temp", "temp", "temp", "temp", "temp",
"temp", "temp", "temp", "temp", "temp", "temp", "temp", "temp",
"temp", "temp"), extreme = c("min", "max", "min", "max", "min",
"max", "min", "max", "min", "max", "min", "max", "min", "max",
"min", "max", "min", "max", "min", "max", "min", "max", "min",
"max", "min", "max", "min", "max", "min", "max", "min", "max",
"min", "max", "min", "max", "min", "max", "min", "max", "min",
"max", "min", "max", "min", "max", "min", "max", "min", "max",
"min", "max", "min", "max", "min", "max", "min", "max", "min",
"max", "min", "max", "min", "max", "min", "max", "min", "max",
"min", "max", "min", "max", "min", "max", "min", "max", "min",
"max", "min", "max", "min", "max", "min", "max", "min", "max",
"min", "max", "min", "max", "min", "max", "min", "max", "min",
"max", "min", "max", "min", "max"), value = c(0.75, 5, 4.5, 4.5,
3, 4, 4, 6, 6, 6, 4, 6, 5, 6, 5, 20, 20.5, 22.5, 20, 21, 16,
26, 15.5, 23.5, 23, 26, 19, 26, 23.5, 25.5, 18, 27, 24, 26, 17,
27, 14, 25, 12, 26, 25, 26, 17.5, 26.5, 17, 26, 16, 27, 16.5,
26.5, 17.9, 18.1, 17.9, 17.9, 17.9, 17.9, 17.9, 17.9, 17.9, 17.9,
17.7, 18.1, 17.7, 17.9, 17.7, 17.7, 17.7, 17.7, 17.7, 17.7, 18.1,
18.1, 17.9, 17.9, 18.1, 18.1, 17.9, 17.9, 18.1, 18.1, 17.9, 17.9,
18.1, 18.3, 18.1, 17.9, 18.1, 18.1, 18.1, 18.1, 18.1, 18.1, 17.7,
18.3, 17.7, 17.9, 17.7, 17.7, 17.7, 17.7)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -100L)) -> depth_plus_temp_long
depth_plus_temp_long
library(tidyverse)
# normalize depth and temperature data to make them comparable
depth_plus_temp_long |>
# perform the following calculations separately for depth and temperature
group_by(measure) |>
mutate(
# negate depth values
value = if_else(measure == 'depth', -value, value),
# create new column with 1 if date is before Oct 9 and NA otherwise
pre_oct9 = case_when(Date < as.Date('2023-10-09') ~ 1),
# create new column with value if date is before Oct 9 and NA otherwise
pre_oct9_values = value * pre_oct9,
# create column with max of values that occur before Oct 9
pre_oct9_max = max(pre_oct9_values, na.rm = T),
# create column with min of all values
oct_min = min(value),
# normalize values relative to min (0) and pre-Oct 9 max (1)
norm_value = (value - oct_min) / (pre_oct9_max - oct_min)
) -> norm_depth_plus_temp_long
# extract the min and pre-Oct 9 max for depth and temperature
norm_depth_plus_temp_long |>
summarise(across(c(pre_oct9_max, oct_min), unique)) -> min_max_table
min_max_table
#> # A tibble: 2 × 3
#> measure pre_oct9_max oct_min
#> <chr> <dbl> <dbl>
#> 1 depth -0.75 -27
#> 2 temp 18.3 17.7
# create function that converts normalized value of temperature to Celsius
rescale_temp <-
function(temp){
min_max_table |>
filter(measure == 'temp') |>
mutate(rescaled = temp * (pre_oct9_max - oct_min) + oct_min) |>
pull(rescaled)
}
# vectorize rescale_temp so it can be used with ggplot()
rescale_temp <- Vectorize(rescale_temp)
# create function that converts normalized value of depth to original depth
rescale_depth <-
function(depth){
min_max_table |>
filter(measure == 'depth') |>
mutate(rescaled = depth * (pre_oct9_max - oct_min) + oct_min) |>
mutate(rescaled = -rescaled) |>
pull(rescaled)
}
# vectorize rescale_depth so it can be used with ggplot()
rescale_depth <- Vectorize(rescale_depth)
# create labels that combine extreme and measure and order them
norm_depth_plus_temp_long |>
ungroup() |>
# create column to combine extreme and measure labels
unite(extreme_measure, c(extreme, measure), sep = ' ', remove = F) |>
# set order of curve labels for color legend
mutate(
extreme_measure =
fct_relevel(
extreme_measure,
c('max temp', 'min temp', 'min depth', 'max depth')
)
) -> norm_depth_plus_temp_long_w_labels
# plot
norm_depth_plus_temp_long_w_labels |>
ggplot() +
geom_line(aes(Date, norm_value, color = extreme_measure)) +
scale_y_continuous(
name = 'Temperature (°C)',
labels = rescale_temp,
breaks = seq(0, 1, by = 0.2),
sec.axis =
dup_axis(
name = "Depth (m)",
labels = rescale_depth,
breaks = seq(0, 1, by = 0.2),
)
) +
scale_color_discrete(name = NULL) +
theme_minimal()
Created on 2024-04-02 with reprex v2.0.2
full reprex (click to open)
structure(list(Date = structure(c(1696428000, 1696428000, 1696449600,
1696449600, 1696471200, 1696471200, 1696492800, 1696492800, 1696514400,
1696514400, 1696536000, 1696536000, 1696557600, 1696557600, 1696579200,
1696579200, 1696600800, 1696600800, 1696622400, 1696622400, 1696644000,
1696644000, 1696665600, 1696665600, 1696687200, 1696687200, 1696708800,
1696708800, 1696730400, 1696730400, 1696752000, 1696752000, 1696773600,
1696773600, 1696795200, 1696795200, 1696816800, 1696816800, 1696838400,
1696838400, 1696860000, 1696860000, 1696881600, 1696881600, 1696903200,
1696903200, 1696924800, 1696924800, 1696946400, 1696946400, 1696471200,
1696471200, 1696471200, 1696471200, 1696471200, 1696471200, 1696471200,
1696471200, 1696471200, 1696471200, 1696471200, 1696471200, 1696471200,
1696471200, 1696471200, 1696471200, 1696471200, 1696471200, 1696471200,
1696471200, 1696471200, 1696471200, 1696471200, 1696471200, 1696471200,
1696471200, 1696471200, 1696471200, 1696471200, 1696471200, 1696471200,
1696471200, 1696492800, 1696492800, 1696492800, 1696492800, 1696492800,
1696492800, 1696492800, 1696492800, 1696492800, 1696492800, 1696492800,
1696492800, 1696492800, 1696492800, 1696492800, 1696492800, 1696492800,
1696492800), tzone = "", class = c("POSIXct", "POSIXt")), measure = c("depth",
"depth", "depth", "depth", "depth", "depth", "depth", "depth",
"depth", "depth", "depth", "depth", "depth", "depth", "depth",
"depth", "depth", "depth", "depth", "depth", "depth", "depth",
"depth", "depth", "depth", "depth", "depth", "depth", "depth",
"depth", "depth", "depth", "depth", "depth", "depth", "depth",
"depth", "depth", "depth", "depth", "depth", "depth", "depth",
"depth", "depth", "depth", "depth", "depth", "depth", "depth",
"temp", "temp", "temp", "temp", "temp", "temp", "temp", "temp",
"temp", "temp", "temp", "temp", "temp", "temp", "temp", "temp",
"temp", "temp", "temp", "temp", "temp", "temp", "temp", "temp",
"temp", "temp", "temp", "temp", "temp", "temp", "temp", "temp",
"temp", "temp", "temp", "temp", "temp", "temp", "temp", "temp",
"temp", "temp", "temp", "temp", "temp", "temp", "temp", "temp",
"temp", "temp"), extreme = c("min", "max", "min", "max", "min",
"max", "min", "max", "min", "max", "min", "max", "min", "max",
"min", "max", "min", "max", "min", "max", "min", "max", "min",
"max", "min", "max", "min", "max", "min", "max", "min", "max",
"min", "max", "min", "max", "min", "max", "min", "max", "min",
"max", "min", "max", "min", "max", "min", "max", "min", "max",
"min", "max", "min", "max", "min", "max", "min", "max", "min",
"max", "min", "max", "min", "max", "min", "max", "min", "max",
"min", "max", "min", "max", "min", "max", "min", "max", "min",
"max", "min", "max", "min", "max", "min", "max", "min", "max",
"min", "max", "min", "max", "min", "max", "min", "max", "min",
"max", "min", "max", "min", "max"), value = c(0.75, 5, 4.5, 4.5,
3, 4, 4, 6, 6, 6, 4, 6, 5, 6, 5, 20, 20.5, 22.5, 20, 21, 16,
26, 15.5, 23.5, 23, 26, 19, 26, 23.5, 25.5, 18, 27, 24, 26, 17,
27, 14, 25, 12, 26, 25, 26, 17.5, 26.5, 17, 26, 16, 27, 16.5,
26.5, 17.9, 18.1, 17.9, 17.9, 17.9, 17.9, 17.9, 17.9, 17.9, 17.9,
17.7, 18.1, 17.7, 17.9, 17.7, 17.7, 17.7, 17.7, 17.7, 17.7, 18.1,
18.1, 17.9, 17.9, 18.1, 18.1, 17.9, 17.9, 18.1, 18.1, 17.9, 17.9,
18.1, 18.3, 18.1, 17.9, 18.1, 18.1, 18.1, 18.1, 18.1, 18.1, 17.7,
18.3, 17.7, 17.9, 17.7, 17.7, 17.7, 17.7)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -100L)) -> depth_plus_temp_long
library(tidyverse)
# normalize depth and temperature data to make them comparable
depth_plus_temp_long |>
# perform the following calculations separately for depth and temperature
group_by(measure) |>
mutate(
# negate depth values
value = if_else(measure == 'depth', -value, value),
# create new column with 1 if date is before Oct 9 and NA otherwise
pre_oct9 = case_when(Date < as.Date('2023-10-09') ~ 1),
# create new column with value if date is before Oct 9 and NA otherwise
pre_oct9_values = value * pre_oct9,
# create column with max of values that occur before Oct 9
pre_oct9_max = max(pre_oct9_values, na.rm = T),
# create column with min of all values
oct_min = min(value),
# normalize values relative to min (0) and pre-Oct 9 max (1)
norm_value = (value - oct_min) / (pre_oct9_max - oct_min)
) -> norm_depth_plus_temp_long
# extract the min and pre-Oct 9 max for depth and temperature
norm_depth_plus_temp_long |>
summarise(across(c(pre_oct9_max, oct_min), unique)) -> min_max_table
min_max_table
#> # A tibble: 2 × 3
#> measure pre_oct9_max oct_min
#> <chr> <dbl> <dbl>
#> 1 depth -0.75 -27
#> 2 temp 18.3 17.7
# create function that converts normalized value of temperature to Celsius
rescale_temp <-
function(temp){
min_max_table |>
filter(measure == 'temp') |>
mutate(rescaled = temp * (pre_oct9_max - oct_min) + oct_min) |>
pull(rescaled)
}
# vectorize rescale_temp so it can be used with ggplot()
rescale_temp <- Vectorize(rescale_temp)
# create function that converts normalized value of depth to original depth
rescale_depth <-
function(depth){
min_max_table |>
filter(measure == 'depth') |>
mutate(rescaled = depth * (pre_oct9_max - oct_min) + oct_min) |>
mutate(rescaled = -rescaled) |>
pull(rescaled)
}
# vectorize rescale_depth so it can be used with ggplot()
rescale_depth <- Vectorize(rescale_depth)
# create labels that combine extreme and measure and order them
norm_depth_plus_temp_long |>
ungroup() |>
# create column to combine extreme and measure labels
unite(extreme_measure, c(extreme, measure), sep = ' ', remove = F) |>
# set order of curve labels for color legend
mutate(
extreme_measure =
fct_relevel(
extreme_measure,
c('max temp', 'min temp', 'min depth', 'max depth')
)
) -> norm_depth_plus_temp_long_w_labels
# plot
norm_depth_plus_temp_long_w_labels |>
ggplot() +
geom_line(aes(Date, norm_value, color = extreme_measure)) +
scale_y_continuous(
name = 'Temperature (°C)',
labels = rescale_temp,
breaks = seq(0, 1, by = 0.2),
sec.axis =
dup_axis(
name = "Depth (m)",
labels = rescale_depth,
breaks = seq(0, 1, by = 0.2),
)
) +
scale_color_discrete(name = NULL) +
theme_minimal()
Created on 2024-04-02 with reprex v2.0.2