R: Plot of mean values with error bars for grouped data

62 views Asked by At

I have the following problem. The data-set was collected in a within-subject design with repeated measures. My data-set contains 4 variables. The participant ID, the participant's sex, condition and one measured value. The data-set looks something like this:

df <- read.table(text = 'ID sex condition value
01 m A 0.0765
01 m B 0.063
01 m C 0.0773
02 f B 0.0599
02 f C 0.0679
02 f A 0.067
03 f C 0.0728
03 f A 0.0589
03 f B 0.0699', header=TRUE)

What I would like to do now is to have a graph which shows the mean value and standard deviation for every condition. But the graph should be grouped by sex. I tried to plot this with ezPlot from ez package:

library(ez)

ez_Condition <- ezPlot(df,
                 dv = value, 
                 wid = ID, 
                 within = condition, 
                 between=sex,
                 split=sex,
                 do_bars=TRUE,
                 type = 2, 
                 x = condition
)
ez_Condition

MY PROBLEM: The output of this code is the plot I was looking for where the average value is visualized for every condition for both sexes seperatly. But the error bars don't show the individual standard diviation per condition per sex. Instead the error bars are equally long for all conditions which doesn't fit the data.

library(ggplot2)

Bar_Mean <- ggplot(data=df, aes(x=condition, y=value, fill=sex)) + 
  geom_bar(position = "dodge", stat = "summary", fun = "mean") +
  labs(title = "Average Value") +
  labs (x="Condition",y="Value") +
  scale_y_reverse(breaks = seq(0,-5, by=-1)) +
  theme_classic() +
  stat_summary(fun.data = mean_cl_normal, geom = "errorbar", position = position_dodge(width=0.9),width=.1)
Bar_Mean

This code works, the visualized data also fit what I calculated before. But I would rather not use a bar plot of box plot. I would like to have a plot which looks like ezPlot where the mean values are connected with lines and I have 2 graphes for each sex.

If I try to modify my ggplot code and use ,,geom_line” instead of ,,geom_bar” it doesn't work any more. HOW CAN I GET A PLOT LIKE THIS USING GGPLOT?: enter image description here

Thank you in advance for your help!

2

There are 2 answers

0
Quinten On

You could calculate the mean and sd value per group_by. After that you could add the error bars using geom_errorbar and use the group aesthetic to keep them per group like this:

library(dplyr)
library(ggplot2)
df %>%
  group_by(sex, condition) %>%
  summarise(mean_value = mean(value),
            sd_value = sd(value)) %>%
  ggplot(aes(x = condition, y = mean_value, color = sex, group = sex)) +
  geom_point() +
  geom_line() +
  geom_errorbar(aes(ymin = mean_value-sd_value, ymax = mean_value+sd_value)) +
  labs(title = "Average Value") +
  labs (x="Condition",y="Value") +
  theme_classic() 

Created on 2024-01-29 with reprex v2.0.2

0
John On

This code works very well. I added a background, so the result is more clearly laid out:

library(ggplot2)
library(dplyr)

    p1 <- df %>%
      group_by(Hand, AOV) %>%
      summarise(mean_value = mean(Error_Sum),
      sd_value = sd(Error_Sum)) %>%
      ggplot(aes(x = AOV, y = mean_value, color = Hand, group = Hand)) +
      geom_point() +
      geom_line() +
      geom_errorbar(aes(ymin = mean_value-sd_value, ymax = mean_value+sd_value)) +
      labs(title = "Average Amount of Errors") + #Beschriftung der Achsen
      labs(subtitle="faithful eruptions") + #Beschriftung der Abbildung
      labs (x="AOV",y="Errors") +
      theme_classic()
    p1 + theme_gray()