Problem with error bars in ggplot2

Dear users,
I have a problem with error bars in ggplot2.

I want to plot an interaction between continuous [iv] and nominal [condition] variable.
I use such a code

iv = runif(n = 100, min = 1, max = 7)
condition <- rep(letters[1:2], length.out = 100)
y = runif(n = 100, min = 1, max = 100)

df <- data.frame(iv, condition, y)

lm20 <- lm(y ~ condition * iv, data = df)
summary(lm20) 

df$condition <- as.factor(df$condition)

ggeffect(lm20, terms = c("condition", "iv")) %>%  
  plot(show.title = FALSE) + 
  stat_summary(fun.y = mean,
               geom = "point") +
  stat_summary(fun.y = mean,
               geom = "line") +
  stat_summary(fun.data = mean_cl_boot,
               geom = "errorbar") +
  scale_y_continuous("Voting intentions", limits = c(0, 100)) + 
  scale_colour_discrete(name = "Control", labels = c("Low", "Medium", "High")) +
  scale_x_discrete("Condition", labels = c("Low","High")

And this is what I get:

I encounter two problems

  1. Error bars and lines do not match
  2. I cannot change the labels on the X axis

I would be very grateful for your help - I've spent almost two days looking for an answer already.
Many thanks!

Is this what you are after?

library(ggeffects)
library(effects)
ggeffect(lm20, terms = c("condition", "iv")) %>%  
  plot(show.title = FALSE,
       connect.lines = TRUE) + 
  scale_colour_discrete(name = "Control", labels = c("Low", "Medium", "High")) +
  labs(x = "Condition", y = "Voting intentions")

From here: Introduction: Customize Plot Appearance • ggeffects

Do you want to change 'a' and 'b' as well when you refer to the x-axis labels?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.