rggplot2brms

How to plot default plots from brms with ggplot?


I have a bayesian model made using brms:

height_nb <- brms::brm(height_mm ~ dist + altitude + (1|trans.pair), seed = 010123,
                   data = dat, family = negbinomial(link = "log", link_shape = "log"), 
                   # fitting information
                   chains = 3, iter = 5000, warmup = 1000, cores = 4)

A plot can be made using plot(conditional_effects(phyemp_height_nb))[[1]] that shows the effect of dist on height_mm.

plot

How can I plot this in ggplot2 so I can edit aspects of the plot (background, axes text, etc.)?


Solution

  • plot.brmsfit already returns a plot of class ggplot. You can add a theme of your choice or specification like: plot( ..., theme = your_theme_here).

    To customize further, save the multiplot output of conditional_effects(your_fit_here) like so:

    the_plots <- plot(conditional_effects(your_fit_here),
                      ask = FALSE
                      )
    

    ... and pick and style individual plots this way:

    the_plots[['dist']] + ## if 'dist' is the conditional to show
        theme_minimal() + ## e. g. an existing theme of choice
        labs(x = 'the distance',
             y = 'the count',
             title = 'amazing!',
             caption = 'but mind the small print'
             )