Search code examples
rstanrstan

Changing the title of individual plots within a panel of traceplots in ggplot2/rstan to greek letters with subscript


I create traceplots of my stanfit objects via mcmc_trace. I want to rename the titles of the traceplots. I already managed to change the title, but I don't know how I can rename the plots to greek letters with subscripts similar to the expression function.

array <- as.array(fit)
array[1,1,1:3]
dimnames(array)[[3]][1:3] <- c("alpha1", "alpha2", "alpha3")

trace <- mcmc_trace(array, pars = c("alpha1", "alpha2", "alpha3")

I want to replace alpha1 with expression(gamma[0]), but it doesn't work.


Solution

  • Okay, so after a little digging, it simply seems like bayesplott::mcmc_trace doesn't have an option for greek lettering.

    However, the much similar mcmcplots::traplot package has an option greek = TRUE. I did a random example for you to see:

    library(mcmcplots)
    
    nc <- 3; nr <- 1000
    pnames <- c(paste('alpha[', 1:2, ']', sep = ''), paste('gamma[1]', sep = ''))
    means <- rpois(10, 20)
    fakemcmc <- coda::as.mcmc.list(
      lapply(1:3, function(i) coda::mcmc(matrix(rnorm(nc*nr, rep(means, each=nr)),
                                                nrow=nr, dimnames=list(NULL,pnames)))))
    traplot(fakemcmc, greek = TRUE)
    

    Which produces this output:

    enter image description here