I know there are several answers to this question, but it's slightly confusing to understand. I believe there are simpler codes than those explained in the current posts.
location=c("L1","L2","L1","L2")
y=c(5,10,12,12)
cat=c("A","A","B","B")
se=c(1,2,2,3)
df=data.frame(location,y,cat,se)
ggplot(data=df, aes(x=location , y=y, fill=cat))+
geom_bar(stat="identity", position = position_stack(reverse=T), width=0.7, size=1) +
geom_errorbar(aes(ymin=y-se, ymax=y+se),
position = "identity", width=0.5)
When I run the code provided above, the error bars appear dispersed.
I want to place one error bar at each bar, as shown below. Could you please share the code about this?
Many thanks,


You can calculate a per-
locationcumulative sum onyand place the errorbars there. (I'll show with dplyr, but this can be done fairly easily with base R if needed.)The changes to your code:
mutateto add incume_y, grouped bylocationgeom_errorbar, change fromy-setocume_y - se, similar forymaxsize=, changed tolinewidth=