bayesplot
bayesplot copied to clipboard
infer annotation aesthetics from global theme
I was thinking today about how mcmc_intervals() draws a vertical line if the x's contain 0.
library(bayesplot)
library(ggplot2)
x <- example_mcmc_draws()
mcmc_intervals(x)

That vertical line doesn't play well with other themes.
theme_set(theme_grey())
mcmc_intervals(x)

That line color is hard-coded... Specifically in this code.
# faint vertical line at zero if zero is within x_lim
layer_vertical_line <- if (0 > x_lim[1] && 0 < x_lim[2]) {
vline_0(color = "gray90", size = 0.5)
} else {
geom_ignore()
}
But in principle, we could just borrow theme's current gridline color and fatten it.
layer_vertical_line <- if (0 > x_lim[1] && 0 < x_lim[2]) {
# this would change to `theme_bayesplot_get()` if go that route in
# issue 117
t <- theme_get()
# `x %||% y` returns y when x is NULL and x otherwise
color <- t$panel.grid.major$colour %||% "grey92"
minor_size <- t$panel.grid.minor$size %||% .125
major_size <- t$panel.grid.major$size %||% (minor_size * 2)
size <- major_size * 2
vline_0(color = color, size = size)
} else {
geom_ignore()
}
When there are no gridlines, it would default to .5.
theme_set(theme_default())
mcmc_intervals(x)

Otherwise it goes twice the width of the major gridlines.
theme_set(theme_grey())
mcmc_intervals(x)

And generalize to any other gridlined theme.
theme_set(ggthemes::theme_solarized())
mcmc_intervals(x)

I see what you mean about the line not playing well with other themes. This this seems like a good solution.
Let's wait and see what happens here https://github.com/tidyverse/ggplot2/pull/2749 where the ggplot2 authors are sorting out how to make default aesthetics customizable with a plotting theme.
Thanks for the link. If they add that functionality to ggplot2 we should definitely have it play nicely with bayesplot, which would also take care of this early issue I had forgotten about,https://github.com/stan-dev/bayesplot/issues/45, regarding setting more plotting defaults.