diff --git a/R/plot_trace.R b/R/plot_trace.R index ec8f2580..78f20501 100644 --- a/R/plot_trace.R +++ b/R/plot_trace.R @@ -2,6 +2,7 @@ #' #' @param data An object of class [mice::mids]. #' @param vrb String, vector, or unquoted expression with variable name(s), default is "all". +#' @param trend Logical indicating whether a trend line should be displayed. #' #' @return An object of class [ggplot2::ggplot]. #' @@ -9,7 +10,7 @@ #' imp <- mice::mice(mice::nhanes, print = FALSE) #' plot_trace(imp) #' @export -plot_trace <- function(data, vrb = "all") { +plot_trace <- function(data, vrb = "all", trend = FALSE) { verify_data(data, imp = TRUE) if (is.null(data$chainMean) && is.null(data$chainVar)) { cli::cli_abort("No convergence diagnostics found", call. = FALSE) @@ -60,12 +61,12 @@ plot_trace <- function(data, vrb = "all") { )) # plot the convergence diagnostics - ggplot2::ggplot(long, - ggplot2::aes( - x = .data$.it, - y = .data$val, - color = as.factor(.data$.m) - )) + + gg <- ggplot2::ggplot(long, + ggplot2::aes( + x = .data$.it, + y = .data$val, + color = as.factor(.data$.m) + )) + ggplot2::geom_line(linewidth = 0.6) + ggplot2::geom_hline(yintercept = -Inf) + ggplot2::facet_wrap( @@ -88,4 +89,15 @@ plot_trace <- function(data, vrb = "all") { strip.placement = "outside", strip.switch.pad.wrap = ggplot2::unit(0, "cm") ) + if (trend) { + gg <- gg + + ggplot2::geom_smooth( + formula = y ~ x, + method = "loess", + se = FALSE, + color = "black", + linetype = "dashed" + ) + } + return(gg) } diff --git a/man/plot_trace.Rd b/man/plot_trace.Rd index acb4cf34..5176f9ac 100644 --- a/man/plot_trace.Rd +++ b/man/plot_trace.Rd @@ -4,12 +4,14 @@ \alias{plot_trace} \title{Plot the trace lines of the imputation algorithm} \usage{ -plot_trace(data, vrb = "all") +plot_trace(data, vrb = "all", trend = FALSE) } \arguments{ \item{data}{An object of class \link[mice:mids-class]{mice::mids}.} \item{vrb}{String, vector, or unquoted expression with variable name(s), default is "all".} + +\item{trend}{Logical indicating whether a trend line should be displayed.} } \value{ An object of class \link[ggplot2:ggplot]{ggplot2::ggplot}.