1

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

  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. If you are asking for general data visualization advice, then that might be a better question for [stats.se] first. You should know what you want the final plot to look like. – MrFlick Jul 14 '23 at 14:09
  • Which picture Madelein? You don't seem to have included one. It would be much easier to help you if you included your data. Can you perhaps add the output of `dput(data)` in your post? – Allan Cameron Jul 14 '23 at 14:09

1 Answers1

0

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

Mark
  • 7,785
  • 2
  • 14
  • 34