How to Connect Error Bars with Lines for Different Conditions?

I have a graph with different colors representing separate conditions. I used stat_summary(geom="errorbar") to add error bars to each bar. Now, I want to connect the error bars for each condition with a line. For example, the three error bars of the red condition should be connected with a line, the three green bars should be connected with a line, and the three orange bars should also be connected with a line. I'm considering using geom_line, but I'm unsure about the steps to proceed. How can I achieve this?

Here the code

my <- ggplot(my_data[my_data$Stimulation == "Sham", ], aes(x = Manipulation, fill = interaction(Manipulation, Session), y = Accuracy * 100)) +
geom_bar(position = position_dodge(.8), stat = "summary", width = 0.7) +
scale_fill_manual(values = c("#A2142F", "#20B2AA", "#D95319", "#A2142F", "#20B2AA", "#D95319", "#A2142F", "#20B2AA", "#D95319")) +
stat_summary(geom = "errorbar", fun.data = mean_se, position = position_dodge(0.8), width = 0.3, color = "black", size = 1) +
labs(x = "Manipulation", y = "Total Accuracy %") +
geom_point(data = my_data[my_data$Stimulation == "Sham", ], aes(x = Manipulation, y = Accuracy * 100),
alpha = 1.5, position = position_dodge(.8), shape = 21, size = 2.5, color = "black") +
theme_minimal() +
theme(
panel.background = element_blank(),
panel.grid = element_blank(),
axis.line = element_line(color = "black", size = 1.5),
axis.ticks = element_line(color = "black"),
axis.text = element_text(color = "black", size = 20),
plot.title = element_text(size = 16, face = "bold", hjust = 0.5),
axis.title = element_text(size = 12, face = "bold"),
strip.text = element_blank()
) +
coord_cartesian(ylim = c(40, 100)) +
scale_y_continuous(breaks = seq(0, 100, by = 20)) +
geom_hline(yintercept = 0, linetype = "dashed", color = "black", size = 0.85)

Here the graph:

Thanks.

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.