Here your issue to reorder bargraph is that you are calculating the mean and the standard deviation in ggplot2
. So, if you pass the "classic" reorder(x, -y)
, it will set the order based on the individual values of y not the mean.
So, you need to calculate Mean and SD before passing nbi as an argument in ggplot2
:
library(dplyr)
library(ggplot2)
DF %>% group_by(sig_lip) %>%
summarise(Mean = mean(nbi, na.rm = TRUE),
SD = sd(nbi, na.rm = TRUE)) %>%
ggplot(aes(x = reorder(sig_lip,-Mean), y = Mean, fill = sig_lip))+
geom_col()+
geom_errorbar(aes(ymin = Mean-SD, ymax = Mean+SD))
Does it answer your question ?
If not, please provide a reproducible example of your dataset by follwoign this guide: How to make a great R reproducible example