0

I have these data:

db<- data.frame(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48),
Time = c(1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2),
Part  =c(Iv, Iv, Iv, Iv, Id, Id, Id, Id, Iv, Iv, Iv, Iv, Id, Id, Id, Id, Iv, Iv, Iv, Iv, Id, Id, Id, Id, Iv, Iv, Iv, Iv, Id, Id, Id, Id, Iv, Iv, Iv, Iv, Id, Id, Id, Id, Iv, Iv, Iv, Iv, Id, Id, Id, Id), 

Cicle = c(I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, II, II, II, II, II, II, II, II, II, II, II, II, II, II, II, II, III, III, III, III, III, III, III, III, III, III, III, III, III, III, III, III), 

val = c(24.5, 35.5, 31.5, 25, 25.5, 13.5, 18, 24.5, 27.5, 29, 19, 33, 22.5, 20, 28.5, 19.5, 21, 28.5, 22.5, 22.5, 28.5, 21, 26.5, 26.5, 20.5, 26.5, 22, 30, 28.5, 23, 27.5, 20, 17.5, 29.5, 27, 22, 32, 21, 22.5, 27.5, 26, 23.5, 18.5, 27.5, 23, 23, 31.5, 22.5))

I would like to obtain this percent stacked barchart with errorbars: enter image description here

I've tried in this way:

db$Cicle <- factor (db$Cicle, levels=c("I", "II", "III"))

db <- db %>% 
  group_by(Time, Part, Cicle) %>% 
  summarise(val = sum(val))

sd<-sd(db$val)

b<-ggplot(db, aes(x = Time, y = val, fill= Part)) + 
  geom_bar(position="fill", stat="identity", colour="black")+ facet_grid(. ~ Cicle)+
  scale_fill_manual(values = c("#DADAEB", "#9E9AC8"))
b + labs(x="Time", y="%", fill="") + theme_bw() + 
  theme(panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank(), axis.line = element_line(colour ="black")) + 
  theme(axis.text=element_text(size=14,face="bold"),axis.title=element_text(size=14,face="bold")) + 
  theme(legend.title = element_text(size=14),legend.text = element_text(size=12)) +  
  theme(strip.text = element_text(size = 14, face="bold"))  +
  theme(plot.title = element_text(size = 20, face = "bold", hjust = 0.5))




ggplot(db, aes(x = Time, y = val, fill= Part)) + 
  geom_bar(position="fill", stat="identity", colour="black")+ facet_grid(. ~ Cicle)+
  scale_fill_manual(values = c("#DADAEB", "#9E9AC8")) +
  geom_errorbar(aes(ymin=val-sd, ymax=val+sd), width=.2,
                position=position_dodge(.9)) 


But I've obtained this plot, just perfect but withour errorbars:

enter image description here

and then this plot with errorbars but wrong:

enter image description here

How can I fix it?

Thank you!

As suggested by @harre I've create a new variable:

db$val1<-(db$val)/100

then I've tried again the code but errorbars random placed..

ggplot(db, aes(x = Time, y = val1, fill= Part)) + 
  geom_bar(position="fill", stat="identity", colour="black")+ facet_grid(. ~ Cicle)+
  scale_fill_manual(values = c("#DADAEB", "#9E9AC8")) +
  geom_errorbar(aes(ymin=val1-sd, ymax=val1+sd), width=.2,position=position_dodge(.9)) 

Oh well.. Why this?

enter image description here

ArTu
  • 431
  • 4
  • 20
  • 3
    Your problem is that the first is plotted between 0-1 and the second 80-120. You'll probably want to do the calculations before plotting to put them on a similar scale. – harre Aug 01 '22 at 17:12
  • Thank you @harre I've tried as you suggested, but no results.. – ArTu Aug 01 '22 at 22:41

1 Answers1

0

I've solved using these suggestions: Stacked barplot with errorbars using ggplot2

Here we are: enter image description here

ArTu
  • 431
  • 4
  • 20