-
Notifications
You must be signed in to change notification settings - Fork 3
/
recipe-plottingSnippets.Rmd
185 lines (147 loc) · 5.91 KB
/
recipe-plottingSnippets.Rmd
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
# Plotting snippets {#recipe:plottingSnippets}
This recipe contains various plotting snippets for various visualization challenges. All snippets will use the ae demo emuDB. Let us create and load that emuDB:
```{r, message = FALSE, results = 'hide'}
library(emuR)
library(tidyverse) # containing - amongst others - dplyr, purrr, tibble, and ggplot2
# create demo data in directory provided by the tempdir() function
# (of course other directory paths may be chosen)
create_emuRdemoData(dir = tempdir())
# create path to demo data directory, which is
# called "emuR_demoData"
demo_data_dir = file.path(tempdir(),
"emuR_demoData")
# create path to ae_emuDB which is part of the demo data
path2ae = file.path(demo_data_dir,
"ae_emuDB")
# load database
# (verbose = F is only set to avoid additional output in manual)
ae = load_emuDB(path2ae, verbose = F)
```
## Formant trajectory plots (equivalent to the legacy `dplot(..., normalise = T/F, average = T/F,...)`)
```{r, message = FALSE, results = 'hide'}
# query A and V (front and back open vowels),
# i:and u: (front and back closed vowels), and
# E and o: (front and back mid vowels)
ae_vowels = query(emuDBhandle = ae,
query = "[Phonetic == V | A | i: | u: | o: | E]")
#get the formants:
ae_formants = get_trackdata(ae,
seglist = ae_vowels,
ssffTrackName = "fm",
resultType = "tibble")
# plot all F2 trajectories
# (note that T1 == F1, T2 == F2, ...)
ggplot(ae_formants) +
aes(x = times_rel, y = T2, col = labels, group = sl_rowIdx) +
geom_line() +
labs(x = "time (ms)", y = "F2 (Hz)") +
theme(legend.position = "none")
```
```{r, message = FALSE, results = 'hide'}
# time normalize the formant values
ae_formants_norm = normalize_length(ae_formants)
# plot all normalized F2 trajectories
ggplot(ae_formants_norm) +
aes(x = times_norm, y = T2, col = labels, group = sl_rowIdx) +
geom_line() +
labs(x = "normalized time", y = "F2 (Hz)") +
theme(legend.position = "none")
```
```{r, message = FALSE, results = 'hide'}
# calculate and plot averages (== dplot(..., average = T, ...))
ae_formants_norm_average = ae_formants_norm %>%
group_by(labels, times_norm) %>%
summarise(F2 = mean(T2))
ggplot(ae_formants_norm_average) +
aes(x = times_norm, y = F2, col = labels) +
geom_line() +
labs(x = "normalized time", y = "F2 (Hz)") +
theme(legend.position = "none")
```
## F1/F2 plots (equivalent to the legacy `eplot(..., dopoints = T/F, doellipse = T/F, centroid = T/F, ...)`)
```{r, message = FALSE, results = 'hide'}
# query A and V (front and back open vowels),
# i:and u: (front and back closed vowels), and
# E and o: (front and back mid vowels)
ae_vowels = query(emuDBhandle = ae,
query = "[Phonetic == V | A | i: | u: | o: | E]")
#get the formants:
ae_formants = get_trackdata(ae,
seglist = ae_vowels,
ssffTrackName = "fm",
resultType = "tibble")
# time normalize the formant values
ae_formants_norm = normalize_length(ae_formants)
# extract the temporal mid-points
ae_midpoints = ae_formants_norm %>%
filter(times_norm == 0.5)
# plot F1 & F2 values (== eplot(..., dopoints = T, doellipse = F, centroid = F, ...))
ggplot(ae_midpoints) +
aes(x = T2, y = T1, label = labels, col = labels) +
geom_text() +
scale_y_reverse() + scale_x_reverse() +
labs(x = "F2 (Hz)", y = "F1 (Hz)") +
theme(legend.position = "none")
```
```{r, message = FALSE, results = 'hide'}
# plot F1 & F2 values (== eplot(..., dopoints = T, doellipse = T, centroid = F, ...))
ggplot(ae_midpoints) +
aes(x = T2, y = T1, label = labels, col = labels) +
geom_text() +
stat_ellipse() +
scale_y_reverse() + scale_x_reverse() +
labs(x = "F2 (Hz)", y = "F1 (Hz)") +
theme(legend.position = "none")
```
```{r, message = FALSE, results = 'hide'}
# filter out vowels with enough data points
# to calc. ellipse
ae_midpoints_Eiu = ae_midpoints %>% filter(labels%in%c("E","i:","u:"))
ae_centroid = ae_midpoints_Eiu %>%
group_by(labels) %>%
summarise(T1 = mean(T1), T2 = mean(T2))
# plot F1 & F2 values (== eplot(..., dopoints = T, doellipse = T, centroid = T, ...))
ggplot(ae_midpoints_Eiu) +
aes(x = T2, y = T1, label = labels, col = labels) +
stat_ellipse() +
scale_y_reverse() + scale_x_reverse() +
labs(x = "F2 (Hz)", y = "F1 (Hz)") +
theme(legend.position = "none") +
geom_text(data = ae_centroid)
```
Regarding stat_ellipse() this is worth pointing out: https://github.com/tidyverse/ggplot2/issues/2776
## F1/F2 plot separated by speaker
```{r, message = FALSE, results = 'hide'}
# query A and V (front and back open vowels),
# i:and u: (front and back closed vowels), and
# E and o: (front and back mid vowels)
ae_vowels = query(emuDBhandle = ae,
query = "[Phonetic == V | A | i: | u: | o: | E]")
#get the formants:
ae_formants = get_trackdata(ae,
seglist = ae_vowels,
ssffTrackName = "fm",
resultType = "tibble")
# extract the temporal mid-points
ae_midpoints = ae_formants %>%
filter(times_norm == 0.5)
# plot using facet_wrap()
# to plot vowels separately for every bundle
# (this assumes that every bundle contains a different
# speaker which is actually not the case in the ae emuDB)
ggplot(ae_midpoints) +
aes(x = T2, y = T1, label = labels, col = labels) +
geom_text() +
scale_y_reverse() + scale_x_reverse() +
labs(x = "F2 (Hz)", y = "F1 (Hz)") +
theme(legend.position = "none") +
facet_wrap(~bundle)
```
```{r echo=FALSE, results='hide', message=FALSE}
# disconnect to avoid file locking to sqliteDB that causes unlink
# to fail under windows
# DBI::dbDisconnect(db_handle$connection)
# clean up emuR_demoData
unlink(file.path(tempdir(), "emuR_demoData"), recursive = TRUE)
unlink(file.path(tempdir(),'my-first_emuDB'), recursive = TRUE)
```