library(ggplot2)
library(tidyr)
library(scales)

#faili sisselugemine ja andmete formaadi korrigeerimine
J223=read.csv("PT2-T2.2-J2.2.3.csv",header=TRUE, encoding ="UTF-8")
J223=J223[1:7]
names(J223)[c(3,4)]="Poisid"
names(J223)[c(6,7)]="Tüdrukud"
names(J223)=paste(names(J223),J223[1,])
J223=J223[2:7,]
J223=pivot_longer(J223,2:7)
J223=separate(J223,2,c("Sugu","Vanus")," ")
names(J223)[1]="Riik"
J223$value=as.numeric(J223$value)
J223x=subset(J223,Riik!="HBSC kokku")
J223y=subset(J223,Riik=="HBSC kokku")


#joonis
ggplot()+
  facet_grid(~Sugu)+
  geom_col(data=J223x,aes(x=Vanus,y=value,fill=Riik),pos=position_dodge(0.6),width=0.7)+
  geom_line(data=J223y,aes(x=Vanus,y=value,group=Sugu,col=Riik),linewidth=1)+
  geom_point(data=J223y,aes(x=Vanus,y=value,group=Sugu,col=Riik),cex=1.5)+
  geom_text(data=J223y,aes(x=Vanus,y=value+2,group=Sugu,label=value),fontface="bold",cex=3,col="#FF3600")+
  theme_minimal()+
  theme(legend.position = "bottom",legend.title=element_blank())+
  ylab("Noorte osakaal (%)")+
  scale_y_continuous(limits=c(0,50))+
  scale_color_manual(values="#FF3600")+
  scale_fill_manual(values=c("#f09d00","#bf6900","#6666cc","#4026cc","#668080"))+
  theme(text = element_text(color="#668080"),axis.text=element_text(color="#668080"))+
  theme(strip.text.x = element_text(face = "bold",size=10,color="#668080"))