-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsticker_code.R
412 lines (323 loc) · 12.7 KB
/
sticker_code.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
library(ggplot2)
library(png)
library(grid)
library(hexSticker)
library(qqplotr)
library(cowplot)
library(gridExtra)
# the hexSticker package is currently producing an error even with using test example from: https://github.com/GuangchuangYu/hexSticker
# The following code creats the panel to show on the sticker
make_plot <- function(){
resid_spanel <- function(resid, pred, plots = "SAS", bins = NA, scale = 1,
smoother = FALSE, theme = "bw",
axis.text.size = 10, title.text.size = 12,
title = TRUE, qqline = TRUE, qqbands = FALSE){
## Errors and Warnings -------------------------------------------------------
# Return an error if the request residual type is not available for the model type
# Return an error if the requested plots involve standardizing residuals for an 'lmer' or
# 'glmer' model
# Return an error if smoother option is not specified correctly
if(smoother == TRUE | smoother == FALSE){
}else{
stop("Smoother option for residual plot not specified correctly.
Choose either TRUE or FALSE.")
}
# Return an error if theme is not specified correctly
if(theme == "bw" | theme == "classic" | theme == "grey" | theme == "gray"){
}else{
theme = "bw"
warning("Theme option not specified correctly. Accepted themes are bw, classic,
and grey (or gray). Default theme will be used.")
}
# Return an error if smoother option is not specified correctly
if(title == TRUE | title == FALSE){
}else{
stop("Title option not specified correctly. Choose either TRUE or FALSE.")
}
# Return a warning about choosing number of bins if a histogram is included
if("SAS" %in% plots | "hist" %in% plots){
if(is.na(bins)){
bins = 30
warning("By default, bins = 30 in the histogram of residuals. If necessary, specify
an appropriate number of bins.")
}
}
## Creation of plots ---------------------------------------------------------
# Create a boxplot of the residuals if selected in plots otherwise set as NULL
if("boxplot" %in% plots | "SAS" %in% plots | "all" %in% plots){
boxplot <- resid_sboxplot(resid,pred, theme, axis.text.size, title.text.size, title)+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank(),
axis.text.y = element_blank(),
axis.ticks.y=element_blank())
} else{
boxplot <- NULL
}
# Create a histogram of the residuals if selected in plots otherwise set as NULL
if("hist" %in% plots | "SAS" %in% plots | "all" %in% plots | "SASextend" %in% plots){
hist <- resid_shist(resid, pred, bins = bins, theme, axis.text.size,
title.text.size, title)+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank(),
axis.text.y = element_blank(),
axis.ticks.y=element_blank())
} else{
hist <- NULL
}
# Create a q-q plot of the residuals if selected in plots otherwise set as NULL
if("qq" %in% plots | "SAS" %in% plots){
qq <- resid_sqq(resid, pred, theme, axis.text.size, title.text.size, title, qqline, qqbands)+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank(),
axis.text.y = element_blank(),
axis.ticks.y=element_blank())
} else{
qq <- NULL
}
# Create a residual plot if selected in plots otherwise set as NULL
if("residplot" %in% plots | "SAS" %in% plots){
residplot <- resid_splot(resid, pred, smoother, theme, axis.text.size, title.text.size, title)+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank(),
axis.text.y = element_blank(),
axis.ticks.y=element_blank())
} else{
residplot <- NULL
}
## Creation of grid of plots -------------------------------------------------
# If individual plots have been specified, set plots equal to "individual"
# Return an error if none of the correct plot options have been specified
if(plots=="SAS"){
plots <- plots
} else if("boxplot" %in% plots | "cookd" %in% plots | "hist" %in% plots |
"ls" %in% plots | "qq" %in% plots | "residlev" %in% plots |
"residplot" %in% plots | "respred" %in% plots){
plots <- "individual"
} else{
stop("Invalid plots option entered")
}
# Create a grid of plots based on the plots specified
if(plots == "SAS"){
# Create grid of SAS plots in resid panel
plot_grid(residplot, hist, qq, boxplot, scale = scale)
}else if (plots == "individual") {
# Turn the specified plots into a list
individual_plots <- list(residplot = residplot, hist = hist, qq = qq,
boxplot = boxplot)
# Remove the plots which are null
individual_plots <- individual_plots[-which(sapply(individual_plots, is.null))]
# Turn the list of plots into a grob
my_grobs = lapply(individual_plots, ggplotGrob)
# Specify number of columns for grid of plots based on number of plots specified
ifelse(length(individual_plots) == 1, grid_col <- 1, grid_col <- 2)
# Create grid of individual plots specified
grid.arrange(grobs = my_grobs, ncol = grid_col, scale = scale)
}
}
resid_splot <- function(resid, pred,smoother, theme, axis.text.size, title.text.size, title){
# Create a data frame with the residuals
model_values <- data.frame(Residual=resid, Prediction=pred)
model_values$Lowess.x <- lowess(x=model_values$Prediction, y=model_values$Residual)$x
model_values$Lowess.y <- lowess(x=model_values$Prediction, y=model_values$Residual)$y
# Create the residual plot
plot <- ggplot(data=model_values, aes(x = Prediction, y = Residual)) +
geom_point(size=4) +
geom_abline(slope = 0, intercept = 0, color = "blue",size=2) +
labs(x = "Predicted Values", y = "Residuals")
# If smoother is set to true, add it to the plot
if (smoother == TRUE){
plot <- plot +
geom_line(aes(Lowess.x, Lowess.y),colour = "red", size = 0.5)
}
# Add theme to plot
if (theme == "bw"){
plot <- plot + theme_bw()
} else if (theme == "classic"){
plot <- plot + theme_classic()
} else if (theme == "gray" | theme == "grey"){
plot <- plot + theme_grey()
}
# Set text size of title and axis labels, determine whether to include a title, and return plot
if(title == TRUE){
plot +
labs(title = "Residuals Plot") +
theme(plot.title = element_text(size = title.text.size, face = "bold"),
axis.title = element_text(size = axis.text.size))
} else if (title == FALSE){
plot + theme(axis.title = element_text(size = axis.text.size))
}
}
resid_sqq <- function(resid, pred, theme, axis.text.size, title.text.size, title, qqline, qqbands){
r <- data.frame(r=resid)
if(qqbands==TRUE){
plot <- ggplot(data = r, mapping = aes(sample = r)) +
stat_qq_band()+
stat_qq_point(size=4) +labs(x = "Theoretical Quantiles", y = "Sample Quantiles")
}else{
plot <- ggplot(data = r, mapping = aes(sample = r)) +
stat_qq_point(size=4) +labs(x = "Theoretical Quantiles", y = "Sample Quantiles")
}
if(qqline==TRUE){
plot <- plot+stat_qq_line(color="blue",linewidth=2)
}
# Add theme to plot
if (theme == "bw"){
plot <- plot + theme_bw()
} else if (theme == "classic"){
plot <- plot + theme_classic()
} else if (theme == "gray" | theme == "grey"){
plot <- plot + theme_grey()
}
# Set text size of title and axis labels, determine whether to include a title, and return plot
if(title == TRUE){
plot +
labs(title = "Q-Q Plot of Residuals") +
theme(plot.title = element_text(size = title.text.size, face = "bold"),
axis.title = element_text(size = axis.text.size))
} else if (title == FALSE){
plot + theme(axis.title = element_text(size = axis.text.size))
}
}
resid_sboxplot <- function(resid,pred, theme, axis.text.size, title.text.size, title){
model_values <- data.frame(Residual=resid)
model_values$Observation <- 1:nrow(model_values)
# Create the boxplot of residuals
plot <- ggplot(model_values, aes(x = " ", y = Residual)) +
geom_boxplot(size=2,outlier.size = 4,width=.5) +
geom_point(alpha=0)+
labs(x = " ", y = "Residuals") +
theme(plot.title = element_text(size = 12, face = "bold"),
axis.title = element_text(size = 10))
# Add theme to plot
if (theme == "bw"){
plot <- plot + theme_bw()
} else if (theme == "classic"){
plot <- plot + theme_classic()
} else if (theme == "gray" | theme == "grey"){
plot <- plot + theme_grey()
}
# Set text size of title and axis labels, determine whether to include a title, and return plot
if (title == TRUE){
plot +
labs(title = "Boxplot of Residuals") +
theme(plot.title = element_text(size = title.text.size, face = "bold"),
axis.title = element_text(size = axis.text.size))
} else if (title == FALSE){
plot + theme(axis.title = element_text(size = axis.text.size))
}
}
resid_shist <- function(resid, pred, bins, theme, axis.text.size, title.text.size, title){
#If bins=NA, use default
if(is.na(bins)){
bins <- 30
}
model_values <- data.frame(resid=resid)
#Step to make sure are not cutting out any huge outliers
if (min(model_values$resid) < -4*sd(model_values$resid)){
min_x <- NA
}else{
min_x <- -4*sd(model_values$resid)
}
if (max(model_values$resid) > 4*sd(model_values$resid)){
max_x <- NA
}else{
max_x <- 4*sd(model_values$resid)
}
#do not want xlim if data outside 4*sd
if (is.na(min_x)&is.na(max_x)){
# Create the histogram of residuals
plot <- ggplot(model_values, aes(x = resid)) +
geom_histogram(aes(y = ..density.., fill = ..count..),
color = "black", fill = "grey82", bins = 12,size=1.5) +
stat_function(fun = dnorm, color = "blue",size=2,
args = list(mean = 0,
sd = sd(model_values$resid))) +
labs(x = "Residuals", y = "Density") +
theme(plot.title = element_text(size = 12, face = "bold"),
axis.title = element_text(size = 10))
}else{
# Create the histogram of residuals
plot <- ggplot(model_values, aes(x = resid)) +
geom_histogram(aes(y = ..density.., fill = ..count..),
color = "black", fill = "grey82", bins = 12,size=1.5) +
stat_function(fun = dnorm, color = "blue",size=2,
args = list(mean = 0,
sd = sd(model_values$resid))) +
labs(x = "Residuals", y = "Density") +
xlim(c(min_x, max_x))+
theme(plot.title = element_text(size = 12, face = "bold"),
axis.title = element_text(size = 10))
}
# Add theme to plot
if (theme == "bw"){
plot <- plot + theme_bw()
} else if (theme == "classic"){
plot <- plot + theme_classic()
} else if (theme == "gray" | theme == "grey"){
plot <- plot + theme_grey()
}
# Set text size of title and axis labels, determine whether to include a title, and return plot
if(title == TRUE){
plot +
labs(title = "Histogram of Residuals") +
theme(plot.title = element_text(size = title.text.size, face = "bold"),
axis.title = element_text(size = axis.text.size))
} else if (title == FALSE){
plot + theme(axis.title = element_text(size = axis.text.size))
}
}
x <- rnorm(30)
hist(x)
y <- x+rnorm(30,0,1)
plot(x,y)
m <- lm(y~x)
gg <- resid_spanel(resid(m), fitted(m),title = FALSE, theme="classic")
return(gg)
}
gg <- make_plot()
gg
#pic <- readPNG("sticker_plot2.png")
#pic <- rasterGrob(pic, width = 1, x = 0.5, y = 0.61, interpolate = TRUE)
# gg <- ggplot() +
# annotation_custom(pic) +
# theme_void()
set.seed(123)
#col_border <- "dodgerblue4" # Cambridge Blue
#col_border <- "mediumblue"
col_border <- "blue3"
#col_border <- "navyblue"
#too dark
#col_bg <- "grey90"
col_bg <- "grey25"
#col_bg <- "grey70"
col_text <- col_border
col_text <- "#FFFFFF"
url_color <- col_text
sticker(gg,
package="ggResidpanel",
p_size = 12,
p_y=1.5,
s_x = 1,
s_y = .8,
s_width = 1.4,
s_height = 1.4,
h_fill = col_bg,
h_color = col_border,
p_family = "Aller_Lt",
filename="gg_resid_sticker4.png",
spotlight = FALSE,
l_x = 1.0,
l_y = 1.5,
l_alpha = 0.3,
p_color = col_text,
url = "https://github.com/goodekat/ggResidpanel",
u_color = url_color,
u_size=3.5)