---
title: "deletion"
author: "Keren Xu"
date: "1/22/2020"
output: 
  rmarkdown::html_document:
    toc: true  
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = T)
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(message = FALSE)
knitr::opts_chunk$set(fig.width= 7, fig.height=5) 
```


# Load packages    
```{r, dev = 'CairoPNG'}
library(tidyverse)
library(tableone)
library(knitr)
library(scales)
library(ggpubr)
library(grid)
library(ggrepel)
theme_set(theme_pubr()) 
```


```{r, fig.width= 12, fig.height = 10}
df_cpg_compare_set12 <- read.csv("df_cpg_compare_set12.csv")
df_cpg_compare_set4 <- read.csv("df_cpg_compare_set4.csv")
df <- rbind(df_cpg_compare_set12, df_cpg_compare_set4) %>% 
  mutate(Set = ifelse(Set%in%c("set1", "set2"), "450K", "EPIC"))

df_C <- cbind(
  direction = c("-", "+", "+", "+", "+", "+", "+", "-", "-", "+", "+", "-", "-", "+", "+", "+", "-", "+", "-", "-", "+", "-", "-", "-", "+", "-"),
  CpG = c( "cg02256631","cg02482603", "cg04103532", "cg04180046", "cg04506190", "cg05549655", "cg08698721", "cg09743950", "cg10799846", "cg12186702", "cg13834112", "cg13893782", "cg14179389", "cg14351425", "cg14633298", "cg14743346", "cg17397069", "cg19381766", "cg22154659", "cg22802102", "cg23304605", "cg25189904", "cg25949550", "cg26764244", "cg27291468", "cg05575921")
) %>% data.frame()

df %>%
  pivot_longer(-c("X", "subjectid", "Set"), names_to = "CpG", values_to = "value") %>% 
  left_join(., df_C) %>% 
  unite("CpG_direction", "CpG", "direction", remove = FALSE, sep = " ") %>% 
  ggboxplot(., x = "Set", y = "value", 
          color = "Set", palette = "npg",
          facet.by = "CpG_direction", ncol = 10, short.panel.labs = T,  varwidth = TRUE) + 
  stat_compare_means(method = "t.test", label = "p.format", label.y = 1.0) + 
  scale_y_continuous(limits = c(0,1.1), breaks = c(0, 0.25, 0.5, 0.75, 1.0)) + xlab(" ") + ylab(" ") + 
  labs(color = "Sample") + 
  theme(legend.position = "bottom")
```
