-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassumption_plot.R
More file actions
256 lines (196 loc) · 6.27 KB
/
Copy pathassumption_plot.R
File metadata and controls
256 lines (196 loc) · 6.27 KB
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
#' AssumptionPlotter Plot
#'
#' @details
#' This function creates the plots in the Plots tab of AssumptionPlotter
#' @param df Selected dataset.
#' @param participant Participant to be plotted.
#' @param variables All variables to be plotted.
#' @param expected_days Expected days of the study
#' @param beeps_per_day Expected beeps per day
#' @param include_day Option to include day labels.
#' @param include_day_line Option to include day lines.
#' @param impute Method for imputing NA's.
#' @param add_trend Include a regression line showing the trend in the data.
#' @param trend_type Type of trend line to be passed to geom_smooth().
#' @param theme_choice Decide what theme the plot should have.
#' @param palette Chooses either no palette or custom.
#' @param palette_options Palette options. Any of grDevices::hcl.pals().
#' @param text_font What text font the plot should have.
#' @param axis_text_size Adjust the text size of the plot's axes.
#' @param legend_text_size Adjust the text size of the legend.
#' @return Interactive plot in AssumptionPlotter.
#' @import ggplot2
#' @import dplyr
#' @import tidyr
#' @import grDevices
#' @export
#' @examples
#' \dontrun{
#' assumption_plot(
#' df,
#' participant,
#' variables,
#' expected_days,
#' beeps_per_day,
#' include_day = TRUE,
#' include_day_line = TRUE,
#' impute = c("none", "mean", "mode"),
#' add_trend = FALSE,
#' trend_type = c("lm", "loess"),
#' theme_choice = c("classic", "minimal", "bw", "dark"),
#' palette = c("none", "custom"),
#' palette_option = "Reds",
#' text_font = c("sans", "serif", "mono"),
#' axis_text_size = 12,
#' legend_text_size = 12
#' )
#' }
#'
assumption_plot <- function(
df,
participant,
variables,
expected_days,
beeps_per_day,
include_day = TRUE,
include_day_line = TRUE,
impute = "none",
add_trend = FALSE,
trend_type = "lm",
theme_choice = "classic",
palette = "none",
palette_option = "Reds",
text_font = "sans",
axis_text_size = 12,
legend_text_size = 12){
# Create error message for when all variables are deselected
if(length(variables)==0){
stop("The plot cannot render when no variables are selected")
}
# Subset data
## In the future it would be cool to implement the option to plot multiple
## participants at once (so you can compare them).
df <- df %>%
filter(id == participant)
# Keep relevant variables
keep_cols <- c(
"id",
"day",
"beep",
"missing",
variables
)
df <- df[,keep_cols]
# Specify imputation method (per variable)
if(impute!="none"){
for(v in variables){
if(impute=="mean"){
df[[v]][is.na(df[[v]])] <-mean(df[[v]],na.rm=TRUE)
}
if(impute == "mode"){
mode_val <- names(sort(table(df[[v]]), decreasing = TRUE))[1]
if(is.numeric(df[[v]])) {
mode_val <- as.numeric(mode_val)
}
df[[v]][is.na(df[[v]])] <- mode_val
}
}
}
# Ensure that all variables are numeric
df[variables] <- lapply(df[variables], as.numeric)
# Create plotting index
df <- df %>%
mutate(
plot_x = (day - 1) * beeps_per_day + beep
) %>%
arrange(plot_x)
# Make df long format (required for plotting with ggplot)
long_df <- df %>%
pivot_longer(
cols = -c(id, day, beep, missing, plot_x),
names_to = "Variables", # Risky name since name is same as argument
values_to = "value"
)
# Create location of vertical day lines on the x-axis
day_lines <- seq(beeps_per_day+.5, # Make line appear between last and first day
max(long_df$plot_x),
by = beeps_per_day)
# Create location of days
## Note that this is not ideal currently. Might make sense to define max days
## and beeps by participant. Whether to plot predetermined expected days and
## beeps for all participants or a specific one could be plot argument.
day_labels <- data.frame(
day = 1:expected_days,
x = (0:(expected_days - 1)) * beeps_per_day +
(beeps_per_day + 1) / 2,
label = paste("Day", 1:expected_days)
)
# Initialize plot
p <- ggplot2::ggplot(long_df, aes(x = plot_x, y = value, color = Variables))+
geom_line()+
geom_point()+
ylab("Value")+
xlab("Time")
# Option to include day labels
if(include_day){
p <- p +
geom_label(
data = day_labels,
aes(x = x, y = Inf, label = label),
inherit.aes = FALSE,
vjust = 1.5,
size = 4,
alpha = .8
)
}
# Option to include lines separating days
if(include_day_line){
p <- p +
geom_vline(xintercept = day_lines, alpha = .15, linetype = "dashed")
}
# Option to add a trend line
if(add_trend){
p <- p +
geom_smooth(method = trend_type,
se = FALSE)
}
# Create x-axis tick labels
breaks <- sort(unique(long_df$plot_x))
labels <-rep(1:beeps_per_day, length.out = length(breaks))
## Edit x-axis tick labels
p <- p +
scale_x_continuous(breaks = breaks, labels = labels)
# Ensure that y-lim is large enough to show all data points of all variables
p <- p +
coord_cartesian(ylim = c(
min(long_df$value, na.rm = TRUE),
max(long_df$value, na.rm = TRUE)
))
# Switch themes
p <- switch(theme_choice,
minimal = p + theme_minimal(base_family = text_font),
classic = p + theme_classic(base_family = text_font),
bw = p + theme_bw(base_family = text_font),
dark = p + theme_dark(base_family = text_font),
p)
# Switch the colors of the plot
## Get number of variables
nr_var <- length(variables)
p <- switch(palette,
none = p + scale_color_hue(),
custom = p + scale_color_manual(values =
grDevices::hcl.colors(nr_var, palette_option,
rev=TRUE)),
p)
# Edit font sizes
p <- p +
theme(
text = element_text(size = axis_text_size),
axis.title = element_text(size = axis_text_size+2),
axis.text = element_text(size = axis_text_size),
legend.title = element_text(size = legend_text_size+2),
legend.text = element_text(size = legend_text_size)
)
# Return plot
return(p)
}