library(tidyr)
library(ggplot2)

#faili sisselugemine ja andmete formaadi korrigeerimine
J143=read.csv2("PT1-T1.4-J1.4.3.csv",header=TRUE, encoding ="UTF-8")
J143x=J143[4:6,]
J143=J143[1:3,]
J143=pivot_longer(J143,2:5)
J143x=pivot_longer(J143x,2:5)
J143$value=as.numeric(J143$value)
J143$dif=as.numeric(J143x$value)

J143$name[J143$name=="Ärevushäired..F40.F41."]="Ärevushäired (F40-F41)"
J143$name[J143$name=="Depressioon..F32.F33."]="Depressioon (F32-F33)"
J143$name[J143$name=="Alkoholisõltuvus..F10."]="Alkoholisõltuvus (F10)"
J143$name[J143$name=="Unehäired..F51."]="Unehäired (F51)"

J143$X=as.factor(J143$X)
J143$X=factor(J143$X,levels=c("Kokku","Naised","Mehed"))

J143$name=as.factor(J143$name)
J143$name=factor(J143$name,levels=levels(J143$name)[c(4,1,3,2)])

# joonis
ggplot(J143)+
  geom_col(aes(x=name,y=value,fill=X),pos=position_dodge(0.6),width=0.5)+
  geom_errorbar(aes(x=name,ymin=value-dif,ymax=value+dif,col=X),pos=position_dodge(0.6),width=0.2,linewidth=0.8,show.legend = FALSE)+
  theme_minimal()+
  theme(legend.title=element_blank())+
  coord_flip()+
  ylab("%")+
  xlab("")+
  scale_fill_manual(values=c("#1E272E","#FF3600","#668080"),breaks=c("Mehed","Naised","Kokku"))+
  scale_color_manual(values=c("#1E272E","#1E272E","#FF3600"))+
  theme(text = element_text(color="#668080"),axis.text=element_text(color="#668080"))