Reputation: 13
I am trying to add the mean ± standard error value in text on top of my bars, in my bar-chart. Like Excel does, sorta... I have the mean, but I can't manage to find a way to add the standard error value as well (and even less a "±" symbol).
Is there a way to do so?
Here's my data (nh1), the code, and the graph I have for now.
dput(nh1)
traitement N nombre_moules sd se ci
1 canard 12 33.7500000 53.0696369 15.3198846 33.7188386
2 canard_lam 9 37.2222222 48.8102904 16.2700968 37.5189105
3 lam 9 0.4444444 0.5270463 0.1756821 0.4051236
4 nouee 12 0.8333333 1.4034589 0.4051437 0.8917153
5 nouee_canard 9 119.2222222 37.1274083 12.3758028 28.5386523
6 nouee_canard_lam 12 83.3333333 58.3225098 16.8362584 37.0563548
7 nouee_lam 9 4.3333333 4.3011626 1.4337209 3.3061663
8 temoin 12 0.5833333 1.2401124 0.3579896 0.7879298
Thank you for your help!
nh1 <- summarySE(data = my_data_nombremoules, measurevar="nombre_moules", groupvars="traitement")
nh1
nh1$traitement <- factor(nh1$traitemeent)
ggplot(nh1, aes(x = traitement, y=nombre_moules
, fill=factor(traitement))) +
geom_bar(position=position_dodge(), stat="identity") +
geom_errorbar(aes(ymin=nombre_moules-se, ymax=nombre_moules+se),
width=.2,
position=position_dodge(.9))
nh2 <- ggplot(nh1, aes(x = traitement, y=nombre_moules,
fill=factor(traitement))) +
geom_bar(position=position_dodge(), stat="identity") +
geom_errorbar(aes(ymin=nombre_moules-se, ymax=nombre_moules+se),
width=.2, position=position_dodge(.9)) +
stat_summary(fun=mean, colour="black", geom="point", size=2,show.legend = FALSE) +
stat_summary(fun=mean, colour="black", geom="text",size=4, show.legend = FALSE,
vjust=-0.7, aes( label=round(nombre_moules, digits=1)))
(On a side note, I tried using geom_text instead of stat_summary for my mean labels, but I couldn't reduce the number of digits with digits=1, it gave me the "aesthetic unknown" error...)
Upvotes: 0
Views: 1404
Reputation: 173858
It's not clear why you're using stat_summary
here, since your data are already summarised. You can use geom_point
and geom_text
directly.
I have had to try to reclaim your data from your plot since you did not provide raw data in your question (see data used at the end of this answer)
I have to agree with DJJ in the comments: this looks messier with the SE labels attached. I have chosen geom_label
here instead of geom_text
to stop the numbers being obscured by the error bars.
library(ggplot2)
ggplot(nh1, aes(x = traitement, y=nombre_moules, fill = factor(traitement))) +
geom_col() +
geom_errorbar(aes(ymin = nombre_moules - se, ymax = nombre_moules + se), width =.2) +
geom_label(aes(label = paste(nombre_moules, "\ub1", se)), nudge_y = 5, size = 4,
label.size = 0, label.r = unit(0, "pt"), fill = "#ebebeb") +
geom_point(size = 3) +
theme(legend.position = "none", axis.text.x = element_blank()) +
labs(x = NULL, y = NULL)
Data used
nh1 <- structure(list(traitement = c("a", "b", "c", "d", "e", "f", "g", "h"),
nombre_moules = c(33.8, 37.2, 0.4, 0.8, 119.2, 83.3, 4.3, 0.6),
se = c(16, 16, 0.05, 0.05, 13, 17, 1, 0.1)),
class = "data.frame", row.names = c(NA, -8L))
Created on 2020-07-02 by the reprex package (v0.3.0)
Upvotes: 2