library(ggplot2)
library(tidyr)

#faili sisselugemine ja andmete formaadi korrigeerimine
J341=read.csv("PT3-T3.4-J3.4.1.csv",header=TRUE, encoding ="UTF-8")
J341$X[19]="EESTI"
levels=reorder(J341$X,J341$Proportion.of.those.feeling.depressed.among.the.employed.and.unemployed)
J341=na.omit(pivot_longer(J341,2:6))
J341$X=as.factor(J341$X)
J341$X=factor(J341$X,levels)
J341x=J341[J341$name %in% c("Employed", "Unemployed", "Retired"),]
J341x$name=as.factor(J341x$name)
J341x$name=factor(J341x$name,levels(J341x$name)[order(c(3,1,2))])
J341y=J341[J341$name=="Proportion.of.those.feeling.depressed.among.the.employed.and.unemployed",]
J341y$name[J341y$name=="Proportion.of.those.feeling.depressed.among.the.employed.and.unemployed"]="Proportion of those feeling depressed among the employed and unemployed"
font=rep(1,27)
font[19]=2

#joonis
ggplot()+
  geom_col(data=J341x, aes(x=X,y=value,fill=name),pos=position_dodge(0.8),width=0.7)+
  scale_fill_manual(values=c("#FF3600","#f09d00","#668080"))+
  geom_point(data=J341y,aes(x=X,y=value*20,pch=name),col="#6666cc",cex=2)+
  theme_minimal()+
  theme(text = element_text(color="#668080"),axis.text=element_text(color="#668080"))+
  scale_y_continuous("%", sec.axis = sec_axis(~ . /20, name = "Proportion",breaks=seq(0,3.5,0.5)))+
  theme(axis.text.x = element_text(angle = 90,face=font))+
  theme(legend.position = "bottom")+
  theme(legend.title = element_blank())+
  xlab("")
## Warning: Vectorized input to `element_text()` is not officially supported.
## ℹ Results may be unexpected or may change in future versions of ggplot2.