Creating a line graph with SE bars using ggplot and repeated measures

Hello! I am trying to create a line graph with SE using data that has within subject variables. I converted my data into long format using the rstatix package:

.

Then I summarized the data using the summarySEwithin function in the Rmsic package:
PrefSE <- summarySEwithin(PrefAll, measurevar = "Intake", withinvars = "Day", idvar = "...1", na.rm = TRUE, conf.interval = 0.95)
which gave me:

Then I want to graph it in a line graph across days, while separating it into treatments (PCB) -- so I want to have one line for treatment A and one line for treatment B, across Days 2 - 11. However my code:
ggplot(PrefSE, aes(x = Day, y = Intake, group = PCB_Treatment, color = PCB_Treatment)) +
geom_line() +
geom_errorbar(width = 0.1, aes(ymin = Intake-se, ymax = Intake+se)) +
geom_point(shape=20, size=2, fill=c("black")) +
ylim(25,100)
gave me:
Error: Aesthetics must be either length 1 or the same as the data (10): group, colour

I saw a similar method of graphing and implemented it:
ggplot(PrefSE, aes(x = Day, y = Intake, group = 2)) +
geom_line() +
geom_errorbar(width = 0.1, aes(ymin = Intake-se, ymax = Intake+se)) +
geom_point(shape=20, size=2, fill=c("black")) +
ylim(25,100)

although it did not give me separate groups:

If anyone can help me create separate groups without getting the error messages please help!!

Thank you in advance :slight_smile:

The data frame PrefSE has no information about PCB_Treatment. I cannot tell whether that column is a within-subject variable or not, so I cannot say how to handle this, and in any case I have never used summarySEwithin(). You need to modify your call to summarySEwithin so that PCB_Treatment is taken into account.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.