rmeanconfidence-intervalstandard-deviationerrorbar

Is there another way to represent the confidence interval as an error bar?


I am currently working on a code that involves representing the mean standard deviation of the total weight of species collected over forest stands containin shelterbelts (Yes, No) further I want the error bars to represent the confidence interval of the mean. But I get the error bars, however I want to add the jitter plots as well as the geompoint (as seen in the picture) How can I do this without producing an error?

Code:

# Calculate the mean and standard deviation for each level of the variable
mean_sd_yes <- data %>%
  filter(`Type of plot with shelterbelt` == "Yes") %>%
  group_by("Yes") %>%
  summarise(mean_weight = mean(Weight, na.rm = TRUE),
            sd_weight = sd(Weight, na.rm = TRUE))

#confidence interval
mean_sd_yes$ci_lower <- mean_sd_yes$mean_weight - qt(0.975, 12 - 1) * mean_sd_yes$sd_weight / sqrt(12)
mean_sd_yes$ci_upper <- mean_sd_yes$mean_weight + qt(0.975, 12 - 1) * mean_sd_yes$sd_weight / sqrt(12)

mean_sd_no <- data %>%
  filter(`Type of plot with shelterbelt` == "No") %>%
  group_by("No") %>%
  summarise(mean_weight = mean(Weight, na.rm = TRUE),
            sd_weight = sd(Weight, na.rm = TRUE))

#confidence interval
mean_sd_no$ci_lower <- mean_sd_no$mean_weight - qt(0.975, 8 - 1) * mean_sd_no$sd_weight / sqrt(8)
mean_sd_no$ci_upper <- mean_sd_no$mean_weight + qt(0.975, 8 - 1) * mean_sd_no$sd_weight / sqrt(8)

# Create a separate dataframe for the points
points_df_yes <- data %>%
  filter(`Type of plot with shelterbelt` == "Yes") %>%
  group_by("Yes", sd) %>%
  summarise ("Yes", sd)

points_df_no <- data %>%
  filter(`Type of plot with shelterbelt` == "No") %>%
  group_by("No", sd) %>%
  summarise ("No", sd)

# Plot the mean and error bars
ggplot(mean_sd, aes(x = `Type of plot with shelterbelt`, y = sd_weight)) +
  geom_errorbar(data = mean_sd_yes, aes(x = "Yes",
                                        ymin = ci_lower,
                                        ymax = ci_upper),
                width = 0.2) +
  geom_errorbar(data = mean_sd_no, aes(x= "No",
                                       ymin = ci_lower,
                                       ymax = ci_upper),
                width = 0.2)

Desired output

this is what I want

Current output

this is what I get


Solution

  • Here is one approach:

    library(tidyverse)
    set.seed(0)
    
    tibble(
        x = rep(1:3, each = 100),
        y = runif(300, 0, 1)
    ) %>%
      ggplot(aes(x = x, y = y)) +
      geom_point(alpha = 0.25) +
      stat_summary(fun = mean, size = 1) + 
      stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width = 0.1)
    

    plot