I use label_parsed
excessively, and I’d like to continue to do so in bayesplot
. I can’t figure out exactly how to do so. Here’s some code showing kinda what I’d like to do:
library(bayesplot)
#> This is bayesplot version 1.7.1
#> - Online documentation and vignettes at mc-stan.org/bayesplot
#> - bayesplot theme set to bayesplot::theme_default()
#> * Does _not_ affect other ggplot2 plots
#> * See ?bayesplot_theme_set for details on theme setting
library(ggplot2)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
draws <- example_mcmc_draws()
mcmc_trace(draws)
my_labeller_one <- labeller(
parameter = c(
'alpha' = 'alpha',
'sigma' = 'epsilon',
'beta[1]' = 'gamma[0]', # intentional change
'beta[2]' = 'gamma[2]' %>%
sapply(function(x) parse(text = x))
)
)
my_labeller_two <- labeller(
parameter = c(
'alpha' = 'alpha',
'sigma' = 'epsilon',
'beta[1]' = 'gamma[0]', # intentional change
'beta[2]' = 'gamma[2]'
)
)
# I cannot figure out how to get these `parsed`?
mcmc_trace(
draws,
facet_args = list(labeller = my_labeller_one)
)
mcmc_trace(
draws,
facet_args = list(labeller = my_labeller_two)
)
As the comment suggests, I can’t figure out how to get these new labels to produce plotmath expressions – I know I can specify facet_args = list(labeller = label_parsed)
to get the original labels rendered as plotmath expressions.
In addition bayesplot::mcmc_combo
suggests I could do the following:
> mcmc_combo(
+ x = draws,
+ combo = c("trace", "rank_hist"),
+ facet_args = list(labeller = my_labeller_two)
+ )
Error in (function (rows = NULL, cols = NULL, scales = "fixed", space = "fixed", :
unused argument (ncol = 1)
but I cannot.
I can sort of achieve what I’m after in a very manual fashion:
recode_vec <- c(
'alpha' = 'alpha',
'sigma' = 'epsilon',
'beta[1]' = 'gamma[0]', # intentional change
'beta[2]' = 'gamma[2]'
)
plot_data <- mcmc_trace_data(draws)
plot_data$parameter <- plot_data$parameter %>%
recode(!!!recode_vec)
ggplot(plot_data) +
geom_line(
aes(
x = iteration,
y = value,
group = chain,
colour = chain
)
) +
facet_wrap(
vars(parameter),
scales = "free",
labeller = label_parsed
) +
scale_colour_manual( # this isn't right, but kinda close
values = as.character(unlist(color_scheme_get(scheme = 'blue', i = 1 : 4)))
)
Created on 2020-05-18 by the reprex package (v0.3.0)
but it is a pain – especially when trying to recreate mcmc_combo
plots. Any suggestions on getting the output I want (changed, plotmath labels, in the form of a vector/function I can pass to facet_args
in bayesplot
) would be great!