Skip to content
Closed
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* New functions `mcmc_dots` and `mcmc_dots_by_chain` for dot plots of MCMC draws by @behramulukir (#402)
* Default to `quantiles=100` for all dot plots by @behramulukir (#402)
* Fix `mcmc_areas_ridges()` overlay breaking when `+ scale_y_discrete()` is added (#287)

# bayesplot 1.15.0

Expand Down
29 changes: 23 additions & 6 deletions R/mcmc-intervals.R
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,14 @@ mcmc_areas_ridges <- function(x,
bw = bw, adjust = adjust, kernel = kernel,
n_dens = n_dens, bounds = bounds)

# Encode the intended display order (first parameter at top, last at bottom)
# directly in the factor levels. This way the default scale_y_discrete does
# not require an explicit `limits =` argument to produce the correct ordering,
# so the plot remains correct when users add their own scale_y_discrete (e.g.
# to relabel or reorder the y-axis) — which would otherwise silently discard
# the `limits =` argument and break the layer masking draw order.
data$parameter <- factor(data$parameter, levels = rev(levels(data$parameter)))

datas <- data %>%
split(data$interval)

Expand Down Expand Up @@ -524,7 +532,11 @@ mcmc_areas_ridges <- function(x,

# Draw each ridgeline from top the bottom
layer_list_inner <- list()
par_draw_order <- levels(unique(data$parameter))
# After reversing the factor levels above, the first level is the bottom-most
# parameter and the last is the top-most. `par_draw_order` must go from the
# top-most to the bottom-most so that each successive layer correctly masks
# the outer ridgelines of the parameters below it.
par_draw_order <- rev(levels(unique(data$parameter)))
bg <- bayesplot_theme_get()[["panel.background"]][["fill"]] %||% "white"

for (par_num in seq_along(unique(data$parameter))) {
Expand Down Expand Up @@ -557,11 +569,16 @@ mcmc_areas_ridges <- function(x,
ggplot(datas$outer) +
aes(x = .data$x, y = .data$parameter) +
layer_outer +
scale_y_discrete(limits = unique(rev(data$parameter)),
expand = expansion(
add = c(0, 1.4 + 1/(2 * nlevels(data$parameter))),
mult = c(0.05, 1/(2 * nlevels(data$parameter)))
)) +
# No `limits =` needed: factor levels already encode the correct display
# order so that the default scale_y_discrete produces the right result.
# Omitting `limits =` means a user-supplied scale_y_discrete (e.g. to
# rename labels) will not silently break the overlay.
scale_y_discrete(
expand = expansion(
add = c(0, 1.4 + 1 / (2 * nlevels(data$parameter))),
mult = c(0.05, 1 / (2 * nlevels(data$parameter)))
)
) +
layer_list_inner +
layer_vertical_line +
scale_fill_identity() +
Expand Down
Loading
Loading