diff --git a/R/helpers-gg.R b/R/helpers-gg.R index 953a51d4..5aeaf0cc 100644 --- a/R/helpers-gg.R +++ b/R/helpers-gg.R @@ -10,8 +10,9 @@ #' @examples #' # Draw a vertical line at zero (or do nothing) #' xs <- -2:2 +#' style <- annotation_style() #' maybe_vertical_line <- if (0 > min(xs) && 0 < max(xs)) { -#' vline_0(color = "gray90", linewidth = 0.5) +#' vline_0(color = style$color, linewidth = style$linewidth) #' } else { #' geom_ignore() #' } @@ -75,6 +76,29 @@ force_x_axis_in_facets <- function() { ) } +# Derive annotation line aesthetics from the active theme's gridlines. +# When the theme has visible gridlines, the reference line inherits their +# color at twice the major gridline width. When gridlines are blank (e.g. +# bayesplot's default theme), falls back to a light gray. +annotation_style <- function() { + thm <- bayesplot_theme_get() + grid <- calc_element("panel.grid.major", thm) + if (inherits(grid, "element_blank") || is.null(grid)) { + return(list(color = "gray90", linewidth = 0.5)) + } + minor <- calc_element("panel.grid.minor", thm) + minor_lw <- if (!inherits(minor, "element_blank") && !is.null(minor$linewidth)) { + minor$linewidth + } else { + 0.125 + } + major_lw <- grid$linewidth %||% (minor_lw * 2) + list( + color = grid$colour %||% "gray90", + linewidth = major_lw * 2 + ) +} + no_legend_spacing <- function() { theme(legend.spacing.y = unit(0, "cm")) } diff --git a/R/mcmc-diagnostics.R b/R/mcmc-diagnostics.R index 2727928f..532dd076 100644 --- a/R/mcmc-diagnostics.R +++ b/R/mcmc-diagnostics.R @@ -154,8 +154,9 @@ mcmc_rhat <- function(rhat, ..., size = NULL) { bayesplot_theme_get() if (min(data$value) < 1) { + ref_style <- annotation_style() graph <- graph + - vline_at(1, color = "gray", linewidth = 1) + vline_at(1, color = ref_style$color, linewidth = ref_style$linewidth) } brks <- set_rhat_breaks(data$value) diff --git a/R/mcmc-intervals.R b/R/mcmc-intervals.R index 1ab9298a..3d6c87ee 100644 --- a/R/mcmc-intervals.R +++ b/R/mcmc-intervals.R @@ -222,8 +222,9 @@ mcmc_intervals <- function(x, x_lim[2] <- x_lim[2] + 0.05 * x_range # faint vertical line at zero if zero is within x_lim + ref_style <- annotation_style() layer_vertical_line <- if (0 > x_lim[1] && 0 < x_lim[2]) { - vline_0(color = "gray90", linewidth = 0.5) + vline_0(color = ref_style$color, linewidth = ref_style$linewidth) } else { geom_ignore() } @@ -338,8 +339,9 @@ mcmc_areas <- function(x, x_lim[1] <- x_lim[1] - 0.05 * x_range x_lim[2] <- x_lim[2] + 0.05 * x_range + ref_style <- annotation_style() layer_vertical_line <- if (0 > x_lim[1] && 0 < x_lim[2]) { - vline_0(color = "gray90", linewidth = 0.5) + vline_0(color = ref_style$color, linewidth = ref_style$linewidth) } else { geom_ignore() } @@ -501,8 +503,9 @@ mcmc_areas_ridges <- function(x, x_lim[1] <- x_lim[1] - 0.05 * x_range x_lim[2] <- x_lim[2] + 0.05 * x_range + ref_style <- annotation_style() layer_vertical_line <- if (0 > x_lim[1] && 0 < x_lim[2]) { - vline_0(color = "gray90", linewidth = 0.5) + vline_0(color = ref_style$color, linewidth = ref_style$linewidth) } else { geom_ignore() }