Bayesplot, facet labels, labeller, and label_parsed

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!

@jonah & @tjmahr, any ideas?

1 Like

Thanks for the ping @bbbales2.

@hhau I think you want to use as_labeller() instead of labeller():

# use as_labeller()
my_labeller_one <- as_labeller(
  x = c(
    'alpha' = 'alpha',
    'sigma' = 'epsilon',
    'beta[1]' = 'gamma[0]', # intentional change
    'beta[2]' = 'gamma[2]'
  ), 
  default = label_parsed
)
mcmc_trace(draws, facet_args = list(labeller = my_labeller_one))

Does that make the plot you’re looking for?

2 Likes

Amazing! Thanks!

I have one more query @jonah (sorry!) - I can’t quite get this working with mcmc_rank_overlay:

library(bayesplot)
library(dplyr)
library(ggplot2)

draws <- example_mcmc_draws()
my_labeller <- as_labeller(
  x = c(
    'alpha' = 'alpha',
    'sigma' = 'epsilon',
    'beta[1]' = 'gamma[0]', # intentional change
    'beta[2]' = 'gamma[2]'
  ), 
  default = label_parsed
)

# works as expected
mcmc_combo(
  x = draws, 
  combo = c("dens", "trace"),
  facet_args = list(
    ncol = 1,
    labeller = my_labeller
  )
)

# does not
mcmc_combo(
  x = draws, 
  combo = c("rank_overlay", "trace"),
  facet_args = list(
    ncol = 1,
    labeller = my_labeller
  )
)

Created on 2020-05-19 by the reprex package (v0.3.0)

I’ve got a PR for this now - one moment

1 Like

Thanks, I just noticed that we didn’t add the facet_args argument to mcmc_rank_overlay for some reason.