position=position_dodge2(0.9) not working in R

41 Views Asked by At

So, I'm trying to make a bar plot in R but unfortunately it has decided to be obstinate. Most of it works fine, but the error bars overlap and noting I have tried can separate them.

enter image description here

DataR2 <- read_excel("file.xlsx")

ggplot(data=DataR2, aes(x=Samples, y=`Protein (%) iso + lit factors`)) + 
  geom_bar(stat = "identity", 
           aes(fill=Matrix, colour = Matrix, alpha=`Fermented/Raw`),
           position = position_dodge2()) +
  theme_classic() +
  scale_alpha_discrete(range = c(0.4, 1)) + 
  theme(text = element_text(size=10), axis.text.x = element_text(angle=90, hjust=1, vjust = 0.5)) + 
  geom_errorbar(aes(x=Samples, ymin = `Protein (%) iso + lit factors` - `SDProtein ufærdig`, ymax = `Protein (%) iso + lit factors` + `SDProtein ufærdig`),
                width=0.3, position=position_dodge2(0.9)) + 
  scale_y_continuous("Protein (%) iso + lit", limits=c(0, 3.75), n.breaks = 15)+
  scale_fill_manual(values=c('#3335ef','#36ffff', '#fe3637','#e7a4e3', '#2be436'))+
  scale_color_manual(values=c('#6d6d6d','#6d6d6d', '#6d6d6d','#6d6d6d', '#6d6d6d')) 

So I have tried googeling - there seem to be several people with the same problem. However, none of their solutions worked? I have tried many, many variations of position=position_dodge2(0.9) and postion="dodge".

Data:

> dput(head(DataR2))
structure(list(Samples = c("ID001.F.H", "ID001.F.H", "ID002.F.H", 
"ID002.F.H", "ID003.F.H", "ID003.F.H"), Matrix = c("Oat", "Oat", 
"Soy", "Soy", "Almond", "Almond"), `Fermented/Raw` = c("Raw", 
"Fermented", "Raw", "Fermented", "Raw", "Fermented"), `Protein (%) factor 6.25` = c(7.45993910066117, 
2.24298049691794, 6.62321282117985, 9.91212116591297, 8.29495444554842, 
3.71983398765437), `SD - Protein (%) factor 6.25` = c(0.111538007745426, 
0.395856611653545, 0.532126160389082, 0.559732380628379, 0.599892190493479, 
0.492060895988664), `Protein (%) iso + lit factors` = c(7.43890051657589, 
9.21238358560627, 6.39803385725862, 8.51093445087067, 8.13309172459924, 
0.729412074068291), `SD - Protein (%) iso + lit factors` = c(0.363617023306606, 
0.712641511743164, 0.725095089273275, 0.623535634252706, 0.527460445764065, 
0.719172054941388), `Fat (%)` = c(5.12183323797388, 4.12655954548906, 
6.65049809505611, 3.41355896527647, 4.23334771018789, 6.52865527096824
), `SD - fat (%)` = c(0.0718937166076469, 0.363279900603612, 
0.901895348474346, 0.858296718356204, 0.723308125415264, 0.841471103222819
), `SDProtein ufærdig` = c(0.512214279917204, 0.375925901573847, 
0.895847643787482, 0.44039693554103, 0.53530129094088, 0.406213985430093
), `SDFat ufærdig` = c(0.962435657371665, 0.250163216081875, 
0.842970512920795, 0.746696473456681, 0.425704493865501, 0.811682134778189
)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))
0

There are 0 best solutions below