library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
library(scales)

#faili sisselugemine ja andmete struktuur
J122=read.csv("PT1-T1.2-J1.2.2.csv",header=TRUE, encoding ="UTF-8")
names(J122)[2:6]=c("18-29","30-44","45-59","60-74","75+")
J122_cl=rbind(J122[4:5,],J122[11:12,])
J122_cl=J122_cl[,1:6]
J122_cl$Sugu=c("Mehed","Mehed","Naised","Naised")
J122_cl=pivot_longer(J122_cl,col=c("18-29","30-44","45-59","60-74","75+"),"Vanus")
J122_cl=pivot_wider(J122_cl,names_from=X,values_from=value)
names(J122_cl)[3:4]=c("upper","lower")
J122_cl$upper=as.numeric(J122_cl$upper)
J122_cl$lower=as.numeric(J122_cl$lower)

J122=J122[,1:6]
J122=rbind(J122[3,],J122[10,])
J122$sugu=c("Mehed","Naised")
J122=pivot_longer(J122,col=c("18-29","30-44","45-59","60-74","75+"),"Vanus")
J122$sugu=as.factor(J122$sugu)
J122$Vanus=as.factor(J122$Vanus)
J122$value=as.numeric(J122$value)


#joonis
ggplot(J122)+
  geom_col(aes(x=Vanus,y=value,fill=sugu),position = position_dodge(0.9),width=0.7)+
  theme_minimal()+
  geom_errorbar(data=J122_cl,aes(x=Vanus,ymin=lower,ymax=upper,col=Sugu),pos=position_dodge(0.9),width=0.3,show.legend = FALSE)+
  ylab("Palju või väga palju stressi tundnud indiviidide osakaal (%)")+
  scale_fill_manual(values=c("#1E272E","#FF3600"))+
  scale_color_manual(values=c("#FF3600","#1E272E"))+
  theme(legend.title=element_blank())+
  theme(text = element_text(color="#668080"),axis.text=element_text(color="#668080"))