#open packages

library(ggplot2)
library(ggthemes)
library(gridExtra)

#read data

lf <- read.csv("C:\\Users\\USER\\Desktop\\Articles\\HA330\\Data\\Analysis\\Liver Support.csv",
               header=TRUE)
lf$td <- factor(lf$td)
names(lf)
##  [1] "td"           "GCS"          "M"            "HATT"        
##  [5] "HATtr"        "nh.tho"       "SpO2"         "nh.do"       
##  [9] "glu"          "Ure"          "Cre"          "pH"          
## [13] "pO2"          "pCO2"         "HCO3"         "Lactat"      
## [17] "P.F"          "RBC"          "TC"           "WBC"         
## [21] "NEU"          "PT."          "PT.s"         "INR"         
## [25] "APTT"         "fib"          "Na"           "K"           
## [29] "Cl"           "Catp"         "Ca..."        "Mg.."        
## [33] "Bilirubin.TP" "Bil.TT"       "GOT"          "GPT"         
## [37] "albumin"

#nhip tho

p1 <- ggplot(lf, aes(x=td, y=nh.tho, group=1)) + 
geom_line(col="#DC143C") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="Nh\u1ECBp th\u1EDF") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#Nhiet do

p2 <- ggplot(lf, aes(x=td, y=nh.do, group=1)) + 
geom_line(col="#0000FF") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="Nhi\u1EC7t \u0111\u1ED9") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#Glagow

p3 <- ggplot(lf, aes(x=td, y=GCS, group=1)) + 
geom_line(col="#FF1493") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="Glasgow") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#SpO2

p4 <- ggplot(lf, aes(x=td, y=SpO2, group=1)) + 
geom_line(col="#FF1493") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="SpO2(%)") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#combine four plots above

grid.arrange(p1, p2, p3, p4, ncol=2)

#Mach

m1 <- ggplot(lf, aes(x=td, y=M, group=1)) + 
geom_line(col="#0000FF") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="M\u1EA1ch") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#HATT

m2 <- ggplot(lf, aes(x=td, y=HATtr, group=1)) + 
geom_line(col="#FF1493") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="HA t\u00E2m thu") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#HATtr

m3 <- ggplot(lf, aes(x=td, y=HATT, group=1)) + 
geom_line(col="#FF1493") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="HA t\u00E2m tr\u01B0\u01A1ng") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#Blank plot

m4 <- ggplot(lf, aes(x=td, group=1)) + 
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#combine four plots above

grid.arrange(m1, m2, m3, m4, ncol=2)

##Kidney function #plot ure

k1 <- ggplot(lf, aes(x=td, y=Ure, group=1)) + 
geom_line(col="#FF1493") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="Urea") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#plot creatinin

k2 <- ggplot(lf, aes(x=td, y=Cre, group=1)) + 
geom_line(col="#FFD700") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="Creatinin") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#combine two plots above

grid.arrange(k1, k2, nrow=2, ncol=1)

##Liver function #plot total bilirubin

b1 <- ggplot(lf, aes(x=td, y=Bilirubin.TP, group=1)) + 
geom_line(col="#FF0000") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="Bilirubin to\u00E0n ph\u1EA7n") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#plot direct bilirubin

b2 <- ggplot(lf, aes(x=td, y=Bil.TT, group=1)) + 
geom_line(col="#00FF00") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="Bilirubin tr\u1EF1c ti\u1EBFp") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#plot indirect bilirubin

b3 <- ggplot(lf, aes(x=td, y=Bilirubin.TP - Bil.TT, group=1)) + 
geom_line(col="#0000FF") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="Bilirubin gi\u00E1n ti\u1EBFp") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#plot GOT

b4 <- ggplot(lf, aes(x=td, y=GOT, group=1)) + 
geom_line(col="#FF00FF") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="GOT") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#plot GPT

b5 <- ggplot(lf, aes(x=td, y=GPT, group=1)) + 
geom_line(col="#FFD700") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="GPT") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#plot albumin

b6 <- ggplot(lf, aes(x=td, y=albumin, group=1)) + 
geom_line(col="#191970") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="albumin") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#combine Blood Gas Results

grid.arrange(b1, b2, b3, b4, b5, b6, nrow=3, ncol=2)

##Blood Gas Results #plot pH

b1 <- ggplot(lf, aes(x=td, y=pH, group=1)) + 
geom_line(col="#FF0000") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="pH") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#plot pO2

b2 <- ggplot(lf, aes(x=td, y=pO2, group=1)) + 
geom_line(col="#00FF00") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="pO2") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#plot pCO2

b3 <- ggplot(lf, aes(x=td, y=pCO2, group=1)) + 
geom_line(col="#0000FF") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="pH") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#plot pH

b4 <- ggplot(lf, aes(x=td, y=pH, group=1)) + 
geom_line(col="#FF00FF") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="pH") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#plot HCO3

b5 <- ggplot(lf, aes(x=td, y=HCO3, group=1)) + 
geom_line(col="#FFD700") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="HCO3") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#plot P/F

b6 <- ggplot(lf, aes(x=td, y=P.F, group=1)) + 
geom_line(col="#191970") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="P/F") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#combine Blood Gas Results

grid.arrange(b1, b2, b3, b4, b5, b6, nrow=3, ncol=2)

##Total Blood Count #RBC

bc1 <- ggplot(lf, aes(x=td, y=RBC, group=1)) + 
geom_line(col="#191970") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="H\u1ED3ng c\u1EA7u") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#PLT

bc2 <- ggplot(lf, aes(x=td, y=TC, group=1)) + 
geom_line(col="#191970") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="Ti\u1EC3u c\u1EA7u") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#WBC

bc3 <- ggplot(lf, aes(x=td, y=WBC, group=1)) + 
geom_line(col="#191970") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="B\u1EA1ch c\u1EA7u") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#NEUT

bc4 <- ggplot(lf, aes(x=td, y=NEU, group=1)) + 
geom_line(col="#191970") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="B\u1EA1ch c\u1EA7u trung t\u00EDnh (%)") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#combine total blood counts

grid.arrange(bc1, bc2, bc3, bc4, nrow=2, ncol=2)

##coagulation test # PT%

c1 <- ggplot(lf, aes(x=td, y=PT., group=1)) + 
geom_line(col="#191970") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="PT (%)") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#PT (s)

c2 <- ggplot(lf, aes(x=td, y=PT.s, group=1)) + 
geom_line(col="#191970") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="PT (s)") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#INR

c3 <- ggplot(lf, aes(x=td, y=INR, group=1)) + 
geom_line(col="#191970") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="INR") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#APTT(s)

c4 <- ggplot(lf, aes(x=td, y=APTT, group=1)) + 
geom_line(col="#191970") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="APTT (s)") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#Fibrinogen

c5 <- ggplot(lf, aes(x=td, y=fib, group=1)) + 
geom_line(col="#191970") +
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
labs(x="Th\u1EDDi \u0111i\u1EC3m", y="Fibrinogen") +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#Blank plot

c6 <- ggplot(lf, aes(x=td, group=1)) + 
  scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"),
        labels=c("T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7")) +
theme_economist(base_size = 8, base_family="sans", horizontal = TRUE)

#combine cogulation test plots

grid.arrange(c1, c2, c3, c4, c5, c6, nrow=3, ncol=2)