0

I plot an interaction in my linear model by using plot_model (https://www.rdocumentation.org/packages/sjPlot/versions/2.8.7/topics/plot_model):

plot_model(LME_model, type = "pred", terms = c("match_sp", 
                                               "response"),
           colors = c("blue", "magenta3"),
           alpha = 0.15)

And get these sticks, which look not so nicely: enter image description here

However, I would like to have a bar plot, so I add some lines to the code:

plot_model(LME_model, type = "pred", terms = c("match_sp", 
                                               "response"),
           colors = c("blue", "magenta3"),
           alpha = 0.15) + 
  geom_bar (position=position_dodge(), stat = "identity", width=0.5, alpha = 0.3) + 
  coord_cartesian(ylim=c(550,700))

Now it looks much better:

enter image description here

But now these whiskers produced before are located not at central points of those bars, as it should be the case. Does anyone know how to fix this problem?..

Alex M
  • 129
  • 1
  • 7
  • [See here](https://stackoverflow.com/q/5963269/5325862) on making a reproducible example that is easier for folks to help with. That includes a sample of data, and what packages you're using (where does `plot_model` come from?) – camille Feb 16 '21 at 20:26
  • @camille I added info about the package. Reproducible example: my code is using values from a linear mixed effect model, which in turn is based on a big dataset... So I really have no idea how I can provide a reproducible example in this situation. I hope someone is just familiar with the formula I am asking about and can give me an advice based on what I shared... – Alex M Feb 16 '21 at 21:27

1 Answers1

1

I had the same problem, and after playing around for awhile I found that you can just add a dodge element within plot_model() in order to center the whiskers on the bars. In order for it to be centered, it just needs to be the same value as the width that you specify in geom_bar(). So in your case, it should be "dodge = 0.5". Here is what the final code would look like.

    plot_model(LME_model, type = "pred", terms = c("match_sp", "response"),
           colors = c("blue", "magenta3"),
           alpha = 0.15,
           dodge = 0.5) + 
      geom_bar (position=position_dodge(), stat = "identity", width=0.5, alpha=0.3)+ 
      coord_cartesian(ylim=c(550,700))

I realize that you asked this question a long time ago so it is probably too late to be helpful for you, but hopefully it helps someone out!