Something like that??? (if i understood your question!!!)
summary_data <- data %>%
group_by(x1) %>%
summarize(
mean_y = mean(y),
sd_y = sd(y)
)
ggplot(summary_data, aes(x = x1, y = mean_y)) +
geom_bar(stat = "identity", fill = "#336699") +
geom_label(aes(label = round(mean_y, digits = 2)), position = position_nudge(x=-0.2, y = -0.5)) +
geom_errorbar(aes(ymin = mean_y - sd_y, ymax = mean_y + sd_y),
width = 0.2,
size = 0.8,
color = "darkred") +
xlab("x1") +
ylab("y") +
theme_classic(base_size = 12) +
coord_cartesian(ylim = c(0, max(summary_data$mean_y + summary_data$sd_y) * 1.1))