Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions R/generic_plotting.R
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Expand Down
89 changes: 73 additions & 16 deletions R/specific_plotting_dynamic.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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),
Expand All @@ -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(
Expand All @@ -208,14 +256,16 @@ 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)
) +
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") +
Expand All @@ -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: ",
Expand All @@ -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(
Expand All @@ -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),
Expand All @@ -279,14 +332,15 @@ 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")

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(
Expand All @@ -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),
Expand All @@ -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(
Expand Down
24 changes: 21 additions & 3 deletions man/specific_dynamic_plots.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading