Order of parameter in bayesplot

Hi, I’m not sure if this is the right forum to ask about bayesplot (1.4.0), but here I go:

So it seems that I can select the parameters, but not the order. If I do this, for example,
mcmc_hist(post_stanfit,pars = c("beta","alpha")

I’ll get first alpha and then beta. I guess there must be a way to use facets to fix this, but I can’t figure out how.


Operating System: Ubuntu 16.04
Interface Version: Rstan 2.17.3
Output of writeLines(readLines(file.path(Sys.getenv(“HOME”), “.R/Makevars”))):

CXXFLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function -Wno-unused-local-typedefs

CXXFLAGS+=-flto -ffat-lto-objects  -Wno-unused-local-typedefs

CXXFLAGS += -Wno-ignored-attributes -Wno-deprecated-declarations

Output of devtools::session_info("rstan”):

Session info --------------------------------------------------------------------------
 setting  value                       
 version  R version 3.4.0 (2017-04-21)
 system   x86_64, linux-gnu           
 ui       RStudio (1.0.153)           
 language en_US                       
 collate  en_US.UTF-8                 
 tz       Europe/Berlin               
 date     2018-03-26                  

Packages ------------------------------------------------------------------------------
 package      * version   date       source         
 BH             1.66.0-1  2018-02-13 cran (@1.66.0-)
 colorspace     1.3-2     2016-12-14 CRAN (R 3.4.0) 
 dichromat      2.0-0     2013-01-24 CRAN (R 3.4.0) 
 digest         0.6.15    2018-01-28 cran (@0.6.15) 
 ggplot2        2.2.1     2016-12-30 CRAN (R 3.4.0) 
 glue           1.2.0     2017-10-29 cran (@1.2.0)  
 graphics     * 3.4.0     2017-04-21 local          
 grDevices    * 3.4.0     2017-04-21 local          
 grid           3.4.0     2017-04-21 local          
 gridExtra      2.3       2017-09-09 cran (@2.3)    
 gtable         0.2.0     2016-02-26 CRAN (R 3.4.0) 
 inline         0.3.14    <NA>       local          
 labeling       0.3       2014-08-23 CRAN (R 3.4.0) 
 lattice        0.20-35   2017-03-25 CRAN (R 3.3.3) 
 lazyeval       0.2.0     2016-06-12 CRAN (R 3.4.0) 
 magrittr       1.5       2014-11-22 CRAN (R 3.4.0) 
 MASS           7.3-47    2017-04-21 CRAN (R 3.4.0) 
 Matrix         1.2-10    2017-04-28 CRAN (R 3.4.0) 
 methods      * 3.4.0     2017-04-21 local          
 munsell        0.4.3     2016-02-13 CRAN (R 3.4.0) 
 plyr           1.8.4     2016-06-08 CRAN (R 3.4.0) 
 RColorBrewer   1.1-2     2014-12-07 CRAN (R 3.4.0) 
 Rcpp           0.12.15   2018-01-20 cran (@0.12.15)
 RcppEigen      0.3.3.4.0 2018-02-07 cran (@0.3.3.4)
 reshape2       1.4.3     2017-12-11 cran (@1.4.3)  
 rlang          0.2.0     2018-02-20 cran (@0.2.0)  
 rstan          2.17.3    2018-01-20 cran (@2.17.3) 
 scales         0.4.1     2016-11-09 CRAN (R 3.4.0) 
 StanHeaders    2.17.2    2018-01-20 cran (@2.17.2) 
 stats        * 3.4.0     2017-04-21 local          
 stats4         3.4.0     2017-04-21 local          
 stringi        1.1.6     2017-11-17 cran (@1.1.6)  
 stringr        1.3.0     2018-02-19 cran (@1.3.0)  
 tibble         1.3.4     2017-08-22 cran (@1.3.4)  
 tools          3.4.0     2017-04-21 local          
 utils        * 3.4.0     2017-04-21 local     

mcmc_hist(post_stanfit, pars = c("alpha", "beta")) + scale_y_discrete(limits = c("beta", "alpha")

may work. If not, some other ggplot2 thing surely does, but I don’t think there is a specifically bayesplot way of doing it.

Definitely a fine place to ask about bayesplot!

So if you do the following

library(rstan)
fit <- stan_demo("eight_schools")
posterior <- as.matrix(fit)

mcmc_hist(posterior, pars = c("mu", "tau"))
mcmc_hist(posterior, pars = c("tau", "mu"))

you get the same plot both times? For me the order changes. If you really do get the same plot both times can you try installing the development version of bayesplot (new release coming soon) with

devtools::install_github("stan-dev/bayesplot")

ok, this worked just fine. I need to investigate what’s going on in my specific (much more complex plotting function that uses mcmc_hist).

Sorry!

No problem. Hope you can sort it out. If not let me know and I can take a look.

Ok, I found the problem and the solution.

The order gets fixed when I add a geom using a dataframe where I have some summary of the posterior. The solution is to have the column Parameter in this dataframe as a factor and not as a character (if it’s a character ggplot uses always alphabetical order).

Thanks for reporting back. I’ve run into that issue with factor vs character many times.