-
Notifications
You must be signed in to change notification settings - Fork 2
/
exp1_corrections_erp_plots.R
172 lines (148 loc) · 5.3 KB
/
exp1_corrections_erp_plots.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
# corrections
# make better ERP charts - aggregated
# ERPLab doesn't seem to work as expected
library(dplyr)
library(ggplot2)
library(readr)
library(tidyr)
# load palettes for plotting
source("/media/eub/MyPassport/R_code/phd_palettes.R")
# folder with CSV files
erp_folder <- "./ERPs/ERPs/corrections_plots/"
# txt were exported from ERPLab. then were opened in Excel and saved as csv
txt_erps <- list.files(erp_folder, pattern = ".csv")
erp_path <- paste0(erp_folder, txt_erps)
# function to clean ERP csv files
clean_erp_csv <- function(x, tone, condition) {
# clean the basename
clean_basename <- gsub("\\.csv$", "", basename(x))
clean_basename <- gsub("^AVG-", "", clean_basename)
# assign if missing
if (missing(tone)) {
tone <- substr(clean_basename, nchar(clean_basename) - 3, nchar(clean_basename))
}
if (missing(condition)) {
condition <- substr(clean_basename, 1, 4)
}
all_falls <- readr::read_csv(x)
# keep the key electrodes (1-9)
all_falls <- all_falls[,1:11]
# remove frontal
all_falls$Fp1 <- NULL
# gather
tidy_all <- all_falls %>% tidyr::gather(., key = "Electrode", value = "Amplitude", -time)
# create factors from the electrode names
tidy_all$Horizontal <- substr(tidy_all$Electrode, 1, 1)
tidy_all$Horizontal <- factor(tidy_all$Horizontal, levels = c("F", "C", "P"))
tidy_all$Vertical <- substr(tidy_all$Electrode, 2, 2)
tidy_all$Vertical <- factor(tidy_all$Vertical, levels = c("3", "z", "4"))
# assign a tone
tidy_all$Tone <- tools::toTitleCase(tone) # use title case
# assign a condition
tidy_all$Condition <- gsub("[[:punct:]]", "", condition)
return(tidy_all)
}
### aggregates - all conditions but self, i.e. those without 100 ms silence
# load files
erp_path[1]
all_falls_but_self <- clean_erp_csv(erp_path[1], "Fall", "all_but_self")
erp_path[2]
all_rises_but_self <- clean_erp_csv(erp_path[2], "Rise", "all_but_self")
all_but_self <- bind_rows(all_falls_but_self, all_rises_but_self)
# collapse the electrodes
all_but_self %>%
group_by(time) %>%
summarise(MAmp = mean(Amplitude))
# and tones
all_but_self %>%
group_by(time, Tone) %>%
summarise(MAmp = mean(Amplitude))
ggplot(all_but_self %>%
group_by(time, Tone) %>%
summarise(Amplitude = mean(Amplitude)),
aes(x = time, y = Amplitude, col = Tone)) +
geom_line() +
labs(x = "Time (ms)", y = "Amplitude (μM)") +
theme_bw() +
theme(legend.position = "top")
#### plots
ggplot(all_but_self, aes(x = time, y = Amplitude, col = Tone)) +
geom_line() +
facet_grid(Horizontal~Vertical) +
labs(x = "Time (ms)", y = "Amplitude (μM)") +
theme_bw() +
theme(legend.position = "top")
### individual conditions ###
# define data frame
all_conditions <- data.frame()
# loop
for (i in 3:10) {
print(erp_path[i])
temp_df <- clean_erp_csv(erp_path[i])
all_conditions <- bind_rows(all_conditions, temp_df)
}
# clean names
all_conditions$Condition <-
dplyr::recode(all_conditions$Condition,
perc = "Perception",
self = "Self-Production",
shad = "Shadowing",
sil = "Silent Mouthing")
# without adjustment
# plot conditions, tones and electrodes
ggplot(all_conditions,
aes(x = time, y = Amplitude, col = Condition)) +
scale_color_manual(values = vocalisation_palette) +
geom_line() +
facet_grid(Tone~Horizontal~Vertical) +
labs(x = "Time (ms)", y = "Amplitude (μM)") +
theme_bw() +
theme(legend.position = "top") +
xlim(c(-49, 349))
# with adjustment
# remove 100 ms delay to allow aligning conditions
all_conditions_adj <- all_conditions
all_conditions_adj$time <- ifelse(all_conditions_adj$Condition != "Self-Production",
all_conditions_adj$time - 100,
all_conditions_adj$time)
# full view - to use in the thesis (appendix)
ggplot(all_conditions_adj,
aes(x = time, y = Amplitude, col = Condition)) +
scale_color_manual(values = vocalisation_palette) +
geom_line() +
facet_grid(Tone~Horizontal~Vertical) +
labs(x = "Time (ms)", y = "Amplitude (μV)") +
theme_bw() +
theme(legend.position = "top") +
xlim(c(-49, 349))
## collapse ## across electrodes
all_conditions_adj_tone_collapsed <-
all_conditions_adj %>%
group_by(time, Condition, Electrode) %>%
summarise(Amplitude = mean(Amplitude))
# clean
all_conditions_adj_tone_collapsed$Horizontal <-
substr(all_conditions_adj_tone_collapsed$Electrode, 1, 1)
all_conditions_adj_tone_collapsed$Horizontal <-
factor(all_conditions_adj_tone_collapsed$Horizontal,
levels = c("F", "C", "P"))
all_conditions_adj_tone_collapsed$Vertical <-
substr(all_conditions_adj_tone_collapsed$Electrode, 2, 2)
all_conditions_adj_tone_collapsed$Vertical <-
factor(all_conditions_adj_tone_collapsed$Vertical,
levels = c("3", "z", "4"))
# plot
ggplot(all_conditions_adj_tone_collapsed,
aes(x = time, y = Amplitude, col = Condition)) +
scale_color_manual(values = vocalisation_palette) +
geom_line() +
facet_grid(Horizontal~Vertical) +
labs(x = "Time (ms)", y = "Amplitude (μV)") +
theme_bw() +
theme(legend.position = "top") +
xlim(c(-49, 349))
# doesn't render micro symbol in pdf :/
ggsave("exp1_erp_all_conditions_timealigned_tone_collapsed.pdf",
width = 6, height = 6)
ggsave("exp1_erp_all_conditions_timealigned_tone_collapsed.png",
width = 5.5, height = 5.5)