Enhance cum_ci_plot function with dynamic max values for x-axis breaks
This commit is contained in:
parent
dbc097e42c
commit
d81f1a4e5a
|
|
@ -441,6 +441,10 @@ cum_ci_plot <- function(pivotName, ci_quadrant_data = CI_quadrant, plot_type = "
|
||||||
x_label <- switch(x_unit,
|
x_label <- switch(x_unit,
|
||||||
"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) {
|
||||||
|
|
@ -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() +
|
||||||
|
|
@ -565,14 +569,18 @@ cum_ci_plot <- function(pivotName, ci_quadrant_data = CI_quadrant, plot_type = "
|
||||||
x_label <- switch(x_unit,
|
x_label <- switch(x_unit,
|
||||||
"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")
|
||||||
|
|
||||||
# Choose color palette based on colorblind_friendly flag
|
# Choose color palette based on colorblind_friendly flag
|
||||||
color_scale <- if (colorblind_friendly) {
|
color_scale <- if (colorblind_friendly) {
|
||||||
ggplot2::scale_color_brewer(type = "qual", palette = "Set2")
|
ggplot2::scale_color_brewer(type = "qual", palette = "Set2")
|
||||||
} else {
|
} else {
|
||||||
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()))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue