---
title: "Lprob_PS_RE"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
library(xlsx)
library(ggplot2)
library(zoo)
library(gridExtra)
library(cmcplot)


```

```{r}
#ps data processing
# load data and formate dates
rawPS <- read.xlsx("./Lprob_PS_RE.xlsx", sheetName = "PS", startRow = 2)
rawPS[,1]<- gsub("m","-",rawPS[,1])
rawPS[,1] <- as.character(rawPS[,1])
rawPS[,1] <- as.yearmon(rawPS[,1])
names(rawPS)[1] <- "date"
names(rawPS)[12:14] <- c("age1", "age2", "age3")

# sorting data
industry_ps <- rawPS[,1:4]
occupation_ps <- rawPS[,c(1,5:8)]
education_ps <- rawPS[,c(1,9:11)]
age_ps <- rawPS[, c(1, 12:14)]


```

```{r}
#RE data processing
# load data and formate dates
rawRE <- read.xlsx("./Lprob_PS_RE.xlsx", sheetName = "RE", startRow = 2)
rawRE[,1]<- gsub("m","-",rawRE[,1])
rawRE[,1] <- as.character(rawRE[,1])
rawRE[,1] <- as.yearmon(rawRE[,1])
names(rawRE)[1] <- "date"
names(rawRE)[11:13] <- c("age1", "age2", "age3")

# sorting data
industry_re <- rawRE[,1:4]
occupation_re <- rawRE[,c(1,5:7)]
education_re <- rawRE[,c(1,8:10)]
age_re <- rawRE[, c(1, 11:13)]


```

```{r}
#ps plot

#age
ps_age1 <- ggplot(data=age_ps) +geom_line(aes(x = date, y = age1)) +ylim(0.6,1) + xlab(element_blank()) + ylab("Age") +
  theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("16-24")
ps_age2 <- ggplot(data=age_ps) +geom_line(aes(x = date, y = age2)) +ylim(0.7,1) + xlab(element_blank()) + ylab(element_blank()) +
    theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("24-55")

ps_age3 <- ggplot(data=age_ps) +geom_line(aes(x = date, y = age3)) +ylim(0.7,1) + xlab(element_blank()) + ylab(element_blank()) + 
    theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("55+")

#education
ps_edu1 <- ggplot(data=education_ps) +geom_line(aes(x = date, y = High.school)) +ylim(0.7,1) + xlab(element_blank()) + ylab("Education")+ 
    theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("High School")
ps_edu2 <- ggplot(data=education_ps) +geom_line(aes(x = date, y = Some.college)) +ylim(0.7,1) + xlab(element_blank()) + ylab(element_blank())+ 
    theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("Some college")
ps_edu3 <- ggplot(data=education_ps) +geom_line(aes(x = date, y = College.graduation)) +ylim(0.7,1) + xlab(element_blank()) + ylab(element_blank())+ 
    theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("College Graduation")

#industry
ps_ind1 <- ggplot(data=industry_ps) +geom_line(aes(x = date, y = Cons.Manu)) +ylim(0.7,1) + xlab(element_blank()) + ylab("Industry")+ 
    theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("Construction and Manufacturing")
ps_ind2 <- ggplot(data=industry_ps) +geom_line(aes(x = date, y = Trade.Transportation)) +ylim(0.7,1) + xlab(element_blank()) + ylab(element_blank())+ 
    theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("Trade and Transportation")
ps_ind3 <- ggplot(data=industry_ps) +geom_line(aes(x = date, y = Service)) +ylim(0.7,1) + xlab(element_blank()) + ylab(element_blank())+ 
    theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("Service")

#occupation
ps_occ1 <- ggplot(data=occupation_ps) +geom_line(aes(x = date, y = Routine.Manual)) +ylim(0.7,1) + xlab(element_blank()) + ylab("Occupation")+ 
    theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("Routine Manual")
ps_occ2 <- ggplot(data=occupation_ps) +geom_line(aes(x = date, y = Routine.Cognitive)) +ylim(0.7,1) + xlab(element_blank()) + ylab(element_blank())+ 
    theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("Routine Cognitive")
ps_occ3 <- ggplot(data=occupation_ps) +geom_line(aes(x = date, y = Non.routine.Manual), ) +ylim(0.7,1) + xlab(element_blank()) + ylab(element_blank())+ 
    theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("Non-routine Manual")
ps_occ4 <-ggplot(data=occupation_ps) +geom_line(aes(x = date, y = Non.routine.Cognitive))+ylim(0.7,1)  + xlab(element_blank()) + ylab(element_blank())+ 
    theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE)+
  ggtitle("Non-routine Cognitive") 


#arranging on a grid


mat_layout <- rbind(c(NA, rep(1,2), rep(2,2), rep(3,2), NA),
                                   c(NA, rep(4,2), rep(5,2), rep(6,2), NA),
                                   c(NA, rep(7,2), rep(8,2), rep(9,2), NA),
                                   c(rep(10,2), rep(11,2), rep(12,2),rep(13,2)))

all.grid <- grid.arrange(ps_age1, ps_age2, ps_age3,
             ps_edu1, ps_edu2, ps_edu3,
             ps_ind1, ps_ind2, ps_ind3,
             ps_occ1, ps_occ2, ps_occ3, ps_occ4,
             layout_matrix = mat_layout
             )

ggsave('ps.png', all.grid, width = 11, height = 8)
```

```{r}
#re plot

#age
re_age1 <- ggplot(data=age_re) +geom_line(aes(x = date, y = age1)) +ylim(0.7,1) + xlab(element_blank()) + ylab("Age") +
  theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("16-24")
re_age2 <- ggplot(data=age_re) +geom_line(aes(x = date, y = age2)) +ylim(0.7,1) + xlab(element_blank()) + ylab(element_blank()) +
    theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("24-55")

re_age3 <- ggplot(data=age_re) +geom_line(aes(x = date, y = age3)) +ylim(0.7,1) + xlab(element_blank()) + ylab(element_blank()) + 
    theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("55+")

#education
re_edu1 <- ggplot(data=education_re) +geom_line(aes(x = date, y = High.school)) +ylim(0.7,1) + xlab(element_blank()) + ylab("Education")+ 
    theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("High School")
re_edu2 <- ggplot(data=education_re) +geom_line(aes(x = date, y = Some.college)) +ylim(0.7,1) + xlab(element_blank()) + ylab(element_blank())+ 
    theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("Some college")
re_edu3 <- ggplot(data=education_re) +geom_line(aes(x = date, y = College.graduation)) +ylim(0.7,1) + xlab(element_blank()) + ylab(element_blank())+ 
    theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("College Graduation")

#industry
re_ind1 <- ggplot(data=industry_re) +geom_line(aes(x = date, y = Cons.Manu)) +ylim(0.7,1) + xlab(element_blank()) + ylab("Industry")+ 
    theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("Construction and Manufacturing")
re_ind2 <- ggplot(data=industry_re) +geom_line(aes(x = date, y = Trade.Transportation)) +ylim(0.7,1) + xlab(element_blank()) + ylab(element_blank())+ 
    theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("Trade and Transportation")
re_ind3 <- ggplot(data=industry_re) +geom_line(aes(x = date, y = Service)) +ylim(0.7,1) + xlab(element_blank()) + ylab(element_blank())+ 
    theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("Service")

#occupation
re_occ1 <- ggplot(data=occupation_re) +geom_line(aes(x = date, y = Routine.Manual)) +ylim(0.7,1) + xlab(element_blank()) + ylab("Occupation")+ 
    theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("Routine Manual")
re_occ2 <- ggplot(data=occupation_re) +geom_line(aes(x = date, y = Routine.Cognitive)) +ylim(0.7,1) + xlab(element_blank()) + ylab(element_blank())+ 
    theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("Routine Cognitive")
re_occ3 <- ggplot(data=occupation_re) +geom_line(aes(x = date, y = Non.routine), ) +ylim(0.7,1) + xlab(element_blank()) + ylab(element_blank())+ 
    theme_frb_base() +
  coord_cartesian_frb(line_sides = "tblr", 
                      tick_sides = "tblr", 
                      hooks = FALSE, 
                      x_tick_adj = FALSE)+
  ggtitle("Non-routine")
# re_occ4 <-ggplot(data=occupation_re) +geom_line(aes(x = date, y = Non.routine.Cognitive))+ylim(0.7,1)  + xlab(element_blank()) + ylab(element_blank())+ 
#     theme_frb_base() +
#   coord_cartesian_frb(line_sides = "tblr", 
#                       tick_sides = "tblr", 
#                       hooks = FALSE)+
#   ggtitle("Non-routine Cognitive") 


#arranging on a grid


# mat_layout <- rbind(c(NA, rep(1,2), rep(2,2), rep(3,2), NA),
#                                    c(NA, rep(4,2), rep(5,2), rep(6,2), NA),
#                                    c(NA, rep(7,2), rep(8,2), rep(9,2), NA),
#                                    c(rep(10,2), rep(11,2), rep(12,2),rep(13,2)))


all.grid <- grid.arrange(re_age1, re_age2, re_age3,
             re_edu1, re_edu2, re_edu3,
             re_ind1, re_ind2, re_ind3,
             re_occ1, re_occ2, re_occ3,
             ncol = 3
             )

ggsave('re.png', all.grid, width = 11, height = 8)
```