Alex M
Alex M

Reputation: 129

R plot_model: how to shift whiskers to middle points of bars

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?..

Upvotes: 0

Views: 966

Answers (1)

Adam Smiley
Adam Smiley

Reputation: 46

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!

Upvotes: 2

Related Questions