library(ggplot2)
library(tidyr)
#faili sisselugemine ja andmete formaadi korrigeerimine
J236=read.csv("PT2-T2.3-J2.3.6.csv",header=FALSE, encoding ="UTF-8")
J236x=J236[1:7,1:5]
J236y=J236[10:16,1:5]
J236z=J236[19:25,1:5]
J236y[,1]=J236x[,1]
J236z[,1]=J236x[,1]
names(J236x)=c("X","Suicidal ideation","Fatigue","Depressiveness", "Stress")
names(J236y)=c("X","Suicidal ideation","Fatigue","Depressiveness", "Stress")
names(J236z)=c("X","Suicidal ideation","Fatigue","Depressiveness", "Stress")
J236x=pivot_longer(J236x,2:5)
names(J236x)=c("X","Faktor","value")
J236y=pivot_longer(J236y,2:5)
names(J236y)=c("X","Faktor","high")
J236z=pivot_longer(J236z,2:5)
names(J236z)=c("X","Faktor","low")
J236=cbind(J236x,J236y$high,J236z$low)
names(J236)[4:5]=c("high","low")
J236$value=as.numeric(J236$value)
J236$low=as.numeric(J236$low)
J236$high=as.numeric(J236$high)
J236$X=as.factor(J236$X)
J236$X=factor(J236$X,levels(J236$X)[order(c(6,5,4,3,7,1,2))])
J236$Faktor=as.factor(J236$Faktor)
J236$Faktor=factor(J236$Faktor,levels(J236$Faktor)[order(c(3,1,4,2))])
#joonis
ggplot(J236)+
geom_point(aes(x=X,y=value,col=Faktor),cex=3,pos=position_dodge(0.8))+
# geom_label(aes(x=X,label=value,col=Faktor,y=value-low-0.3),size=3,pos=position_dodge(0.8))+
geom_errorbar(aes(x=X,ymin=value-low,ymax=value+high,col=Faktor),width=0.4,linewidth=0.7,pos=position_dodge(0.8))+
geom_abline(slope=0,intercept=1,linewidth=0.3)+
theme_minimal()+
coord_flip()+
theme(strip.text.y = element_text(angle = 360),legend.position = "bottom",legend.title=element_blank())+
ylab("")+
xlab("")+
scale_y_continuous(limits=c(0,4.5),breaks=seq(0,4.5,0.5))+
scale_color_manual(values=c("#6666cc","#668080","#f09d00","#FF3600"),breaks=rev(levels(J236$Faktor)))+
theme(strip.text.x=element_text(color="#668080"))+
theme(text = element_text(color="#668080"),axis.text=element_text(color="#668080"))+
guides(color = guide_legend(nrow=2,byrow = TRUE))
