Skip to content
Open
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
26 changes: 25 additions & 1 deletion R/helpers-gg.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
#' }
Expand Down Expand Up @@ -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"))
}
Expand Down
3 changes: 2 additions & 1 deletion R/mcmc-diagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions R/mcmc-intervals.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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()
}
Expand Down
Loading