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?:

Thank you in advance for your help!
You could calculate the mean and sd value per
group_by. After that you could add the error bars usinggeom_errorbarand use thegroupaesthetic to keep them per group like this:Created on 2024-01-29 with reprex v2.0.2