Skip to content

Commit

Permalink
new tred argument adds geom_smooth
Browse files Browse the repository at this point in the history
  • Loading branch information
hanneoberman committed Jan 18, 2024
1 parent 3210413 commit f3ea0a0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
26 changes: 19 additions & 7 deletions R/plot_trace.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
#'
#' @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].
#'
#' @examples
#' 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)
Expand Down Expand Up @@ -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(
Expand All @@ -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)
}
4 changes: 3 additions & 1 deletion man/plot_trace.Rd

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

0 comments on commit f3ea0a0

Please sign in to comment.