Error: Aesthetics must be either length 1 or the same as the data (6): ymin, ymax

Hello Community,

I want to plot a 2 x 2 x 3 design using violin plots that include a geom_point for the mean and geom_errorbar (see code below). Here the problem starts, as soon as I add the geom_errorbar the following error massage appears: "Error: Aesthetics must be either length 1 or the same as the data (6): ymin, ymax"

It is pretty weird because the geom_point works totally fine.

Any help is much appreciated.

David

# Data simulation
set.seed(666) 
c.rel.f <- rep(c("Non-Relative", "Relative"), times = 40)
c.resp.f <- rep(c("Non-Responsible", "Responsible"), times = 40)
c.sit.f <- rep(c("Less-severe", "Moral Courage", "More-severe" ), length.out = 80)
hi.mean <- rnorm(n = 80, mean = c(5.62, 5.56, 6.02, 4.50, 5.92, 5.92,
                                  5.06, 5.88, 6.01, 4.72, 6.01, 6.10))

ds <- data.frame(c.sit.f, c.resp.f, c.rel.f, hi.mean)



## Compute CIs for DV  ##
se.hi <- Rmisc::summarySE(ds, 
                          measurevar = "hi.mean", 
                          groupvars = c("c.sit.f", "c.resp.f", "c.rel.f"), 
                          na.rm = TRUE)

# Create plot stepwise #
phi <- ggplot(data = ds,
              mapping = aes(x = c.resp.f, y = hi.mean, fill = c.rel.f))

phi + geom_violin(na.rm = TRUE,
                  position = position_dodge(0.9))  +
  geom_point(data = se.hi,  
             aes(x = c.resp.f, y = hi.mean), 
             color = "black", size = 3.5, shape = 18,
             position = position_dodge(0.9))  +
  #geom_errorbar(data = se.hi,
  #             stat = "identity",
  #             aes(x = c.resp.f, 
  #                 ymin = se.hi - ci, ymax = se.hi + ci), 
  #             color = "black", size = 1.1, width = 0.2,
  #             position = position_dodge(0.9)) +
  facet_grid( ~ c.sit.f) + 
  theme_minimal() +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank()) +
  labs (x = NULL, 
        y = "Helping intention\n") + # \n to increase space between y-axis and y-title
  scale_y_continuous(limits = c(0, 7), 
                     breaks = seq(0, 7, 1), 
                     expand = c(0, 0)) +
  theme(strip.text.x = element_text(size = 12, face = "bold",
                                    color = "#000000")) +
  theme(axis.line = element_line(size = 1, 
                                 linetype = "solid")) +
  theme(axis.text.x = element_text(size = 12, face = "bold", 
                                   color = "#000000")) +
  theme(axis.title.x = element_text(size = 12, face = "bold", 
                                    color="#000000")) +
  theme(axis.text.y = element_text(size = 12, face = "bold", 
                                   color = "#000000")) +
  theme(axis.title.y = element_text(size = 12, face = "bold", 
                                    color="#000000")) +
  scale_color_grey(aesthetics = "fill",
                   name = "Relation",
                   labels = c("Non-Relative", "Relative")) +
  theme(legend.title = element_text(size = 12 , face = "plain", 
                                    color = "#000000"), 
        legend.text = element_text(size = 12, face = "plain", 
                                   color = "#000000"))+
  guides(fill = guide_legend(override.aes = list(shape = NA)))

Should the se.hi in the ymin and ymax statements be hi.mean? That seems to work for me.

Thanks a lot - it works now.

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