library(tidyr)
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(scales)
#faili sisselugemine ja andmete formaadi korrigeerimine
J142=read.csv("PT1-T1.4-J1.4.2.csv",header=TRUE, encoding ="UTF-8")
J142=pivot_longer(J142,2:19)
J142$name=sub('X',"",J142$name)
J142$name=sub('\\.',"-",J142$name)
J142$name[J142$name=="-.85"]="85+"
J142=filter(J142,!name%in%c("0-4","5-9","10-14"))
errorbars=J142[91:150,]
J142=filter(J142,X%in%c("Mehed","Naised"))
J142$Upper=numeric(30)
J142$Lower=numeric(30)
J142$Upper[1:15]=errorbars$value[1:15]
J142$Lower[1:15]=errorbars$value[16:30]
J142$Upper[16:30]=errorbars$value[31:45]
J142$Lower[16:30]=errorbars$value[46:60]


ggplot(J142)+
  geom_col(aes(x=name,y=value,fill=X),pos=position_dodge(0.7),width=0.8)+
  geom_errorbar(aes(x=name,y=value,ymin=Lower, ymax=Upper,col=X),pos=position_dodge(0.7),width=0.3,show.legend = FALSE)+
  theme_minimal()+
  ylab("%")+
  xlab("Vanuserühm")+
  scale_fill_manual(values=c("#1E272E","#FF3600"))+
  scale_color_manual(values=c("#FF3600","#1E272E"))+
  theme(text = element_text(color="#668080"),axis.text=element_text(color="#668080"))+
  theme(legend.title=element_blank())