Convert plot.brms into ggplot object for further processing?


#1

Thanks for the fantastic package which is brms!

I would like to include some of the plots in a figure I am making and wondering how to process plot objects so I use cowplot::plotgrid or RMisc::multiplot to put them into a grid layout?

I tried as.ggplot() and got the following error

Error in UseMethod(“as.grob”) :
no applicable method for ‘as.grob’ applied to an object of class “list”

Thanks in advance.

Please also provide the following information in addition to your question:

  • Operating System: Windows 10
  • brms Version: 2.6.0

#2

The result of plot.brmsfit is hard to transform into ggplot objects. Rather, I would suggest you use stanplot with appropriate type arguments.


#3

Is it possible to create marginal_effects/conditional_effects plots in stanplot? These are the crucial - and very informative - ones.


#4

see ?marginal_effects


#6

You can do something like this:

me <- plot(marginal_effects(YourModel, 
                            effects = "X1:X2"), plot = FALSE)

p <-
  ggplot(data = me$'X1:X2'$data, 
         aes(X1, estimate__)) + OtherGgPlotStuff

#7

Andy, I think your example is not quite working. It would directly be

me <- plot(marginal_effects(YourModel, 
                            effects = "X1:X2"), plot = FALSE)
me[[1]] + OtherGgPlotStuff

as the plot method already returns a list of ggplot objects.


#8

Thank you paul.buerkner and andymilne for your time.

I was able to export the plots as images and grid them using cowplot::plot_grid.

Look forward to exploring more of the excellent brms package!