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. (see my previous question for images, same question)
If it clude the jitter and geopoint it produces no output. How can I do this differently?
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) +
geom_jitter(data = points_df_yes, aes(x = "Yes", y = sd), position = position_jitter(0.2), color = "darkgray") +
geom_jitter(data = points_df_no, aes(x = "No", y = sd), position = position_jitter(0.2), color = "darkgray") +
geom_pointrange(aes(ymin = ci_lower, ymax = ci_upper), data = mean_sd_yes ) +
geom_pointrange(aes(ymin = ci_lower, ymax = ci_upper), data = mean_sd_no ) +
labs(x = "Type of plot with shelterbelt", y = "Mean Weight")