Error bars look weird in ggplot2 (continuos Y x categorical X) - R

Viewed 43

I'm ploting a continuos Y variable against a 2-level categorical variable with ggplot2, grouping by another 2-level categorical variable, but the error bars look weird (they're all connected to each other, I don't know why...) edit: I've added a reproducible dataset below :)

  • Plot: (there's no division between the error bars...)

my plot

  • code:
data %>% 
  ggplot(., aes(x = CATEGORIES, y = CONTINUOSY)) +
  stat_summary(aes(group = MY_GROUP), position = position_dodge(width = 0)) +
  stat_summary(fun = mean, shape = 4) +
  stat_summary(aes(group = MYGROUP, linetype = MY_GROUP, color = MY_GROUP),
               geom = "line", size = 1, position = position_dodge(width = 0)) +
  theme_bw() +
  labs(x = "Categories A and B",
       y = "Continuos Y",
       title = "This is my plot") +
  theme_bw() +
  theme(legend.position = "right",
        legend.background = element_rect(color = "black"),  
        axis.text.x = element_text(angle = 0, hjust = 0.5, face = "bold"), 
        plot.title = element_text(hjust = 0.5, face = "bold"),
        axis.text.y = element_text(face = "bold"))

I also get this error: as here, but it didn't help me...

No summary function supplied, defaulting to `mean_se()`
No summary function supplied, defaulting to `mean_se()`
Warning message:
Removed 2 rows containing missing values (geom_segment). 
  • How can I solve this and correct the error bars? Any ideas would be much appreciated! Thanks in advance :)

  • Edit:

reproducible dataset here on Git

  • trying dput for the first time:
> dput(data)
structure(list(PARTICIPANTS = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 
7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 
10L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 
14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 17L, 
17L, 17L, 17L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 20L, 20L, 
20L, 20L, 21L, 21L, 21L, 21L), CONT_Y = c(19.44, 20.07, 19.21, 
16.35, 11.37, 12.82, 19.42, 18.94, 19.59, 20.01, 19.7, 17.92, 
18.78, 19.21, 19.27, 18.46, 19.52, 20.02, 16.19, 19.97, 13.83, 
15.93, 14.79, 21.55, 18.8, 19.42, 19.27, 19.37, 17.14, 14.45, 
17.63, 20.01, 20.28, 17.93, 19.36, 20.15, 16.06, 17.04, 19.16, 
20.1, 16.44, 18.39, 18.01, 19.05, 18.04, 19.69, 19.61, 16.88, 
19.02, 20.42, 18.27, 18.43, 18.08, 17.1, 19.98, 19.43, 19.71, 
19.93, 20.11, 18.41, 20.31, 20.1, 20.38, 20.29, 13.6, 18.92, 
19.05, 19.13, 17.75, 19.15, 20.19, 18.3, 19.43, 19.8, 19.83, 
19.53, 16.14, 21.14, 17.37, 18.73, 16.51, 17.51, 17.06, 19.42
), CATEGORIES = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("A", 
"B"), class = "factor"), MY_GROUP = structure(c(1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L
), .Label = c("G1", "G2"), class = "factor")), row.names = c(NA, 
-84L), class = c("tbl_df", "tbl", "data.frame"))
1 Answers

When you color the error bars, you could see that they just overlap. You could change the transparency if you want. Because you don't use fun in two of your stat_summary calls, it takes the default mean_se. Here is a reproducible example:

library(ggplot2)
library(dplyr)
data %>% 
  ggplot(aes(x = CATEGORIES, y = CONT_Y)) +
  stat_summary(aes(group = MY_GROUP, color = MY_GROUP), 
               position = position_dodge(width = 0)) +
  stat_summary(fun = mean, shape = 4) +
  stat_summary(aes(group = MY_GROUP, linetype = MY_GROUP, color = MY_GROUP),
               fun = mean, geom = "line", size = 1, position = position_dodge(width = 0)) +
  theme_bw() +
  labs(x = "Categories A and B",
       y = "Continuos Y",
       title = "This is my plot") +
  theme_bw() +
  theme(legend.position = "right",
        legend.background = element_rect(color = "black"),  
        axis.text.x = element_text(angle = 0, hjust = 0.5, face = "bold"), 
        plot.title = element_text(hjust = 0.5, face = "bold"),
        axis.text.y = element_text(face = "bold"))
#> No summary function supplied, defaulting to `mean_se()`
#> Warning: Removed 2 rows containing missing values (geom_segment).

Created on 2022-09-24 with reprex v2.0.2

Related