library(ggplot2)
library(tidyr)
library(stringr)

#faili sisselugemine ja andmete formaadi korrigeerimine
J4110=read.csv("PT4-T4.1-J4.1.10.csv",header=TRUE, encoding ="UTF-8")[,1:5]
J4110x=J4110[c(1,4,7,10),]
names(J4110x)=gsub("\\.", " ", names(J4110x))
names(J4110x)[5]="(Väga) hea"
J4110x=separate(J4110x,X,into=c("Riik","Sugu"),sep=" ")
J4110x$Sugu=str_to_title(J4110x$Sugu)
J4110x=pivot_longer(J4110x,3:6)
J4110x$low=as.vector(t(J4110[c(2,5,8,11),2:5]))
J4110x$high=as.vector(t(J4110[c(3,6,9,12),2:5]))
J4110x$Riik=as.factor(J4110x$Riik)
J4110x$Riik=factor(J4110x$Riik,levels(J4110x$Riik)[order(c(2,1))])
J4110x$name=as.factor(J4110x$name)
J4110x$name=factor(J4110x$name,levels(J4110x$name)[order(c(4,2,1,3))])

#joonis
ggplot(J4110x)+
  geom_point(aes(x=name,y=value,col=Sugu),cex=3)+
  geom_errorbar(aes(x=name,y=value,ymin=low,ymax=high,col=Sugu),width=0.2,linewidth=0.8)+
  theme_minimal()+
 facet_grid(~Riik)+
  scale_y_continuous(breaks=seq(0,4,1),limits=c(0,4))+
  theme(legend.position = "bottom")+
  theme(legend.title = element_blank())+
  scale_color_manual(values=c("#6666cc","#F25D23"))+
  theme(text = element_text(color="#668080"),axis.text=element_text(color="#668080"))+
  theme(strip.text.x=element_text(color="#668080"),strip.text.y = element_text(color="#668080"))+
  xlab("Arvutioskuse tase")+
  ylab("Depressiivsus (EURO-D)")