Creating a Barplot Showing the Means with 95% CI Errorbars

My barplot is showing a lot of errorbars. I think the problem is with the 'ci' or confidence interval.

Can you also post your data or a subset of your data. A convenient way to do that is to post the output of the dput() function.

dput(data)

If the output of that is too large, you can choose a subset with

dput(head(data, 20))

Please put a line with three backticks just before and after the output.
```
Your output here
```

Are you after something like the following?

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.5
library(dplyr)
DATA <- structure(list(Reading1 = c(86.9, 87.9, 85.2, 83.8, 100, 89.4, 
                            74.5, 84.4, 85.7, 95, 89.1, 87.5, 91.9, 82.9, 76.3, 87.9, 92.3, 
                            94.2, 76.4, 85.3), 
                       Reading2 = c(87.81, 89.06, 85.23, 84.55, 102, 
                            90.66, 75.14, 84.58, 88.21, 95.87, 89.32, 90.05, 92.74, 83.05, 
                            76.91, 89.61, 93.41, 94.49, 78.53, 87.06), 
                       Reading3 = c(89.29,89.08, 85.25, 84.5, 102, 92.99, 74.42, 84.15, 
                                    87.59, 96.58, 88.48, 90.52, 94.71, 84.5, 77.22, 89.28, 
                                    94.29, 94.85, 80.46, 88.62), 
               Reading4 = c(92.52, 89.63, 87.46, 84.7, 102, 93.49, 75.56, 
                            85.78, 88.43, 97.32, 88.81, 90.55, 94.86, 87.25, 78.36, 89.58, 
                            94.7, 95.28, 81.34, 89.06), 
               ReadingPost1 = c(91.81, 92.3,88.41, 85.68, 102, 93.35, 76.41, 86.79, 88.82, 97.64, 90.14, 
                                92.31, 95.69, 90.04, 80.59, 92.07, 96.25, 95.54, 82.69, 89.34), 
               Sex = c("Male", "Female", "Female", "Male", "Male", "Female", 
                                       "Female", "Male", "Female", "Male", "Male", "Female", "Male", 
                                       "Female", "Male", "Female", "Female", "Male", "Female", "Female"), 
               FavoriteColor = c("Blue", "Blue", "Blue", "Blue", "Blue", 
                                                 "Blue", "Blue", "Blue", "Blue", "Green", "Green", "Green", 
                                                 "Blue", "Blue", "Blue", "Blue", "Red", "Green", "Green", 
                                                 "Green"), 
               Group = c("Group B", "Group A", "Group A", "Group A", "Group A", "Group A", "Group A", 
                         "Group A", "Group A", "Group B","Group A", "Group A", "Group B", "Group A", "Group A", "Group A", 
                          "Group B", "Group B", "Group A", "Group A"), 
               MathScores = c(81.75, 80.68, 82.13, 82.69, 70.9, 77.28, 92.68, 85.65, 85.56, 73.38,
                              80.43, 86.49, 74.49, 85.97, 94.75, 82.75, 73.89, 77.11, 91.95,82.87), 
               ScienceScores = c(80.36, 82.89, 83.86, 83.54, 71.98, 79.88, 99.09, 84.12, 83.99, 74.59, 77.12, 87.78, 
                                 75.18, 81.51, 90.88, 81.89, 74.67, 72.59, 92.37, 82.29), 
               TypicalLikert1 = c(3L,3L, 5L, 2L, 5L, 3L, 5L, 5L, 1L, 4L, 2L, 3L, 2L, 2L, 4L, 4L,5L, 2L, 5L, 3L), 
               TypicalLikert2 = c(4L, 2L, 5L, 2L, 5L, 3L, 5L, 5L, 1L, 4L, 3L, 4L, 3L, 1L, 4L, 4L, 5L, 2L, 5L, 3L), 
               TypicalLikert3 = c(4L, 2L, 5L, 1L, 5L, 2L, 5L, 5L, 1L, 4L, 
                                  3L, 4L, 3L, 1L, 3L, 4L, 5L, 1L, 5L, 3L), 
               Reverse = c(3L,3L, 1L, 4L, 1L, 3L, 1L, 1L, 5L, 2L, 4L, 3L, 4L, 4L, 2L, 2L,1L, 4L, 1L, 3L), 
               Marital = c(2L, 3L, 1L, 3L, 2L, 1L, 3L,1L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 1L, 2L, 3L, 1L, 1L), 
               Grade = c(1L, 1L, 2L, 4L, 1L, 2L, 1L, 2L, 3L, 3L, 1L, 2L, 4L, 4L, 3L, 3L, 2L, 2L, 3L, 4L), 
               RecodeLikert = c("Disagree", "Disagree", "Strongly Disagree", "Neutral", "Strongly Disagree", "Strongly Disagree",
                                "Neutral", "Neutral", "Agree", "Strongly Disagree", "Strongly Disagree","Agree", "Strongly Disagree", 
                                "Neutral", "Strongly Agree","Neutral", "Strongly Disagree", "Strongly Disagree", "Strongly Agree", "Disagree"), 
               Number = c("One", "Four", "Two", "Two", "One", "Three", "Two", "Four", "One", "Three", "Four", "One", "Three", 
                          "Two", "One", "One", "Two", "Three", "Three", "Four"), 
               DxPre = c("Negative", "Positive", "Positive", "Positive", "Positive", "Positive", "Positive", "Negative", "Positive", "Positive", "Positive", 
                         "Positive", "Positive", "Positive", "Negative", "Negative","Positive", "Positive", "Positive", "Positive"), 
               DxPost = c("Negative", "Positive", "Negative", "Negative", "Negative", "Negative", "Negative", "Negative", "Positive", "Positive", "Negative", 
                          "Positive", "Negative", "Negative", "Positive", "Negative","Negative", "Negative", "Positive", "Negative"), 
               GradePost = c(4L, 4L, 4L, 4L, 3L, 3L, 4L, 4L, 4L, 4L, 3L, 4L, 3L, 3L, 3L, 2L, 2L, 4L, 4L, 4L), 
               GPA = c(2L, 2L, 2L, 2L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), 
               Anxiety = c(0.2951,-0.619, 0.9683, 0.9575, 2.4603, 1.0207, 0.7518, 0.9192, -0.4883, 2.6647, 3.9209, 3.4309, 0.8182, 0.1582, 
                           1.5846, 1.1924, 5.1718, 5.4384, 1.3512, 2.4481), 
               SelfDoubt = c(300L, 300L, 300L,300L, 300L, 300L, 300L, 300L, 390L, 399L, 400L, 500L, 600L, 300L, 305L, 401L, 800L, 800L, 800L, 800L), 
               ACT = c(80L, 80L, 80L, 70L, 70L, 90L, 70L, 50L, 70L, 70L, 60L, 50L, 70L, 70L, 70L, 60L, 80L, 80L, 60L, 90L)), 
               row.names = c(NA, 20L), class = "data.frame")

DF <- DATA %>% group_by(Sex) %>% 
  summarize(Avg = mean(Anxiety),STD = sd(Anxiety), N = n()) %>% 
  mutate(ci = qnorm(0.975)*STD/sqrt(N))
#> `summarise()` ungrouping output (override with `.groups` argument)
DF
#> # A tibble: 2 x 5
#>   Sex      Avg   STD     N    ci
#>   <chr>  <dbl> <dbl> <int> <dbl>
#> 1 Female  1.40  1.72    11  1.01
#> 2 Male    2.12  1.69     9  1.10

ggplot(DF, aes(x=Sex, y=Avg))+
  geom_bar(stat="identity",
           fill="darkgreen")+
  geom_errorbar(aes(ymin=Avg-ci, ymax=Avg+ci),
                width=0.2)+
  labs(x = "Sex", y = "Anxiety") +
  theme_classic()

Created on 2021-08-24 by the reprex package (v0.3.0)

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.