generated from stjude-biohackathon/KIDS24-team
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qqplot.R
89 lines (66 loc) · 2.62 KB
/
qqplot.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
qq.plot=function(form, # formula or name of variable in data
data, # data.frame
clr="sbp.clrs", # color scheme
txt=1, # level of detail in narrative text
mda=1, # missing data action
rpt=F, # include in report T/F
fig.type="pdf", # R function to produce figure file
...)
{
########################################
# convert data to a data.frame and process the form argument
data=as.data.frame(data)
# process the form argument
try.form=try(as.formula(form),silent=T) # determine whether form is a formula
if (class(try.form)=="try-error") # if not, form is a column of data
{
#form=as.character(deparse(substitute(form)))
if (!(form%in%colnames(data)))
stop(paste0(form," is not a column of input data."))
}
#####################################################
# get caption and colors for the q-q plot
fig.cap1=temp=resp=NULL
{
#temp=ggpubr::ggqqplot(test[,form])
fig.cap1=paste0("A Quantile-Quantile plot of ",form,". ")
resp=form
clrs=define.colors(1,clr)
}
################################################
# add more details to the caption as requested by the user
fig.cap2=NULL
if (txt>1)
{
fig.cap2=paste0("If all the points fall approximately along this reference line,
we can assume normality.")
}
#################################################
# add missing data alert as requested by the user
fig.cap3=NULL
nmiss=nrow(data)-length(which(is.na(data[,form])))
if ((mda>0)&&(nmiss>0))
{
fig.cap3=paste0("The plot does not show ",nmiss,
" observation",c("","s")[1+(nmiss>1)]," with missing data.")
}
if ((mda>0)&&(nmiss==0))
{
fig.cap3=paste0("There was no missing data. The plot shows all data. ")
}
fig.cap=paste(fig.cap1,fig.cap2,fig.cap3,collapse="")
################################
# Generate the figure and add it to the report if requested
if (rpt) report.figure(fig.cap,fig.type,...)
par(mar=c(5,10,1,1)) # set wide left margin for group names
{
# boxplot(data[,form],horizontal=T,
# las=1,ylab="",xlab=form,
# col=clrs)
qqnorm(data[,form], pch = 1, frame = FALSE)
qqline(data[,form], col = clrs, lwd = 2)
}
if (rpt) dev.off()
res=list(fig.cap=fig.cap)
return(res)
}