Change linetype aestetics conditional_effects

Dear all,

I ran a brms model with two continuous predictors and am trying to plot the effect. Specifically, I want to customize the linetype of the predictor to make it photocopy safe.

I took a look at the following topic already: Change aesthetics conditional_effects but there it is only explained how to change the color of the line and the color of the ribbon, but I want to change the linetype of the lines using for example scale_linetype_manual.

I use the reproducible example also used in that topic:

data(mtcars)
mtcars$carb<- as.ordered(mtcars$carb)
model<-brm(carb~hp, data=mtcars, family=sratio)

#creating conditional effects object
c_eff <- conditional_effects(model, categorical = T)

#creating plot
ugly_plot <- plot(c_eff, plot = FALSE)[[1]] + 
scale_linetype_manual(values = c("1"= "solid", 
                             "2"= "dashed", 
                             "3"= "dotted",
                             "4" = "twodash",
                             "5" = "dotdash",
                             "6" = "longdash")) +
scale_fill_manual(values = c("1"= "green", 
                             "2"= "#7570b3", 
                             "3"= "#1b9e77",
                             "4" = "grey20",
                             "5" = "blue",
                             "6" = "grey80")) +
scale_color_manual(values = c("1"= "black", 
                      "2"= "black", 
                      "3"= "orange",
                      "4" = "grey20",
                      "5" = "blue",
                       "6" = "grey80")) 
ugly_plot 

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

  • Operating System: Windows 10
  • brms Version: 2.11.1
1 Like

I have found a simple yet elegant solution to this problem by converting the list produced by conditional_effects into a dataframe

data(mtcars)
mtcars$carb<- as.ordered(mtcars$carb)
model<-brm(carb~hp, data=mtcars, family=sratio)

#creating conditional effects object
c_eff <- conditional_effects(model, categorical = T)

#converting the effect of interest into a dataframe
df <- as.data.frame(c_eff$`hp`)

#creating plot
ggplot(df,aes(x=hp,y=estimate__, group=cats__))+
  geom_ribbon(aes(ymin=lower__, ymax=upper__, fill = cats__), alpha=0.2)+
  geom_line(size=1, position=position_dodge(0.05), aes(color=cats__, linetype=cats__))+
  scale_linetype_manual(name = "carb",
                        values = c("1"= "solid", 
                                   "2"= "dashed", 
                                   "3"= "dotted",
                                   "4" = "twodash",
                                   "5" = "dotdash",
                                   "6" = "longdash")) +
  scale_fill_manual(name = "carb",
                    values = c("1"= "green", 
                               "2"= "#7570b3", 
                               "3"= "#1b9e77",
                               "4" = "grey20",
                               "5" = "blue",
                               "6" = "grey80")) +
  scale_color_manual(name = "carb",
                     values = c("1"= "black", 
                                "2"= "black", 
                                "3"= "orange",
                                "4" = "grey20",
                                "5" = "blue",
                                "6" = "grey80"))+
  labs(y="Probability",x="hp")

resulting in the following graph:

5 Likes