######################Ridgeline plots
rm(list = ls())
library(ggplot2)
library(ggridges)
library(igraph) # function "categorical_pal"
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
#install.packages("igraph")
###############################input data 
dir_path <- "C:\\Users\\liyix\\OneDrive\\Desktop\\"
dir_path_name <- dir(dir_path,pattern = ".*.csv",full.names = T)
dir_path_name
## [1] "C:\\Users\\liyix\\OneDrive\\Desktop\\data_1.csv"
#####################################node
data_1 <- read.csv(grep("data_1.csv",dir_path_name,value = T),header = T,stringsAsFactors = F)
colnames(data_1); dim(data_1); str(data_1); head(data_1)
## [1] "in.vivo.toxicity.endpoint" "FDR"
## [1] 5620    2
## 'data.frame':    5620 obs. of  2 variables:
##  $ in.vivo.toxicity.endpoint: chr  "A" "A" "A" "A" ...
##  $ FDR                      : num  6.14e-33 1.99e-27 9.55e-27 9.55e-27 4.29e-26 ...
##   in.vivo.toxicity.endpoint      FDR
## 1                         A 6.14e-33
## 2                         A 1.99e-27
## 3                         A 9.55e-27
## 4                         A 9.55e-27
## 5                         A 4.29e-26
## 6                         A 4.38e-24
####################################density plot
ggplot(data_1, aes(x = FDR)) +
  geom_density(aes(color = in.vivo.toxicity.endpoint),adjust = 1) +
  xlim(-0.01, 0.05)

data_1$in.vivo.toxicity.endpoint <- factor(data_1$in.vivo.toxicity.endpoint,levels = as.character(unique(data_1$in.vivo.toxicity.endpoint))[8:1])

#View(data_1)
data_1$FDR_class <- data_1$FDR
data_1$FDR_class[data_1$FDR < 0.01] <- 1
data_1$FDR_class[data_1$FDR >= 0.01] <- 2
table(data_1$FDR_class)
## 
##    1    2 
## 4135 1485
table(data_1$in.vivo.toxicity.endpoint)
## 
##   H   G   F   E   D   C   B   A 
## 603 663 700 678 701 733 633 909
#################################
ggplot(data_1, aes(x = FDR_class, y = in.vivo.toxicity.endpoint,color = in.vivo.toxicity.endpoint)) +
  geom_density_ridges(aes(fill = in.vivo.toxicity.endpoint),linetype="solid",
                      scale = 1,rel_min_height = 0,alpha = 0.5)+
  scale_fill_manual(values = categorical_pal(8))+
  scale_color_manual(values = categorical_pal(8))+
  scale_y_discrete(expand = c(0, 0)) +
  scale_x_continuous(expand = c(0, 0),limits = c(0.65, 2.4),
                     breaks = seq(1,2,1),
                     labels=c("< 0.01", paste("\u2265", "0.01",sep = " ")))+
  labs(x="False discovery rate (FDR)", y="In vivi toxicity endpoint", title="")+
  theme(panel.spacing = unit(0.1, "cm"),
        legend.position= "none",
        axis.ticks = element_line(colour = "black", 
                                  size = 0.5, linetype = "solid"),
        axis.line = element_line(colour = "black", 
                                 size = 0.5, linetype = "solid"),
        axis.text =element_text(face="plain", color="black", family = "sans",
                                size=14,angle = 0),
        panel.background = element_rect(fill = "white",
                                        colour = "white",
                                        size = 0.5, linetype = "solid"),
        panel.grid.major = element_line(size = 1, linetype = 'dashed',
                                  colour = "white"),
        axis.title = element_text(color="black", size=14, face="plain",family="sans")) 
## Picking joint bandwidth of 0.107

###############scale = 2
ggplot(data_1, aes(x = FDR_class, y = in.vivo.toxicity.endpoint,color = in.vivo.toxicity.endpoint)) +
  geom_density_ridges(aes(fill = in.vivo.toxicity.endpoint),linetype="solid",
                      scale = 2,rel_min_height = 0,alpha = 0.5)+
  scale_fill_manual(values = categorical_pal(8))+
  scale_color_manual(values = categorical_pal(8))+
  scale_y_discrete(expand = c(0, 0)) +
  scale_x_continuous(expand = c(0, 0),limits = c(0.65, 2.4),
                     breaks = seq(1,2,1),
                     labels=c("< 0.01", paste("\u2265", "0.01",sep = " ")))+
  labs(x="False discovery rate (FDR)", y="In vivi toxicity endpoint", title="")+
  theme(panel.spacing = unit(0.1, "cm"),
        legend.position= "none",
        axis.ticks = element_line(colour = "black", 
                                  size = 0.5, linetype = "solid"),
        axis.line = element_line(colour = "black", 
                                 size = 0.5, linetype = "solid"),
        axis.text =element_text(face="plain", color="black", family = "sans",
                                size=14,angle = 0),
        panel.background = element_rect(fill = "white",
                                        colour = "white",
                                        size = 0.5, linetype = "solid"),
        panel.grid.major = element_line(size = 1, linetype = 'dashed',
                                        colour = "white"),
        axis.title = element_text(color="black", size=14, face="plain",family="sans")) 
## Picking joint bandwidth of 0.107

#output
ggsave(filename = paste0(Sys.Date(),"-ridgeline_plot.tif"), plot = last_plot(), 
       device = "tiff", path = dir_path,
       scale = 1, width = 16, height = 16, units = "cm",
       dpi = 300, limitsize = TRUE, compression = "lzw")
## Picking joint bandwidth of 0.107
##########ref https://cran.r-project.org/web/packages/ggridges/vignettes/introduction.html