Manually changing x-axis for each panel in marginal effects plot of interactions

  • Operating System: Windows 10
  • brms Version: 2.9.0

This might be more of a ggplot question. Not sure. I am creating a marginal effects plot for a 3-way interaction. The following code (below at end of post) will produce an example of the plot. However, for the pre-intervention period (period=1pre; left panel) I would like to constrain the x-axis to 1-10 and for the post-intervention period (right panel), constraint the x-axis from 10-20, in the 2-panel plot that is produced. As it is, the x-axis goes from 1-20 in both panels. I have tried a variety of things, including deleting the data rows that I don’t want out of the marginal effects object and then re-calling plot(me) - this constrains the data, but the x-axis is still 1-20 on both plots.
Is there any way to manually constrain the x-axis for this 2-panel marginal effects plot?
Thanks!


Code:
library(ggplot2)
library(brms)

pre_treat1<-rnorm(10,30,1)
post_treat1<-c(rnorm(3,25,1),rnorm(3,20,1),rnorm(4,15,1))
pre_control<-rnorm(10,30,1)
post_control<-rnorm(10,30,1)
pre_treat2<-rnorm(10,30,1)
post_treat2<-c(rnorm(3,28,1),rnorm(3,26,1),rnorm(4,25,1))
outcome<-c(pre_treat1,post_treat1,pre_control,post_control,pre_treat2,post_treat2)
time_treat1<-1:20
time_control<-1:20
time_treat2<-1:20
time<-c(time_treat1,time_control,time_treat2)
pre_period_treat1<-rep(“1pre”,10)
post_period_treat1<-rep(“2post”,10)
pre_period_control<-rep(“1pre”,10)
post_period_control<-rep(“2post”,10)
pre_period_treat2<-rep(“1pre”,10)
post_period_treat2<-rep(“2post”,10)
period<-c(pre_period_treat1,post_period_treat1,pre_period_control,post_period_control,pre_period_treat2,post_period_treat2)
treatment_treat1<-rep(“3treat1”,20)
treatment_control<-rep(“1control”,20)
treatment_treat2<-rep(“2treat2”,20)
treatment<-c(treatment_treat1,treatment_control,treatment_treat2)
data<-cbind(period,treatment)
data<-data.frame(data)
data$outcome<-outcome
data$time<-time

model.3way<-brm(outcome ~ treatmentperiodtime, data=data)

conditions <- make_conditions(model.3way, “period”)
me<-marginal_effects(model.3way, “time:treatment”, conditions = conditions)
plot(me)

See ?marginal_effects

Thanks, Paul. I guess I am just missing something, because I looked through that documentation last week.
I also tried things like this: plot(me)[[1]] + ggplot2::xlim(0, 10)
But it only constrains the x-axis on all panels. I am not sure how to make the x-axis different for each panel.

I have tried a variety of things, including deleting the data rows that I don’t want out of the marginal effects object and then re-calling plot(me) - this constrains the data, but the x-axis is still 1-20 on both plots.

Take that plot then add this:

+ facet_wrap("cond__", scales = "free")

Thanks for the suggestion. I just get “NULL”.

I realize this is horribly inefficient code (but I was also having trouble removing the rows that I wanted), but this code below will remove the rows and plot with constrained data (but not x-axis) for the example that I give above. When I try your suggestion, I just get NULL and no plot.
me plot code.txt (1.6 KB)


plot1 + facet_wrap(“cond__”, scales = “free”)
The above just gives NULL

plot(me)[[1]] + …

2 Likes

Great! Thanks Ax3man and Paul! I appreciate it. I add on ggplot2::ylim() to keep the ylim the same on both, because without it, both the x and y axis vary on each plot.
plot(me)[[1]] + facet_wrap(“cond__”, scales = “free”) + ggplot2::ylim(0, 50)
does the trick.
I really appreciate it.

You can use scales = "free_x" instead to only vary the x.

1 Like

Even better. Thanks!

I’m having a similar problem. I want to modify the ylim of a conditional_effects plot.
However, the code

plot(me)[[1]] + ggplot2::ylim(0, 1000)

returns the error: ‘x’ is a list, but does not have components ‘x’ and ‘y’