library(stringr)
library(tidyr)
library(ggplot2)
#faili sisselugemine ja andmete formaadi korrigeerimine
J131=read.csv("PT1-T1.3-J1.3.1.csv",header=TRUE, encoding ="UTF-8")
J131=pivot_longer(J131,col=c("PISA.b","PISA.lo","PISA.hi", "Põhikool.b","Põhikool.lo","Põhikool.hi","RE.Lai.b","RE.Lai.lo","RE.Lai.hi","RE.Kitsas.b","RE.Kitsas.lo","RE.Kitsas.hi"))
J131$V22rtus = rep(c("b","lo","hi"),32)
J131$name = sub("\\.[^\\.]*$","",J131$name)
J131=pivot_wider(J131,names_from=V22rtus,values_from = value)

#eksamite nimed ilusamaks
J131$name[J131$name=="RE.Lai"]="RE:Lai"
J131$name[J131$name=="RE.Kitsas"]="RE:Kitsas"

#väärtuste järjekorrad selliseks nagu algses joonises
J131$X.=as.factor(J131$X.)
J131$X.=factor(J131$X., levels = levels(J131$X.)[order(c(4,8,5,3,2,6,7,1))])
J131$name=as.factor(J131$name)
J131$name=factor(J131$name, levels = levels(J131$name)[order(c(4,3,1,2))])

#joonis
ggplot(J131)+
  coord_flip()+
  geom_point(aes(x=X.,y=b,col=name),position = position_dodge(width = 0.6),cex=2.5)+
  scale_shape_manual(values=c(6,2,5,16))+
  geom_errorbar(aes(x=X.,ymin=lo,ymax=hi,col=name),position = position_dodge(width = 0.6),linewidth=0.7,width=0.3)+
  geom_abline(aes(intercept=0,slope=0),col="#668080")+
  scale_color_manual(values=rev(c("#1E272E","#f09d00","#FF3600","#668080")))+
  theme_minimal()+
  theme(text = element_text(color="#668080"),axis.text=element_text(color="#668080"))+
  scale_y_continuous(limits=c(-0.5,0.5),breaks=seq(-0.5,0.5,by=0.1))+
  guides(col = guide_legend(reverse=TRUE))+
  theme(legend.title=element_blank(),axis.title=element_blank())