Enhance cum_ci_plot function with dynamic max values for x-axis breaks

This commit is contained in:
Timon 2026-02-12 08:47:30 +01:00
parent dbc097e42c
commit d81f1a4e5a

View file

@ -442,6 +442,10 @@ cum_ci_plot <- function(pivotName, ci_quadrant_data = CI_quadrant, plot_type = "
"days" = if (facet_on) "Date" else "Age of Crop (Days)", "days" = if (facet_on) "Date" else "Age of Crop (Days)",
"weeks" = "Week Number") "weeks" = "Week Number")
# Calculate dynamic max values for breaks
max_doy <- max(plot_data$DOY, na.rm = TRUE) + 20
max_week <- max(as.numeric(plot_data$week), na.rm = TRUE) + ceiling(20 / 7)
# Create plot with either facets by season or overlay by DOY/week # Create plot with either facets by season or overlay by DOY/week
if (facet_on) { if (facet_on) {
g <- ggplot2::ggplot(data = plot_data) + g <- ggplot2::ggplot(data = plot_data) +
@ -512,9 +516,9 @@ cum_ci_plot <- function(pivotName, ci_quadrant_data = CI_quadrant, plot_type = "
color_scale + color_scale +
{ {
if (x_var == "DOY") { if (x_var == "DOY") {
ggplot2::scale_x_continuous(breaks = seq(0, 450, by = 50), sec.axis = ggplot2::sec_axis(~ . / 30.44, name = "Age in Months", breaks = seq(0, 14, by = 1))) ggplot2::scale_x_continuous(breaks = seq(0, max_doy, by = 50), sec.axis = ggplot2::sec_axis(~ . / 30.44, name = "Age in Months", breaks = seq(0, 14, by = 1)))
} else if (x_var == "week") { } else if (x_var == "week") {
ggplot2::scale_x_continuous(breaks = seq(0, 64, by = 4), sec.axis = ggplot2::sec_axis(~ . / 4.348, name = "Age in Months", breaks = seq(0, 14, by = 1))) ggplot2::scale_x_continuous(breaks = seq(0, max_week, by = 4), sec.axis = ggplot2::sec_axis(~ . / 4.348, name = "Age in Months", breaks = seq(0, 14, by = 1)))
} }
} + } +
ggplot2::theme_minimal() + ggplot2::theme_minimal() +
@ -573,6 +577,10 @@ cum_ci_plot <- function(pivotName, ci_quadrant_data = CI_quadrant, plot_type = "
ggplot2::scale_color_discrete() ggplot2::scale_color_discrete()
} }
# Calculate dynamic max values for breaks
max_doy_both <- max(plot_data_both$DOY, na.rm = TRUE) + 20
max_week_both <- max(as.numeric(plot_data_both$week), na.rm = TRUE) + ceiling(20 / 7)
# Create the faceted plot # Create the faceted plot
g_both <- ggplot2::ggplot(data = plot_data_both) + g_both <- ggplot2::ggplot(data = plot_data_both) +
# Add benchmark lines first (behind season lines) # Add benchmark lines first (behind season lines)
@ -620,9 +628,9 @@ cum_ci_plot <- function(pivotName, ci_quadrant_data = CI_quadrant, plot_type = "
color_scale + color_scale +
{ {
if (x_var == "DOY") { if (x_var == "DOY") {
ggplot2::scale_x_continuous(breaks = seq(0, 450, by = 50), sec.axis = ggplot2::sec_axis(~ . / 30.44, name = "Age in Months", breaks = seq(0, 14, by = 1))) ggplot2::scale_x_continuous(breaks = seq(0, max_doy_both, by = 50), sec.axis = ggplot2::sec_axis(~ . / 30.44, name = "Age in Months", breaks = seq(0, 14, by = 1)))
} else if (x_var == "week") { } else if (x_var == "week") {
ggplot2::scale_x_continuous(breaks = seq(0, 64, by = 4), sec.axis = ggplot2::sec_axis(~ . / 4.348, name = "Age in Months", breaks = seq(0, 14, by = 1))) ggplot2::scale_x_continuous(breaks = seq(0, max_week_both, by = 4), sec.axis = ggplot2::sec_axis(~ . / 4.348, name = "Age in Months", breaks = seq(0, 14, by = 1)))
} else if (x_var == "Date") { } else if (x_var == "Date") {
ggplot2::scale_x_date(breaks = "1 month", date_labels = "%b-%Y", sec.axis = ggplot2::sec_axis(~ ., name = "Age in Months", breaks = scales::breaks_pretty())) ggplot2::scale_x_date(breaks = "1 month", date_labels = "%b-%Y", sec.axis = ggplot2::sec_axis(~ ., name = "Age in Months", breaks = scales::breaks_pretty()))
} }