-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_fit_cowdiag.R
167 lines (145 loc) · 6.32 KB
/
process_fit_cowdiag.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Diagnostic plots based on diagnostic_dashboard using cowplot package
# Output is similar in size and font to original script
# Note: ggplot2 theme plot.margin is significantly reduced
# -----------------------------------------------------------------------
# Set up workspace
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Remove all current objects in the workspace
rm(list = ls(all = TRUE))
graphics.off()
# Set the working directory
master.dir <- "E:/Hughes/Data/PK/FLAG"
scriptname <- "process_fit"
setwd(master.dir)
#Load libraries
library(ggplot2)
library(cowplot)
# Source utility functions file
source("E:/Hughes/functions_utility.r")
#Customize ggplot2 theme
theme_bw2 <- theme_set(theme_bw(22))
theme_bw2 <- theme_update(
plot.margin = unit(c(0.5, 0.5, 1, 0.5), "lines"),
axis.title.x = element_text(size = 16, vjust = 0),
axis.title.y = element_text(size = 16, vjust = 0, angle = 90),
strip.text.x = element_text(size = 16),
strip.text.y = element_text(size = 16, angle = 90)
)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Read in the fit file
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Set the name of the required file and set the working directory
cat("Select one of files in directory to process:\n")
path <- gsub("\\\\", "/", file.choose())
base.path <- dirname(path)
setwd(base.path)
file.name.in <- basename(path)
file.name.out <- paste(file.name.in, ".csv", sep = "")
#Read *.fit file and attach, so column names are available
fitdata <- read.table(
file = file.name.in, sep = "", skip = 1, header = T,
na.strings = c("NA","***********","1.#INFE+00")
)
# Write to file
write.csv(fitdata, file = file.name.out)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Perform data specific corrections
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Remove dose events & missing values
fitdata <- subset(fitdata, MDV == 0)
fitdata$HT[fitdata$HT == 0 & fitdata$GEND == 1] <- 1.75
fitdata$HT[fitdata$HT == 0 & fitdata$GEND == 0] <- 1.6
#Set factors and levels
fitdata$DXCATf <- factor(fitdata$DXCAT)
levels(fitdata$DXCATf) <- c("CLL", "AML", "ALL", "MM")
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Diagnostic plots using cowplot
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Define colourblind palette and custom palette
cbPalette <- data.frame(
grey = "#999999",
orange = "#E69F00",
skyblue = "#56B4E9",
green = "#009E73",
yellow = "#F0E442",
blue = "#0072B2",
red = "#D55E00",
pink = "#CC79A7",
stringsAsFactors = F
)
myPalette <- with(cbPalette, c(blue, green, red, pink))
# Plot 1 - OBS vs PRED
# Note: legend is extracted here
xy.lim1 <- c(-0.1, max(c(fitdata$PRED, fitdata$DV), na.rm = T))
p1 <- NULL
p1 <- ggplot(fitdata)
p1 <- p1 + geom_point(aes(x = PRED, y = DV, colour = DXCATf),
shape = 1) #, alpha = 0.5, size = 1)
p1 <- p1 + geom_abline(aes(x = PRED, y = DV),
intercept = 0, slope = 1, colour = "black") # add line of identity
p1 <- p1 + geom_smooth(aes(x = PRED, y = DV),
method = loess, se = T, colour = "red") # add loess smoothing line
p1 <- p1 + xlab("Population Predicted (ug/mL)")
p1 <- p1 + ylab("Observed (ug/mL)")
p1 <- p1 + coord_cartesian(xlim = xy.lim1, ylim = xy.lim1) # set limits
p1 <- p1 + scale_colour_manual(name = "Cancer Type", values = myPalette)
pLegend <- get_legend(p1) ### extract legend before removal ###
p1 <- p1 + theme(legend.position = "none") # remove legend
# Plot 2 - OBS vs IPRED
xy.lim2 <- c(-0.1, max(c(fitdata$IPRED, fitdata$DV), na.rm = T))
p2 <- NULL
p2 <- ggplot(fitdata)
p2 <- p2 + geom_point(aes(x = IPRED, y = DV, colour = DXCATf),
shape = 1) #, alpha = 0.5, size = 1)
p2 <- p2 + geom_abline(aes(x = IPRED, y = DV),
intercept = 0, slope = 1, colour = "black") # add line of identity
p2 <- p2 + geom_smooth(aes(x = IPRED, y = DV),
method = loess, se = T, colour = "red") # add loess smoothing line
p2 <- p2 + xlab("Individual Predicted (ug/mL)")
p2 <- p2 + ylab("Observed (ug/mL)")
p2 <- p2 + coord_cartesian(xlim = xy.lim2, ylim = xy.lim2) # set limits
p2 <- p2 + scale_colour_manual(values = myPalette)
p2 <- p2 + theme(legend.position = "none") # remove legend
# Plot 3 CWRES vs TAD
y.lim <- max(abs(fitdata$CWRES), na.rm = T)
if (y.lim < 0.1) y.lim <- 0.1
p3 <- NULL
p3 <- ggplot(fitdata)
p3 <- p3 + geom_point(aes(x = TAD, y = CWRES, colour = DXCATf),
shape = 1) #, alpha = 0.5, size = 1)
p3 <- p3 + geom_abline(aes(x = TAD, y = CWRES),
intercept = 0, slope = 0, colour = "black") # add zero line
p3 <- p3 + geom_smooth(aes(x = TAD, y = CWRES),
method = loess, se = T, colour = "red") # add loess smoothing line
p3 <- p3 + xlab("Time after dose (h)")
p3 <- p3 + ylab("CWRES")
p3 <- p3 + coord_cartesian(ylim = c(-y.lim, y.lim))
p3 <- p3 + scale_colour_manual(values = myPalette)
p3 <- p3 + theme(legend.position = "none") # remove legend
# Plot 4 CWRES vs PRED
p4 <- NULL
p4 <- ggplot(fitdata)
p4 <- p4 + geom_point(aes(x = PRED, y = CWRES, colour = DXCATf),
shape = 1) #, alpha = 0.5, size = 1)
p4 <- p4 + geom_abline(aes(x = PRED, y = CWRES),
intercept = 0, slope = 0, colour = "black") # add zero line
p4 <- p4 + geom_smooth(aes(x = PRED, y = CWRES),
method = loess, se = T, colour = "red") # add loess smoothing line
p4 <- p4 + xlab("Population Predicted (ug/mL)")
p4 <- p4 + ylab("CWRES")
p4 <- p4 + coord_cartesian(ylim = c(-y.lim, y.lim))
p4 <- p4 + scale_colour_manual(values = myPalette)
p4 <- p4 + theme(legend.position = "none") # remove legend
# Combine the plots with cowplot
# Define top half: two columns, align size of plots
pTop <- plot_grid(p1, p2, ncol = 2, align = "hv")
# Define bottom plots: 2 rows, align size of plots
pCWRES <- plot_grid(p3, p4, ncol = 1, align = "hv")
# Define bottome half: 2 columns, relative width 80% plots, 20% legends
pBot <- plot_grid(pCWRES, pLegend, ncol = 2, rel_widths = c(0.8, 0.2))
# Final plot: 1 column, do not alignment of plots
plot_grid(pTop, pBot, ncol = 1)
# Save plot to file
# Cowplot implementation of ggsave, use ggplot2::ggsave() if misbehaving
ggsave(paste(base.path, "diag_dash.png", sep = "/"),
width = 10, height = 12.78, units = c("in"), dpi = 72) # 720x920 px