Change labels in legend

The probit model has 9 categories in the outcome. plotting the marginal_effects shows vales 1-9 in the legend although in the data it is coded as -2, -1.5, -1.0 … 1.5, 2.0
How can I change the legend to the original meaning/label?

Did you use bayesplot to generate this, or did you write your own code? Could you share the call you used to produce it?

I have used this code:
p <- conditional_effects(fit1, “obs”, categorical = TRUE)
p1 <- plot(p, plot=FALSE)[[1]]
p1$labels$fill <- “Horizontal\ndifference”
p1$labels$colour <- “Horizontal\ndifference”
p1 <- p1 + labs(title = “Observer effect for Pelvis Horizontal”, x = “Observer and Measurement”)
p1 <- p1 +
theme(plot.title=element_text(size=14), axis.text=element_text(size=12), axis.title=element_text(size=14), legend.text=element_text(size=12), legend.title = element_text(size = 12))
print(p1)

when I add next code I get an EXTRA legend (in black)
p1 <- p1 + scale_fill_discrete(labels = c("<=-2", “-1.5”, “-1”, “-0.5”, “0”, “0.5”, “1.0”, “1.5”, “>=2”))

I am not sure this will work in this case but try using

scale_color_discrete(labels = c("<=-2", “-1.5”, “-1”, “-0.5”, “0”, “0.5”, “1.0”, “1.5”, “>=2”))

Unfortunately then an EXTRA legend is displayed:

Perhaps @paul.buerkner can advise if there’s any option to plot.brms_conditional_effect that can help solving this.

There should be an example for exactly this in the doc of conditional_effects.

1 Like

Hi Paul,

Unfortunately I can’t find the information needed to adjusted the legends in the vignettes (https://cran.r-project.org/web/packages/brms/vignettes/)

While plotting I also have to adjust the plot information as it is also not possible to adjust this using the ggplot-options:

p1 <- plot(p, plot=FALSE)[[1]]

p1$labels$fill <- “Difference in\nVertical”

p1$labels$colour <- “Difference in\nVertical”

image001.jpg

If you type ?conditional_effects you see in the examples section:

library(ggplot2)
me <- conditional_effects(fit, "zBase:Trt")
plot(me, plot = FALSE)[[1]] + 
  scale_color_grey() +
  scale_fill_grey()

which shows that you need to change both color and fill in the legend.

1 Like

Hi Paul,

Thanks for the prompt reply.

Unfortunately the suggestion doesn’t work:

fit4 <- brm(diff_V1ord ~ 1 + obs + Breed + Sex.MF + (1|AnimalID), data=data6, family= cumulative(“probit”))

summary(fit4)

p <- conditional_effects(fit4, c(“obs”, “Breed”, “Sex.MF”), categorical = TRUE)

p1 <- plot(p, plot=FALSE)[[1]]

p1$labels$fill <- “Difference in\nVertical” # this is the only way to change the legend title

p1$labels$colour <- “Difference in\nVertical”

p1 <- p1 + labs(title = “Observer effect for Pelvis Vertical”, x = “Observer and Measurement”)

p1 <- p1 + scale_color_grey(limits=c( “-2”, “-1.5”, “-1”, “-0.5”, “0”, “0.5”, “1”, “1.5”, “2” )) + scale_fill_grey(limits=c( “-2”, “-1.5”, “-1”, “-0.5”, “0”, “0.5”, “1”, “1.5”, “2”
))

p1

This produces:

When removing the argument “limits” from scale_fill_grey, then the top legend with categroies 1-9 disappears.

Sorry, I’m lost.

Best regards,

Hans

image001.jpg

Ok that is indeed a special issue for ordinal and categorical models. If you want full control over the generated ggplot objects from the start I recommend using the tidybayes package.

1 Like

Had a similar problem, this worked for me:
library(ggpubr)
ggpar(p1, legend.title = “NEW TITLE”)

2 Likes

I am not sure this is very nice to ggplot, but I had the same problem. this worked for me:

p0 <- conditional_effects(M.SE.w, categorical = T, effects = “SpeciesByName”)
p1 <- plot(p0, plot = F)[[1]] +
theme_minimal() +
theme_update(axis.text.x = element_text(angle = 45, hjust = 1))

p3 <- p1 + scale_colour_manual(
values = c(“red”,“orange”,“yellow”,“green”,“blue”,“violet”),
breaks = c(“1”, “2”, “3”,“4”,“5”,“6”),
labels = ScoreText
) +

scale_fill_manual(
values = c(“red”,“orange”,“yellow”,“green”,“blue”,“violet”),
breaks = c(“1”, “2”, “3”,“4”,“5”,“6”),
labels = ScoreText
)

Thank you. It worked for me, too.