Is it possible to change labels (axes names) in conditional_smooths()
function of the brms
package?
- Operating System: macOS Monterey
- brms Version: 2.17.0
Thanks
Is it possible to change labels (axes names) in conditional_smooths()
function of the brms
package?
Thanks
The object returned is a ggplot2 object. It likely has had its axis labels already set, but any you add with +labs(…)
should override & take precedence.
One addendum to @mike-lawrence’s answer: If there is more than one smooth term, conditional_smooths
will return a list of ggplots, one for each term. The example below shows how to edit the labels for a single plot or for all the plots in the list.
library(tidyverse)
library(brms)
library(patchwork) # For plot layouts
# Adapt the example from the help for conditional_smooths()
set.seed(0)
dat <- mgcv::gamSim(1, n = 200, scale = 2)
fit <- brm(y ~ s(x0) + s(x1) + s(x2) + s(x3), data = dat, cores=4)
# Show all smooth terms (returns a list containing four ggplots)
p = plot(conditional_smooths(fit), ask=FALSE, plot=FALSE)
# Lay out all four plots
wrap_plots(p)
# Edit the labels of the first plot
p[[1]] = p[[1]] + labs(x="x0 new label", title="x0 title")
wrap_plots(p)
# Remove y-axis labels from all of the plots
p = map(p, ~.x + labs(y=NULL))
wrap_plots(p)
# Set custom y-axis labels for each plot
custom.labs = paste0("y", 1:4)
p = map(seq_along(p), ~p[[.x]] +
labs(y=custom.labs[.x]) +
theme(axis.title.y=element_text(angle=0, vjust=0.5)))
wrap_plots(p)
Created on 2022-06-24 by the reprex package (v2.0.1)