Search code examples
rggplot2emmeans

Use variable label for emmip/ggplot prediction plot?


I'm using the emmeans package and the emmip function to plot predicted probabilities from an clmm object. From what I understand emmip uses ggplot under the hood. I am trying to plot predictions across levels of a couple of predictors. This is only a cosmetic problem but is it somehow possible to set (or override) the variable name in the plot to a more readable label value?

I have found a way to add variable labels using the sjlabelled package, similar to what is already implemented in base R for adding labels to variable levels:

library(sjlabelled)
dat$language_oth_home <- set_label(dat$language_oth_home, label = "Other Language at Home") 

but emmip doesn't recognise it.

An example plot I'm wanting to try and automate with correct labelling is:

emmip(mmod_stundvis_fin_em, st_understand_vision_dev_2 ~ visit|language_oth_home|randomization_group) +
  xlab("Visit (0 = Pre-, 1 = Post-)") +
  ylab("Probability") +
  scale_color_manual(name="Response Category",
                 labels=c("Strongly Agree", "Agree", "Disagree", "Strongly Disagree"),
                 values=c("#0371b1", "#91c6de", "#f4a682", "#c90120"))

It would be good to automatically display 'Treatment Group:Control", rather than "randomization_group:Control" as it now stands.

The only alternatives I see are to manually change the text in an image editing app post-hoc (which will be time consuming), or change the actual variable names in the data frame (which is not ideal either as it will break a lot of the code).

enter image description here


Solution

  • One way to do this is by adding the call to facet_wrap yourself, with a custom labeller. At the moment, a call to facet_wrap is hidden under the hood in emmip but you can add a new one and override it. You just need to make sure you have the correct variable.

    Here's an example that replaces the default facet labels ("side: L" and "side: R") with my own text.

    #--- Three-factor example
    noise.lm = lm(noise ~ size * type * side, data = auto.noise)
    
    my_labeller <- as_labeller(function(x){
      return(paste0("Treatment group: ", x))
    })
    
    emmip(noise.lm, type ~ size | side) +
      facet_wrap(~side, labeller = my_labeller)
    

    enter image description here