diff --git a/R/generic_plotting.R b/R/generic_plotting.R index c407d23..006e954 100644 --- a/R/generic_plotting.R +++ b/R/generic_plotting.R @@ -186,22 +186,22 @@ plot_situations <- function(..., obs = NULL, obs_sd = NULL, p[[i]] <- switch(item_case, # Dynamic plots: "mixture_versions_overlap" = - plot_dynamic_mixture_versions_overlap(sim_situation, i, + plot_dynamic_mixture_versions_overlap(sim_situation, i, successive, title = plot_title ), "mixture_versions_no_overlap" = - plot_dynamic_mixture_versions(sim_situation, i, title = plot_title), + plot_dynamic_mixture_versions(sim_situation, i, successive, title = plot_title), "mixture_no_versions_overlap" = - plot_dynamic_mixture_overlap(sim_situation, i, title = plot_title), - "mixture_no_versions_no_overlap" = plot_dynamic_mixture(sim_situation, i, + plot_dynamic_mixture_overlap(sim_situation, i, successive, title = plot_title), + "mixture_no_versions_no_overlap" = plot_dynamic_mixture(sim_situation, i, successive, title = plot_title ), "non_mixture_versions_overlap" = - plot_dynamic_versions_overlap(sim_situation, i, title = plot_title), + plot_dynamic_versions_overlap(sim_situation, i, successive, title = plot_title), "non_mixture_versions_no_overlap" = - plot_dynamic_versions(sim_situation, i, title = plot_title), + plot_dynamic_versions(sim_situation, i, successive, title = plot_title), "non_mixture_no_versions_overlap" = - plot_dynamic_overlap(sim_situation, i, title = plot_title), + plot_dynamic_overlap(sim_situation, i, successive, title = plot_title), "non_mixture_no_versions_no_overlap" = plot_dynamic(sim_situation, i, successive, title = plot_title), diff --git a/R/specific_plotting_dynamic.R b/R/specific_plotting_dynamic.R index 7e1d8b8..0599efc 100644 --- a/R/specific_plotting_dynamic.R +++ b/R/specific_plotting_dynamic.R @@ -45,17 +45,12 @@ #' NULL - #' @keywords internal +#' @description Add vertical lines between situations in case of successive situations. #' @rdname specific_dynamic_plots -plot_dynamic <- function(df_data, sit, successive, title = NULL) { - p <- ggplot2::ggplot( - df_data, - ggplot2::aes(x = .data$Date) - ) + - ggplot2::geom_line(ggplot2::aes(y = .data$Simulated)) + - ggplot2::facet_wrap(~ .data$var, scales = "free_y") - +#' @param p A ggplot to modify` +#' @return A ggplot object with vertical lines added if successive situations are provided. +add_vertical_lines <- function(df_data, successive, p) { if (!is.null(successive)) { dates <- unique(df_data$succession_date) dates_vlines <- as.POSIXct(dates, tz = "UTC") @@ -66,6 +61,50 @@ plot_dynamic <- function(df_data, sit, successive, title = NULL) { color = "black" ) } + return(p) +} + +#' @keywords internal +#' @description Make a multiline title for a ggplot object, splitting the title into multiple lines (for successive situations only) if it exceeds a certain character limit. +#' @rdname specific_dynamic_plots +#' @param title A character string representing the title to be split into multiple lines. +#' @param max_char An integer specifying the maximum number of characters per line. Default is 80 +#' @return A character string with newline characters inserted to create a multiline title. +make_multiline_title <- function(title, max_char = 120) { + labels <- strsplit(title, " \\| ")[[1]] + + lines <- character() + current <- labels[1] + + if (length(labels) > 1) { + for (lab in labels[-1]) { + candidate <- paste(current, lab, sep = " | ") + + if (nchar(candidate) <= max_char) { + current <- candidate + } else { + lines <- c(lines, paste0(current, " ...")) + current <- lab + } + } + } + + lines <- c(lines, current) + + paste(lines, collapse = "\n") +} + +#' @keywords internal +#' @rdname specific_dynamic_plots +plot_dynamic <- function(df_data, sit, successive, title = NULL) { + p <- ggplot2::ggplot( + df_data, + ggplot2::aes(x = .data$Date) + ) + + ggplot2::geom_line(ggplot2::aes(y = .data$Simulated)) + + ggplot2::facet_wrap(~ .data$var, scales = "free_y") + + p <- add_vertical_lines(df_data, successive, p) if ("Observed" %in% colnames(df_data)) { p <- p + ggplot2::geom_point(ggplot2::aes(y = .data$Observed), na.rm = TRUE) @@ -81,12 +120,13 @@ plot_dynamic <- function(df_data, sit, successive, title = NULL) { ) } } + title <- make_multiline_title(title) p <- p + ggplot2::ggtitle(title) return(p) } -plot_dynamic_mixture <- function(df_data, sit, title = NULL) { +plot_dynamic_mixture <- function(df_data, sit, successive, title = NULL) { p <- ggplot2::ggplot( df_data, ggplot2::aes( @@ -97,6 +137,7 @@ plot_dynamic_mixture <- function(df_data, sit, title = NULL) { ggplot2::geom_line(ggplot2::aes(y = .data$Simulated)) + ggplot2::facet_wrap(~ .data$var, scales = "free_y") + p <- add_vertical_lines(df_data, successive, p) if ("Observed" %in% colnames(df_data)) { p <- p + ggplot2::geom_point(ggplot2::aes(y = .data$Observed), na.rm = TRUE) @@ -113,13 +154,14 @@ plot_dynamic_mixture <- function(df_data, sit, title = NULL) { } } + title <- make_multiline_title(title) p <- p + ggplot2::ggtitle(title) + ggplot2::labs(colour = "Plant") return(p) } -plot_dynamic_mixture_overlap <- function(df_data, sit, title = NULL) { +plot_dynamic_mixture_overlap <- function(df_data, sit, successive, title = NULL) { p <- ggplot2::ggplot( df_data, ggplot2::aes( @@ -132,6 +174,8 @@ plot_dynamic_mixture_overlap <- function(df_data, sit, title = NULL) { ggplot2::geom_line(ggplot2::aes(y = .data$Simulated)) + ggplot2::facet_wrap(~ .data$group_var, scales = "free") + p <- add_vertical_lines(df_data, successive, p) + if ("Observed" %in% colnames(df_data)) { p <- p + ggplot2::geom_point( ggplot2::aes( @@ -156,6 +200,7 @@ plot_dynamic_mixture_overlap <- function(df_data, sit, title = NULL) { } } + title <- make_multiline_title(title) p <- p + ggplot2::ggtitle(title) + ggplot2::guides( @@ -167,7 +212,7 @@ plot_dynamic_mixture_overlap <- function(df_data, sit, title = NULL) { return(p) } -plot_dynamic_versions <- function(df_data, sit, title = NULL) { +plot_dynamic_versions <- function(df_data, sit, successive, title = NULL) { df_data$Observed_Legend <- "Observed Value" p <- ggplot2::ggplot( df_data, @@ -176,6 +221,8 @@ plot_dynamic_versions <- function(df_data, sit, title = NULL) { ggplot2::geom_line(ggplot2::aes(y = .data$Simulated)) + ggplot2::facet_wrap(~ .data$var, scales = "free") + p <- add_vertical_lines(df_data, successive, p) + if ("Observed" %in% colnames(df_data)) { p <- p + ggplot2::geom_point( ggplot2::aes(y = .data$Observed, shape = .data$Observed_Legend), @@ -196,6 +243,7 @@ plot_dynamic_versions <- function(df_data, sit, title = NULL) { } } + title <- make_multiline_title(title) p <- p + ggplot2::ggtitle(title) + ggplot2::guides( @@ -208,7 +256,7 @@ plot_dynamic_versions <- function(df_data, sit, title = NULL) { return(p) } -plot_dynamic_overlap <- function(df_data, sit, title = NULL) { +plot_dynamic_overlap <- function(df_data, sit, successive, title = NULL) { p <- ggplot2::ggplot( df_data, ggplot2::aes(x = .data$Date, colour = .data$var) @@ -216,6 +264,8 @@ plot_dynamic_overlap <- function(df_data, sit, title = NULL) { ggplot2::geom_line(ggplot2::aes(y = .data$Simulated)) + ggplot2::facet_wrap(~ .data$group_var, scales = "free") + p <- add_vertical_lines(df_data, successive, p) + if ("Observed" %in% colnames(df_data)) { p <- p + ggplot2::labs(shape = "Variable") + @@ -235,13 +285,14 @@ plot_dynamic_overlap <- function(df_data, sit, title = NULL) { ) } } + title <- make_multiline_title(title) p <- p + ggplot2::labs(colour = "Variable") + ggplot2::ggtitle(title) return(p) } -plot_dynamic_mixture_versions_overlap <- function(df_data, sit, title = NULL) { +plot_dynamic_mixture_versions_overlap <- function(df_data, sit, successive, title = NULL) { stop( "Too many cases to consider at a time: mixture + versions + overlap. ", "Please use only a maximum of two combinations of: ", @@ -250,7 +301,7 @@ plot_dynamic_mixture_versions_overlap <- function(df_data, sit, title = NULL) { } -plot_dynamic_versions_overlap <- function(df_data, sit, title = NULL) { +plot_dynamic_versions_overlap <- function(df_data, sit, successive, title = NULL) { p <- ggplot2::ggplot( df_data, ggplot2::aes( @@ -261,6 +312,8 @@ plot_dynamic_versions_overlap <- function(df_data, sit, title = NULL) { ggplot2::geom_line(ggplot2::aes(y = .data$Simulated)) + ggplot2::facet_wrap(~ .data$group_var, scales = "free") + p <- add_vertical_lines(df_data, successive, p) + if ("Observed" %in% colnames(df_data)) { p <- p + ggplot2::geom_point( ggplot2::aes(y = .data$Observed, colour = .data$var), @@ -279,6 +332,7 @@ plot_dynamic_versions_overlap <- function(df_data, sit, title = NULL) { } } + title <- make_multiline_title(title) p <- p + ggplot2::ggtitle(title) + ggplot2::labs(colour = "Variable", linetype = "Version") @@ -286,7 +340,7 @@ plot_dynamic_versions_overlap <- function(df_data, sit, title = NULL) { return(p) } -plot_dynamic_mixture_versions <- function(df_data, sit, title = NULL) { +plot_dynamic_mixture_versions <- function(df_data, sit, successive, title = NULL) { p <- ggplot2::ggplot( df_data, ggplot2::aes( @@ -298,6 +352,8 @@ plot_dynamic_mixture_versions <- function(df_data, sit, title = NULL) { ggplot2::geom_line(ggplot2::aes(y = .data$Simulated)) + ggplot2::facet_wrap(~ .data$var, scales = "free") + p <- add_vertical_lines(df_data, successive, p) + if ("Observed" %in% colnames(df_data)) { p <- p + ggplot2::geom_point( ggplot2::aes(y = .data$Observed), @@ -314,6 +370,7 @@ plot_dynamic_mixture_versions <- function(df_data, sit, title = NULL) { ) } } + title <- make_multiline_title(title) p <- p + ggplot2::ggtitle(title) + ggplot2::labs( diff --git a/man/specific_dynamic_plots.Rd b/man/specific_dynamic_plots.Rd index d213450..28b1ea9 100644 --- a/man/specific_dynamic_plots.Rd +++ b/man/specific_dynamic_plots.Rd @@ -2,32 +2,50 @@ % Please edit documentation in R/specific_plotting_dynamic.R \name{specific_dynamic_plots} \alias{specific_dynamic_plots} +\alias{add_vertical_lines} +\alias{make_multiline_title} \alias{plot_dynamic} \title{Specific functions to generate dynamic plots} \usage{ +add_vertical_lines(df_data, successive, p) + +make_multiline_title(title, max_char = 120) + plot_dynamic(df_data, sit, successive, title = NULL) } \arguments{ \item{df_data}{A named list of data frame including the data to plot (one df per situation)} -\item{sit}{The name of the situation to plot} - \item{successive}{A list of lists containing the situations to be represented as a contiguous sequence} -\item{title}{The plot title (optional, NULL by default)} +\item{p}{A ggplot to modify`} + +\item{title}{A character string representing the title to be split into multiple lines.} + +\item{max_char}{An integer specifying the maximum number of characters per line. Default is 80} + +\item{sit}{The name of the situation to plot} } \value{ A ggplot object A list of ggplot objects + +A ggplot object with vertical lines added if successive situations are provided. + +A character string with newline characters inserted to create a multiline title. } \description{ Generate dynamic plots for the different cases handled in CroPlotR (plant mixture, plot several simulation results on same graph, ...) as specified by the different arguments. + +Add vertical lines between situations in case of successive situations. + +Make a multiline title for a ggplot object, splitting the title into multiple lines (for successive situations only) if it exceeds a certain character limit. } \details{ List of the different specific functions: