-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
g_lineplot.R
611 lines (575 loc) · 21.3 KB
/
g_lineplot.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#' Function to create line plot of summary statistics over time.
#'
#' @param label text string to be displayed as plot label.
#' @param data `ADaM` structured analysis laboratory data frame e.g. `ADLB`.
#' @param biomarker_var name of variable containing biomarker names.
#' @param biomarker_var_label name of variable containing biomarker labels.
#' @param loq_flag_var name of variable containing `LOQ` flag e.g. `LOQFL`.
#' @param biomarker biomarker name to be analyzed.
#' @param value_var name of variable containing biomarker results.
#' @param unit_var name of variable containing biomarker result unit.
#' @param trt_group name of variable representing treatment group.
#' @param trt_group_level vector that can be used to define the factor level of `trt_group`.
#' @param shape categorical variable whose levels are used to split the plot lines.
#' @param shape_type vector of symbol types.
#' @param time name of variable containing visit names.
#' @param time_level vector that can be used to define the factor level of time. Only use it when
#' x-axis variable is character or factor.
#' @param color_manual vector of colors.
#' @param line_type vector of line types.
#' @param ylim ('numeric vector') optional, a vector of length 2 to specify the minimum and maximum of the y-axis
#' if the default limits are not suitable.
#' @param median boolean whether to display median results.
#' @param hline_arb ('numeric vector') value identifying intercept for arbitrary horizontal lines.
#' @param hline_arb_color ('character vector') optional, color for the arbitrary horizontal lines.
#' @param hline_arb_label ('character vector') optional, label for the legend to the arbitrary horizontal lines.
#' @param xtick a vector to define the tick values of time in x-axis.
#' Default value is `ggplot2::waiver()`.
#' @param xlabel vector with same length of `xtick` to define the label of x-axis tick values.
#' Default value is `ggplot2::waiver()`.
#' @param rotate_xlab boolean whether to rotate x-axis labels.
#' @param plot_font_size control font size for title, x-axis, y-axis and legend font.
#' @param dot_size plot dot size. Default to 3.
#' @param dodge control position dodge.
#' @param plot_height height of produced plot. 989 pixels by default.
#' @param count_threshold \code{integer} minimum number observations needed to show the appropriate
#' bar and point on the plot. Default: 0
#' @param table_font_size \code{float} controls the font size of the values printed in the table.
#' Default: 12
#' @param display_center_tbl boolean whether to include table of means or medians
#'
#'
#' @author Balazs Toth (toth.balazs@gene.com)
#' @author Wenyi Liu (wenyi.liu@roche.com)
#'
#' @details Currently, the output plot can display mean and median of input value. For mean, the
#' error bar denotes
#' 95\% confidence interval. For median, the error bar denotes median-25% quartile to median+75%
#' quartile.
#'
#' @return \code{ggplot} object
#'
#' @export
#'
#' @examples
#' # Example using ADaM structure analysis dataset.
#'
#' library(stringr)
#' library(dplyr)
#' library(nestcolor)
#'
#' # original ARM value = dose value
#' arm_mapping <- list(
#' "A: Drug X" = "150mg QD", "B: Placebo" = "Placebo", "C: Combination" = "Combination"
#' )
#' color_manual <- c("150mg QD" = "thistle", "Placebo" = "orange", "Combination" = "steelblue")
#' type_manual <- c("150mg QD" = "solid", "Placebo" = "dashed", "Combination" = "dotted")
#'
#' ADSL <- rADSL %>% filter(!(ARM == "B: Placebo" & AGE < 40))
#' ADLB <- rADLB
#' ADLB <- right_join(ADLB, ADSL[, c("STUDYID", "USUBJID")])
#' var_labels <- lapply(ADLB, function(x) attributes(x)$label)
#'
#' ADLB <- ADLB %>%
#' mutate(AVISITCD = case_when(
#' AVISIT == "SCREENING" ~ "SCR",
#' AVISIT == "BASELINE" ~ "BL",
#' grepl("WEEK", AVISIT) ~
#' paste(
#' "W",
#' trimws(
#' substr(
#' AVISIT,
#' start = 6,
#' stop = str_locate(AVISIT, "DAY") - 1
#' )
#' )
#' ),
#' TRUE ~ NA_character_
#' )) %>%
#' mutate(AVISITCDN = case_when(
#' AVISITCD == "SCR" ~ -2,
#' AVISITCD == "BL" ~ 0,
#' grepl("W", AVISITCD) ~ as.numeric(gsub("\\D+", "", AVISITCD)),
#' TRUE ~ NA_real_
#' )) %>%
#' # use ARMCD values to order treatment in visualization legend
#' mutate(TRTORD = ifelse(grepl("C", ARMCD), 1,
#' ifelse(grepl("B", ARMCD), 2,
#' ifelse(grepl("A", ARMCD), 3, NA)
#' )
#' )) %>%
#' mutate(ARM = as.character(arm_mapping[match(ARM, names(arm_mapping))])) %>%
#' mutate(ARM = factor(ARM) %>%
#' reorder(TRTORD))
#' attr(ADLB[["ARM"]], "label") <- var_labels[["ARM"]]
#'
#' g_lineplot(
#' label = "Line Plot",
#' data = ADLB,
#' biomarker_var = "PARAMCD",
#' biomarker = "CRP",
#' value_var = "AVAL",
#' trt_group = "ARM",
#' shape = NULL,
#' time = "AVISITCDN",
#' color_manual = color_manual,
#' line_type = type_manual,
#' median = FALSE,
#' hline_arb = c(.9, 1.1, 1.2, 1.5),
#' hline_arb_color = c("green", "red", "blue", "pink"),
#' hline_arb_label = c("A", "B", "C", "D"),
#' xtick = c(0, 1, 5),
#' xlabel = c("Baseline", "Week 1", "Week 5"),
#' rotate_xlab = FALSE,
#' plot_height = 600
#' )
#'
#' g_lineplot(
#' label = "Line Plot",
#' data = ADLB,
#' biomarker_var = "PARAMCD",
#' biomarker = "CRP",
#' value_var = "AVAL",
#' trt_group = "ARM",
#' shape = NULL,
#' time = "AVISITCD",
#' color_manual = NULL,
#' line_type = type_manual,
#' median = TRUE,
#' hline_arb = c(.9, 1.1, 1.2, 1.5),
#' hline_arb_color = c("green", "red", "blue", "pink"),
#' hline_arb_label = c("A", "B", "C", "D"),
#' xtick = c("BL", "W 1", "W 5"),
#' xlabel = c("Baseline", "Week 1", "Week 5"),
#' rotate_xlab = FALSE,
#' plot_height = 600
#' )
#'
#' g_lineplot(
#' label = "Line Plot",
#' data = ADLB,
#' biomarker_var = "PARAMCD",
#' biomarker = "CRP",
#' value_var = "AVAL",
#' trt_group = "ARM",
#' shape = NULL,
#' time = "AVISITCD",
#' color_manual = color_manual,
#' line_type = type_manual,
#' median = FALSE,
#' hline_arb = c(.9, 1.1, 1.2, 1.5),
#' hline_arb_color = c("green", "red", "blue", "pink"),
#' hline_arb_label = c("A", "B", "C", "D"),
#' xtick = c("BL", "W 1", "W 5"),
#' xlabel = c("Baseline", "Week 1", "Week 5"),
#' rotate_xlab = FALSE,
#' plot_height = 600,
#' count_threshold = 90,
#' table_font_size = 15
#' )
#'
#' g_lineplot(
#' label = "Line Plot",
#' data = ADLB,
#' biomarker_var = "PARAMCD",
#' biomarker = "CRP",
#' value_var = "AVAL",
#' trt_group = "ARM",
#' shape = NULL,
#' time = "AVISITCDN",
#' color_manual = color_manual,
#' line_type = type_manual,
#' median = TRUE,
#' hline_arb = c(.9, 1.1, 1.2, 1.5),
#' hline_arb_color = c("green", "red", "blue", "pink"),
#' hline_arb_label = c("A", "B", "C", "D"),
#' xtick = c(0, 1, 5),
#' xlabel = c("Baseline", "Week 1", "Week 5"),
#' rotate_xlab = FALSE,
#' plot_height = 600
#' )
#'
#' g_lineplot(
#' label = "Line Plot",
#' data = subset(ADLB, SEX %in% c("M", "F")),
#' biomarker_var = "PARAMCD",
#' biomarker = "CRP",
#' value_var = "AVAL",
#' trt_group = "ARM",
#' shape = "SEX",
#' time = "AVISITCDN",
#' color_manual = color_manual,
#' line_type = type_manual,
#' median = FALSE,
#' hline_arb = c(.9, 1.1, 1.2, 1.5),
#' hline_arb_color = c("green", "red", "blue", "pink"),
#' hline_arb_label = c("A", "B", "C", "D"),
#' xtick = c(0, 1, 5),
#' xlabel = c("Baseline", "Week 1", "Week 5"),
#' rotate_xlab = FALSE,
#' plot_height = 1500,
#' dot_size = 1
#' )
#'
#' g_lineplot(
#' label = "Line Plot",
#' data = subset(ADLB, SEX %in% c("M", "F")),
#' biomarker_var = "PARAMCD",
#' biomarker = "CRP",
#' value_var = "AVAL",
#' trt_group = "ARM",
#' shape = "SEX",
#' time = "AVISITCDN",
#' color_manual = NULL,
#' median = FALSE,
#' hline_arb = c(.9, 1.1, 1.2, 1.5),
#' hline_arb_color = c("green", "red", "blue", "pink"),
#' hline_arb_label = c("A", "B", "C", "D"),
#' xtick = c(0, 1, 5),
#' xlabel = c("Baseline", "Week 1", "Week 5"),
#' rotate_xlab = FALSE,
#' plot_height = 1500,
#' dot_size = 4
#' )
g_lineplot <- function(label = "Line Plot",
data,
biomarker_var = "PARAMCD",
biomarker_var_label = "PARAM",
biomarker,
value_var = "AVAL",
unit_var = "AVALU",
loq_flag_var = "LOQFL",
ylim = c(NA, NA),
trt_group,
trt_group_level = NULL,
shape = NULL,
shape_type = NULL,
time,
time_level = NULL,
color_manual = NULL,
line_type = NULL,
median = FALSE,
hline_arb = numeric(0),
hline_arb_color = "red",
hline_arb_label = "Horizontal line",
xtick = ggplot2::waiver(),
xlabel = xtick,
rotate_xlab = FALSE,
plot_font_size = 12,
dot_size = 3,
dodge = 0.4,
plot_height = 989,
count_threshold = 0,
table_font_size = 12,
display_center_tbl = TRUE) {
checkmate::assert_numeric(ylim, len = 2)
## Pre-process data
table_font_size <- grid::convertX(grid::unit(table_font_size, "points"), "mm", valueOnly = TRUE)
## - convert to factors
label_trt_group <- attr(data[[trt_group]], "label")
data[[trt_group]] <- if (is.null(trt_group_level)) {
factor(data[[trt_group]])
} else {
factor(data[[trt_group]], levels = trt_group_level)
}
attr(data[[trt_group]], "label") <- label_trt_group
color_manual <- if (is.null(color_manual)) {
temp <- if (!is.null(getOption("ggplot2.discrete.colour"))) {
getOption("ggplot2.discrete.colour")[1:nlevels(data[[trt_group]])]
} else {
gg_color_hue(nlevels(data[[trt_group]]))
}
names(temp) <- levels(data[[trt_group]])
temp
} else {
stopifnot(all(levels(data[[trt_group]]) %in% names(color_manual)))
color_manual
}
line_type <- if (is.null(line_type)) {
stats::setNames(rep("dashed", nlevels(data[[trt_group]])), levels(data[[trt_group]]))
} else {
stopifnot(all(levels(data[[trt_group]]) %in% names(line_type)))
line_type
}
shape_type <- if (is.null(shape)) {
NULL
} else {
if (is.null(shape_type)) {
default_shapes <- c(15:18, 3:14, 0:2)
res <- if (nlevels(data[[shape]]) > length(default_shapes)) {
rep(default_shapes, ceiling(nlevels(data[[shape]]) / length(default_shapes)))
} else {
default_shapes[seq_len(nlevels(data[[shape]]))]
}
stats::setNames(res, levels(data[[shape]]))
} else {
stopifnot(all(levels(data[[shape]]) %in% names(shape_type)))
shape_type
}
}
xtype <- if (is.factor(data[[time]]) || is.character(data[[time]])) {
"discrete"
} else {
"continuous"
}
if (xtype == "discrete") {
data[[time]] <- if (is.null(time_level)) {
factor(data[[time]])
} else {
factor(data[[time]], levels = time_level)
}
}
groupings <- c(time, trt_group, shape)
## Summary statistics
sum_data <- data %>%
filter(!!sym(biomarker_var) == biomarker) %>%
group_by_at(groupings) %>%
summarise(
count = sum(!is.na(!!sym(value_var))),
mean = mean(!!sym(value_var), na.rm = TRUE),
CIup = mean(!!sym(value_var), na.rm = TRUE) + 1.96 * stats::sd(!!sym(value_var), na.rm = TRUE) / sqrt(n()),
CIdown = mean(!!sym(value_var), na.rm = TRUE) - 1.96 * stats::sd(!!sym(value_var), na.rm = TRUE) / sqrt(n()),
median = stats::median(!!sym(value_var), na.rm = TRUE),
quant25 = stats::quantile(!!sym(value_var), 0.25, na.rm = TRUE),
quant75 = stats::quantile(!!sym(value_var), 0.75, na.rm = TRUE)
) %>%
arrange_at(c(trt_group, shape))
## Filter out rows with insufficient number of counts
listin <- list()
listin[[trt_group]] <- sum_data[[trt_group]]
if (!is.null(shape)) {
listin[[shape]] <- sum_data[[shape]]
}
int <- unique_name("int", names(sum_data))
sum_data[[int]] <- new_interaction(listin, sep = " ")
sum_data[[int]] <- stringr::str_wrap(sum_data[[int]], 12)
sum_data[[int]] <- factor(sum_data[[int]], sort(unique(sum_data[[int]])))
unfiltered_data <- sum_data %>% mutate("met_threshold" = count >= count_threshold)
sum_data <- unfiltered_data %>% filter(.data[["met_threshold"]])
## Base plot
pd <- ggplot2::position_dodge(dodge)
if (median) {
line <- "median"
up_limit <- "quant75"
down_limit <- "quant25"
} else {
line <- "mean"
up_limit <- "CIup"
down_limit <- "CIdown"
}
filtered_data <- data %>%
filter(!!sym(biomarker_var) == biomarker)
unit <- filtered_data %>%
pull(unit_var) %>%
unique()
unit1 <- if (is.na(unit) || unit == "") {
" "
} else {
paste0(" (", unit, ") ")
}
biomarker1 <- filtered_data %>%
pull(biomarker_var_label) %>%
unique()
gtitle <- paste0(biomarker1, unit1, stringr::str_to_title(line), " by Treatment @ Visits")
gylab <- paste0(biomarker1, " ", stringr::str_to_title(line), " of ", value_var, " Values")
# Setup legend label
trt_label <- `if`(is.null(attr(data[[trt_group]], "label")), "Dose", attr(data[[trt_group]], "label"))
# Add footnote to identify LLOQ and ULOQ values pulled from data
caption_loqs_label <- h_caption_loqs_label(loqs_data = filtered_data, flag_var = loq_flag_var)
if (is.null(shape)) {
plot1 <- ggplot2::ggplot(
data = sum_data,
ggplot2::aes(
x = !!sym(time),
y = !!sym(line),
color = !!sym(trt_group),
linetype = !!sym(trt_group),
group = !!sym(int)
)
) +
ggplot2::theme_bw() +
ggplot2::geom_point(position = pd, size = dot_size) +
ggplot2::scale_color_manual(
values = color_manual, name = trt_label, guide = ggplot2::guide_legend(ncol = 3, order = 1)
) +
ggplot2::scale_linetype_manual(
values = line_type, name = trt_label, guide = ggplot2::guide_legend(ncol = 3, order = 1)
)
} else {
mappings <- sum_data %>%
ungroup() %>%
select(!!sym(trt_group), !!sym(shape), int) %>%
distinct() %>%
mutate(
cols = color_manual[as.character(!!sym(trt_group))],
types = line_type[as.character(!!sym(trt_group))],
shps = shape_type[!!sym(shape)]
)
col_mapping <- stats::setNames(mappings$cols, mappings$int)
shape_mapping <- stats::setNames(mappings$shps, mappings$int)
type_mapping <- stats::setNames(mappings$types, mappings$int)
plot1 <- ggplot2::ggplot(
data = sum_data,
ggplot2::aes(
x = !!sym(time),
y = !!sym(line),
color = !!sym(int),
linetype = !!sym(int),
group = !!sym(int),
shape = !!sym(int)
)
) +
ggplot2::theme_bw() +
ggplot2::scale_color_manual(" ", values = col_mapping, guide = ggplot2::guide_legend(ncol = 3, order = 1)) +
ggplot2::scale_linetype_manual(" ", values = type_mapping, guide = ggplot2::guide_legend(ncol = 3, order = 1)) +
ggplot2::scale_shape_manual(" ", values = shape_mapping, guide = ggplot2::guide_legend(ncol = 3, order = 1)) +
ggplot2::theme(legend.key.size = grid::unit(1, "cm")) +
ggplot2::geom_point(position = pd, size = dot_size)
}
plot1 <- plot1 +
ggplot2::geom_line(position = pd) +
ggplot2::geom_errorbar(
ggplot2::aes(ymin = !!sym(down_limit), ymax = !!sym(up_limit)),
width = 0.45, position = pd, linetype = "solid"
) +
ggplot2::ggtitle(gtitle) +
ggplot2::labs(caption = paste(
"The output plot can display mean and median of input value.",
"For mean, the error bar denotes 95% confidence interval.",
"For median, the bar denotes the first to third quartile.\n",
caption_loqs_label
)) +
ggplot2::xlab(time) +
ggplot2::ylab(gylab) +
ggplot2::theme(
legend.box = "vertical",
legend.position = "bottom",
legend.direction = "horizontal",
plot.title = ggplot2::element_text(size = plot_font_size, margin = ggplot2::margin(), hjust = 0.5),
axis.title.y = ggplot2::element_text(margin = ggplot2::margin(r = 20))
)
# Apply y-axis zoom range
plot1 <- plot1 +
ggplot2::coord_cartesian(ylim = ylim)
# Format x-label
if (xtype == "continuous") {
plot1 <- plot1 +
ggplot2::scale_x_continuous(breaks = xtick, labels = xlabel, limits = c(NA, NA))
} else if (xtype == "discrete") {
plot1 <- plot1 +
ggplot2::scale_x_discrete(breaks = xtick, labels = xlabel)
}
if (rotate_xlab) {
plot1 <- plot1 +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45, hjust = 1))
}
plot1 <- plot1 + geom_axes_lines(
sum_data,
hline_arb = hline_arb, hline_arb_color = hline_arb_color, hline_arb_label = hline_arb_label
)
# Format font size
if (!is.null(plot_font_size)) {
plot1 <- plot1 +
ggplot2::theme(
axis.title.x = ggplot2::element_text(size = plot_font_size),
axis.text.x = ggplot2::element_text(size = plot_font_size),
axis.title.y = ggplot2::element_text(size = plot_font_size),
axis.text.y = ggplot2::element_text(size = plot_font_size),
legend.title = ggplot2::element_text(size = plot_font_size),
legend.text = ggplot2::element_text(size = plot_font_size)
)
}
labels <- levels(unfiltered_data[[int]])
lines <- sum(stringr::str_count(unique(labels), "\n")) / 2 + length(unique(labels))
minline <- 36
tabletotal <- lines * minline * ifelse(display_center_tbl, 2, 1)
plotsize <- plot_height - tabletotal
if (plotsize <= 250) {
stop("Due to number of line splitting levels the current plot height is not sufficient to display plot.
If applicable, please try a combination of:
* increasing the plot height using the Plot Aesthetic Settings,
* increasing the relative height of plot to table(s),
* increasing the initial maximum plot_height argument during creation of this app,
* and / or consider removing the mean / median table.")
}
if (display_center_tbl) {
unfiltered_data$center <- if (median) {
sprintf(ifelse(unfiltered_data$count > 0, "%.2f", ""), unfiltered_data$median)
} else {
sprintf(ifelse(unfiltered_data$count > 0, "%.2f", ""), unfiltered_data$mean)
}
tbl_central_value_title <- if (median) "Median" else "Mean"
tbl_central_value <- ggplot2::ggplot(
unfiltered_data,
ggplot2::aes(x = !!sym(time), y = !!sym(int), label = .data[["center"]])
) +
ggplot2::geom_text(ggplot2::aes(color = .data[["met_threshold"]]), size = table_font_size) +
ggplot2::ggtitle(tbl_central_value_title) +
ggplot2::theme_minimal() +
ggplot2::scale_y_discrete(labels = labels) +
ggplot2::theme(
panel.grid.major = ggplot2::element_blank(),
legend.position = "none",
panel.grid.minor = ggplot2::element_blank(),
panel.border = ggplot2::element_blank(), axis.text.x = ggplot2::element_blank(),
axis.ticks = ggplot2::element_blank(),
axis.title.x = ggplot2::element_blank(),
axis.title.y = ggplot2::element_blank(),
axis.text.y = ggplot2::element_text(size = plot_font_size),
plot.title = ggplot2::element_text(face = "bold", size = plot_font_size)
) +
ggplot2::scale_color_manual(values = c("FALSE" = "red", "TRUE" = "black"))
}
tbl <- ggplot2::ggplot(
unfiltered_data,
ggplot2::aes(x = !!sym(time), y = !!sym(int), label = .data[["count"]])
) +
ggplot2::geom_text(ggplot2::aes(color = .data[["met_threshold"]]), size = table_font_size) +
ggplot2::ggtitle("Number of observations") +
ggplot2::theme_minimal() +
ggplot2::scale_y_discrete(labels = labels) +
ggplot2::theme(
panel.grid.major = ggplot2::element_blank(),
legend.position = "none",
panel.grid.minor = ggplot2::element_blank(),
panel.border = ggplot2::element_blank(), axis.text.x = ggplot2::element_blank(),
axis.ticks = ggplot2::element_blank(),
axis.title.x = ggplot2::element_blank(),
axis.title.y = ggplot2::element_blank(),
axis.text.y = ggplot2::element_text(size = plot_font_size),
plot.title = ggplot2::element_text(face = "bold", size = plot_font_size)
) +
ggplot2::scale_color_manual(values = c("FALSE" = "red", "TRUE" = "black"))
# Plot the grobs using plot_grid
if (display_center_tbl) {
cowplot::plot_grid(plot1, tbl_central_value, tbl,
align = "v", ncol = 1,
rel_heights = c(plotsize, tabletotal / 2, tabletotal / 2)
)
} else {
cowplot::plot_grid(plot1, tbl, align = "v", ncol = 1, rel_heights = c(plotsize, tabletotal))
}
}
new_interaction <- function(args, drop = FALSE, sep = ".", lex.order = FALSE) { # nolint
for (i in seq_along(args)) {
if (is.null(args[[i]])) {
args[[i]] <- NULL
}
}
if (length(args) == 1) {
return(paste0(names(args), ":", args[[1]]))
}
args <- mapply(function(n, val) paste0(n, ":", val), names(args), args, SIMPLIFY = FALSE)
interaction(args, drop = drop, sep = sep, lex.order = lex.order)
}
unique_name <- function(newname, old_names) {
if (newname %in% old_names) {
unique_name(paste0(newname, "1"), old_names)
}
newname
}
gg_color_hue <- function(n) {
hues <- seq(15, 375, length = n + 1)
grDevices::hcl(h = hues, l = 65, c = 100)[1:n]
}