/
pupil_plot.R
93 lines (84 loc) · 3.56 KB
/
pupil_plot.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
#' Plot and inspect pupil data
#'
#' Plot and inspect how preprocessing steps are changing the pupil data.
#' An exploratory function to help determine preprocessing parameters. See
#' https://dr-jt.github.io/pupillometry/ for more information.
#'
#' @section Output:
#'
#' Plots.
#'
#' @section Usage:
#'
#' This function is primarily used within other preprocessing functions.
#' (e.g., `pupil_smooth(plot = TRUE)`).
#'
#' @param x data before preprocessing step.
#' @param y data after preprocessing step.
#' @param trial what trial(s) to plot. default = "all"
#' @param sub_title subtitle for plot. default = ""
#' @export pupil_plot
#'
pupil_plot <- function(x, y, trial = "all", sub_title = "") {
theme_spacious <- function(font.size = 14, bold = TRUE){
key.size <- trunc(font.size*.8)
if (bold == TRUE) {
face.type <- "bold"
} else {
face.type <- "plain"
}
ggplot2::theme(text = ggplot2::element_text(size = font.size),
axis.title.x =
ggplot2::element_text(margin =
ggplot2::margin(t = 20, r = 0,
b = 0, l = 0),
face = face.type),
axis.title.y =
ggplot2::element_text(margin =
ggplot2::margin(t = 0, r = 15,
b = 0, l = 0),
face = face.type),
legend.title = ggplot2::element_text(face = face.type),
legend.spacing = ggplot2::unit(20, "pt"),
legend.text = ggplot2::element_text(size = key.size))
}
eyes <- eyes_detect(x)
for (eye in eyes) {
real_name <- eye
colnames(x)[which(colnames(x) == real_name)] <- "pupil_val_before"
colnames(y)[which(colnames(y) == real_name)] <- "pupil_val_after"
x <- dplyr::select(x, Subject, Trial, Time, pupil_val_before)
if (trial != "all") {
x <- dplyr::filter(x, Trial %in% trial)
}
y <- dplyr::select(y, Subject, Trial, Time, pupil_val_after)
if (trial != "all") {
y <- dplyr::filter(y, Trial %in% trial)
}
data_plot <- merge(x, y, by = c("Subject", "Trial", "Time"), all = TRUE)
colors <- c("Before" = "grey65", "After" = "firebrick")
for (trial in unique(x$Trial)) {
data_trial <- dplyr::filter(data_plot, Trial == trial)
plot <- ggplot2::ggplot(data_trial, ggplot2::aes(Time)) +
ggplot2::geom_point(ggplot2::aes(y = pupil_val_before,
color = "Before"),
stroke = 1.2, size = 1.7,
alpha = 1) +
ggplot2::geom_point(ggplot2::aes(y = pupil_val_after,
color = "After"),
stroke = .5, size = .7,
alpha = 1) +
ggplot2::ggtitle(paste("Subject: ", data_trial$Subject[1], " ",
"Trial: ", data_trial$Trial[1], sep = ""),
subtitle = sub_title) +
ggplot2::labs(y = "Pupil Size", x = "Time (ms)", color = "Legend") +
ggplot2::scale_color_manual(values = colors) +
ggplot2::theme_linedraw() + theme_spacious() +
ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5),
plot.subtitle = ggplot2::element_text(hjust = 0.5),
legend.position = "top")
# Print plot
grid::grid.draw(plot)
}
}
}