emuR has for historical reasons some specialized objects and specialized methods that allow working with these specialized emuR objects. While it is sometimes unavoidable to have such specialized objects and methods, it should be avoided to do so whenever possible - instead, we could use some standardized procedures that are very common in R.

In order to see the advantages of a more standardized procedure, let us one again create a temporary EMU-SDMS-database first (using, by the way, some specialized (but unavoidable) commands from the package emuR):

# load package
library(emuR)
# create demo data in directory
# provided by tempdir()
create_emuRdemoData(dir = tempdir())
# create path to demo database
path2ae = file.path(tempdir(), "emuR_demoData", "ae_emuDB")
# load database
ae = load_emuDB(path2ae, verbose = F)

Specialized objects containing data need specialized commands to plot data

As we have seen in chapter 06, the default resulting object of a call to get_trackdata() is of class trackdata, which is a very special class only existing in the package emuR (and its predecessors). The emuR package provides multiple specialized routines such as dcut(), trapply(), eplot and dplot() for processing and visually inspect objects of this type (see Harrington, 2010, for the use of these functions).

vowels = query(ae,query="Phonetic==i:|u:|E")
vowels_fm = get_trackdata(ae,
                        seglist = vowels,
                        ssffTrackName =  "fm",
                        verbose = FALSE)
# show class of vowels_fm
class(vowels_fm)
## [1] "trackdata"

The folloing command then extracts the formant values at the temporal midpoint of each segment (each vowel, in this case):

vowels_fm05=dcut(vowels_fm,.5,prop = TRUE)

We can then use this object to plot the data and 95%-confidence ellipses.

eplot(vowels_fm05[,1:2],label(vowels),centroid=TRUE,formant = TRUE)

The original emutrack trackdata object can be used to plot trajectories of formants (here: F2 only) as a function of time (first example) or a mean trajectory for each vowel categories’ F2 as a function of normalized time (second example)

dplot(vowels_fm[,2],label(vowels))

dplot(vowels_fm[,2],label(vowels),normalise=TRUE,average=TRUE)

These commands (and many other commands in the predecessors of emuR) are specialized to work with (and only with) emutrack data objects.

Use of standard data.frames instead of specialized objects

In most cases, however, a R user will store his data in data.frames. Data.frames are required for most commands in most packages concerned with plotting and/or statistical analyses.

As the emutrack trackdata object is a fairly complex nested matrix object with internal reference matrices, which can be cumbersome to work with, the emuR package introduces a new equivalent object type called emuRtrackdata that essentially is a flat data.frame or data.table object. This object type can be retrieved by setting the resultType parameter of the get trackdata() function to emuRtrackdata:

vowels_fm_new = get_trackdata(ae,
                        seglist = vowels,
                        ssffTrackName =  "fm",
                        resultType="emuRtrackdata",
                        verbose = FALSE)
# show class of vowels_fm_new
class(vowels_fm_new)
## [1] "emuRtrackdata" "data.frame"
vowels_fm_new
##     sl_rowIdx labels    start      end session    level    type times_orig
## 1           1      E  949.925 1031.925    0000 Phonetic SEGMENT      952.5
## 2           1      E  949.925 1031.925    0000 Phonetic SEGMENT      957.5
## 3           1      E  949.925 1031.925    0000 Phonetic SEGMENT      962.5
## 4           1      E  949.925 1031.925    0000 Phonetic SEGMENT      967.5
## 5           1      E  949.925 1031.925    0000 Phonetic SEGMENT      972.5
## 6           1      E  949.925 1031.925    0000 Phonetic SEGMENT      977.5
## 7           1      E  949.925 1031.925    0000 Phonetic SEGMENT      982.5
## 8           1      E  949.925 1031.925    0000 Phonetic SEGMENT      987.5
## 9           1      E  949.925 1031.925    0000 Phonetic SEGMENT      992.5
## 10          1      E  949.925 1031.925    0000 Phonetic SEGMENT      997.5
## 11          1      E  949.925 1031.925    0000 Phonetic SEGMENT     1002.5
## 12          1      E  949.925 1031.925    0000 Phonetic SEGMENT     1007.5
## 13          1      E  949.925 1031.925    0000 Phonetic SEGMENT     1012.5
## 14          1      E  949.925 1031.925    0000 Phonetic SEGMENT     1017.5
## 15          1      E  949.925 1031.925    0000 Phonetic SEGMENT     1022.5
## 16          1      E  949.925 1031.925    0000 Phonetic SEGMENT     1027.5
## 17          2     i: 1419.925 1463.175    0000 Phonetic SEGMENT     1422.5
## 18          2     i: 1419.925 1463.175    0000 Phonetic SEGMENT     1427.5
## 19          2     i: 1419.925 1463.175    0000 Phonetic SEGMENT     1432.5
## 20          2     i: 1419.925 1463.175    0000 Phonetic SEGMENT     1437.5
## 21          2     i: 1419.925 1463.175    0000 Phonetic SEGMENT     1442.5
## 22          2     i: 1419.925 1463.175    0000 Phonetic SEGMENT     1447.5
## 23          2     i: 1419.925 1463.175    0000 Phonetic SEGMENT     1452.5
## 24          2     i: 1419.925 1463.175    0000 Phonetic SEGMENT     1457.5
## 25          2     i: 1419.925 1463.175    0000 Phonetic SEGMENT     1462.5
## 26          3     u: 2211.175 2283.675    0000 Phonetic SEGMENT     2212.5
## 27          3     u: 2211.175 2283.675    0000 Phonetic SEGMENT     2217.5
## 28          3     u: 2211.175 2283.675    0000 Phonetic SEGMENT     2222.5
## 29          3     u: 2211.175 2283.675    0000 Phonetic SEGMENT     2227.5
## 30          3     u: 2211.175 2283.675    0000 Phonetic SEGMENT     2232.5
## 31          3     u: 2211.175 2283.675    0000 Phonetic SEGMENT     2237.5
## 32          3     u: 2211.175 2283.675    0000 Phonetic SEGMENT     2242.5
## 33          3     u: 2211.175 2283.675    0000 Phonetic SEGMENT     2247.5
## 34          3     u: 2211.175 2283.675    0000 Phonetic SEGMENT     2252.5
## 35          3     u: 2211.175 2283.675    0000 Phonetic SEGMENT     2257.5
## 36          3     u: 2211.175 2283.675    0000 Phonetic SEGMENT     2262.5
## 37          3     u: 2211.175 2283.675    0000 Phonetic SEGMENT     2267.5
## 38          3     u: 2211.175 2283.675    0000 Phonetic SEGMENT     2272.5
## 39          3     u: 2211.175 2283.675    0000 Phonetic SEGMENT     2277.5
## 40          3     u: 2211.175 2283.675    0000 Phonetic SEGMENT     2282.5
## 41          4     u:  736.975  798.475    0000 Phonetic SEGMENT      737.5
## 42          4     u:  736.975  798.475    0000 Phonetic SEGMENT      742.5
## 43          4     u:  736.975  798.475    0000 Phonetic SEGMENT      747.5
## 44          4     u:  736.975  798.475    0000 Phonetic SEGMENT      752.5
## 45          4     u:  736.975  798.475    0000 Phonetic SEGMENT      757.5
## 46          4     u:  736.975  798.475    0000 Phonetic SEGMENT      762.5
## 47          4     u:  736.975  798.475    0000 Phonetic SEGMENT      767.5
## 48          4     u:  736.975  798.475    0000 Phonetic SEGMENT      772.5
## 49          4     u:  736.975  798.475    0000 Phonetic SEGMENT      777.5
## 50          4     u:  736.975  798.475    0000 Phonetic SEGMENT      782.5
## 51          4     u:  736.975  798.475    0000 Phonetic SEGMENT      787.5
## 52          4     u:  736.975  798.475    0000 Phonetic SEGMENT      792.5
## 53          4     u:  736.975  798.475    0000 Phonetic SEGMENT      797.5
## 54          5     u: 1129.925 1222.325    0000 Phonetic SEGMENT     1132.5
## 55          5     u: 1129.925 1222.325    0000 Phonetic SEGMENT     1137.5
## 56          5     u: 1129.925 1222.325    0000 Phonetic SEGMENT     1142.5
## 57          5     u: 1129.925 1222.325    0000 Phonetic SEGMENT     1147.5
## 58          5     u: 1129.925 1222.325    0000 Phonetic SEGMENT     1152.5
## 59          5     u: 1129.925 1222.325    0000 Phonetic SEGMENT     1157.5
## 60          5     u: 1129.925 1222.325    0000 Phonetic SEGMENT     1162.5
## 61          5     u: 1129.925 1222.325    0000 Phonetic SEGMENT     1167.5
## 62          5     u: 1129.925 1222.325    0000 Phonetic SEGMENT     1172.5
## 63          5     u: 1129.925 1222.325    0000 Phonetic SEGMENT     1177.5
## 64          5     u: 1129.925 1222.325    0000 Phonetic SEGMENT     1182.5
## 65          5     u: 1129.925 1222.325    0000 Phonetic SEGMENT     1187.5
## 66          5     u: 1129.925 1222.325    0000 Phonetic SEGMENT     1192.5
## 67          5     u: 1129.925 1222.325    0000 Phonetic SEGMENT     1197.5
## 68          5     u: 1129.925 1222.325    0000 Phonetic SEGMENT     1202.5
## 69          5     u: 1129.925 1222.325    0000 Phonetic SEGMENT     1207.5
## 70          5     u: 1129.925 1222.325    0000 Phonetic SEGMENT     1212.5
## 71          5     u: 1129.925 1222.325    0000 Phonetic SEGMENT     1217.5
## 72          6      E 1436.725 1515.475    0000 Phonetic SEGMENT     1437.5
## 73          6      E 1436.725 1515.475    0000 Phonetic SEGMENT     1442.5
## 74          6      E 1436.725 1515.475    0000 Phonetic SEGMENT     1447.5
## 75          6      E 1436.725 1515.475    0000 Phonetic SEGMENT     1452.5
## 76          6      E 1436.725 1515.475    0000 Phonetic SEGMENT     1457.5
## 77          6      E 1436.725 1515.475    0000 Phonetic SEGMENT     1462.5
## 78          6      E 1436.725 1515.475    0000 Phonetic SEGMENT     1467.5
## 79          6      E 1436.725 1515.475    0000 Phonetic SEGMENT     1472.5
## 80          6      E 1436.725 1515.475    0000 Phonetic SEGMENT     1477.5
## 81          6      E 1436.725 1515.475    0000 Phonetic SEGMENT     1482.5
## 82          6      E 1436.725 1515.475    0000 Phonetic SEGMENT     1487.5
## 83          6      E 1436.725 1515.475    0000 Phonetic SEGMENT     1492.5
## 84          6      E 1436.725 1515.475    0000 Phonetic SEGMENT     1497.5
## 85          6      E 1436.725 1515.475    0000 Phonetic SEGMENT     1502.5
## 86          6      E 1436.725 1515.475    0000 Phonetic SEGMENT     1507.5
## 87          6      E 1436.725 1515.475    0000 Phonetic SEGMENT     1512.5
## 88          7     i: 1554.475 1628.475    0000 Phonetic SEGMENT     1557.5
## 89          7     i: 1554.475 1628.475    0000 Phonetic SEGMENT     1562.5
## 90          7     i: 1554.475 1628.475    0000 Phonetic SEGMENT     1567.5
## 91          7     i: 1554.475 1628.475    0000 Phonetic SEGMENT     1572.5
## 92          7     i: 1554.475 1628.475    0000 Phonetic SEGMENT     1577.5
## 93          7     i: 1554.475 1628.475    0000 Phonetic SEGMENT     1582.5
## 94          7     i: 1554.475 1628.475    0000 Phonetic SEGMENT     1587.5
## 95          7     i: 1554.475 1628.475    0000 Phonetic SEGMENT     1592.5
## 96          7     i: 1554.475 1628.475    0000 Phonetic SEGMENT     1597.5
## 97          7     i: 1554.475 1628.475    0000 Phonetic SEGMENT     1602.5
## 98          7     i: 1554.475 1628.475    0000 Phonetic SEGMENT     1607.5
## 99          7     i: 1554.475 1628.475    0000 Phonetic SEGMENT     1612.5
## 100         7     i: 1554.475 1628.475    0000 Phonetic SEGMENT     1617.5
## 101         7     i: 1554.475 1628.475    0000 Phonetic SEGMENT     1622.5
## 102         7     i: 1554.475 1628.475    0000 Phonetic SEGMENT     1627.5
## 103         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2572.5
## 104         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2577.5
## 105         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2582.5
## 106         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2587.5
## 107         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2592.5
## 108         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2597.5
## 109         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2602.5
## 110         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2607.5
## 111         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2612.5
## 112         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2617.5
## 113         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2622.5
## 114         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2627.5
## 115         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2632.5
## 116         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2637.5
## 117         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2642.5
## 118         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2647.5
## 119         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2652.5
## 120         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2657.5
## 121         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2662.5
## 122         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2667.5
## 123         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2672.5
## 124         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2677.5
## 125         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2682.5
## 126         8     i: 2569.225 2692.325    0000 Phonetic SEGMENT     2687.5
## 127         9     i:  350.225  425.375    0000 Phonetic SEGMENT      352.5
## 128         9     i:  350.225  425.375    0000 Phonetic SEGMENT      357.5
## 129         9     i:  350.225  425.375    0000 Phonetic SEGMENT      362.5
## 130         9     i:  350.225  425.375    0000 Phonetic SEGMENT      367.5
## 131         9     i:  350.225  425.375    0000 Phonetic SEGMENT      372.5
## 132         9     i:  350.225  425.375    0000 Phonetic SEGMENT      377.5
## 133         9     i:  350.225  425.375    0000 Phonetic SEGMENT      382.5
## 134         9     i:  350.225  425.375    0000 Phonetic SEGMENT      387.5
## 135         9     i:  350.225  425.375    0000 Phonetic SEGMENT      392.5
## 136         9     i:  350.225  425.375    0000 Phonetic SEGMENT      397.5
## 137         9     i:  350.225  425.375    0000 Phonetic SEGMENT      402.5
## 138         9     i:  350.225  425.375    0000 Phonetic SEGMENT      407.5
## 139         9     i:  350.225  425.375    0000 Phonetic SEGMENT      412.5
## 140         9     i:  350.225  425.375    0000 Phonetic SEGMENT      417.5
## 141         9     i:  350.225  425.375    0000 Phonetic SEGMENT      422.5
## 142        10      E  425.375  496.575    0000 Phonetic SEGMENT      427.5
## 143        10      E  425.375  496.575    0000 Phonetic SEGMENT      432.5
## 144        10      E  425.375  496.575    0000 Phonetic SEGMENT      437.5
## 145        10      E  425.375  496.575    0000 Phonetic SEGMENT      442.5
## 146        10      E  425.375  496.575    0000 Phonetic SEGMENT      447.5
## 147        10      E  425.375  496.575    0000 Phonetic SEGMENT      452.5
## 148        10      E  425.375  496.575    0000 Phonetic SEGMENT      457.5
## 149        10      E  425.375  496.575    0000 Phonetic SEGMENT      462.5
## 150        10      E  425.375  496.575    0000 Phonetic SEGMENT      467.5
## 151        10      E  425.375  496.575    0000 Phonetic SEGMENT      472.5
## 152        10      E  425.375  496.575    0000 Phonetic SEGMENT      477.5
## 153        10      E  425.375  496.575    0000 Phonetic SEGMENT      482.5
## 154        10      E  425.375  496.575    0000 Phonetic SEGMENT      487.5
## 155        10      E  425.375  496.575    0000 Phonetic SEGMENT      492.5
## 156        11      E 1500.675 1578.525    0000 Phonetic SEGMENT     1502.5
## 157        11      E 1500.675 1578.525    0000 Phonetic SEGMENT     1507.5
## 158        11      E 1500.675 1578.525    0000 Phonetic SEGMENT     1512.5
## 159        11      E 1500.675 1578.525    0000 Phonetic SEGMENT     1517.5
## 160        11      E 1500.675 1578.525    0000 Phonetic SEGMENT     1522.5
## 161        11      E 1500.675 1578.525    0000 Phonetic SEGMENT     1527.5
## 162        11      E 1500.675 1578.525    0000 Phonetic SEGMENT     1532.5
## 163        11      E 1500.675 1578.525    0000 Phonetic SEGMENT     1537.5
## 164        11      E 1500.675 1578.525    0000 Phonetic SEGMENT     1542.5
## 165        11      E 1500.675 1578.525    0000 Phonetic SEGMENT     1547.5
## 166        11      E 1500.675 1578.525    0000 Phonetic SEGMENT     1552.5
## 167        11      E 1500.675 1578.525    0000 Phonetic SEGMENT     1557.5
## 168        11      E 1500.675 1578.525    0000 Phonetic SEGMENT     1562.5
## 169        11      E 1500.675 1578.525    0000 Phonetic SEGMENT     1567.5
## 170        11      E 1500.675 1578.525    0000 Phonetic SEGMENT     1572.5
## 171        11      E 1500.675 1578.525    0000 Phonetic SEGMENT     1577.5
## 172        12     i: 2408.575 2502.175    0000 Phonetic SEGMENT     2412.5
## 173        12     i: 2408.575 2502.175    0000 Phonetic SEGMENT     2417.5
## 174        12     i: 2408.575 2502.175    0000 Phonetic SEGMENT     2422.5
## 175        12     i: 2408.575 2502.175    0000 Phonetic SEGMENT     2427.5
## 176        12     i: 2408.575 2502.175    0000 Phonetic SEGMENT     2432.5
## 177        12     i: 2408.575 2502.175    0000 Phonetic SEGMENT     2437.5
## 178        12     i: 2408.575 2502.175    0000 Phonetic SEGMENT     2442.5
## 179        12     i: 2408.575 2502.175    0000 Phonetic SEGMENT     2447.5
## 180        12     i: 2408.575 2502.175    0000 Phonetic SEGMENT     2452.5
## 181        12     i: 2408.575 2502.175    0000 Phonetic SEGMENT     2457.5
## 182        12     i: 2408.575 2502.175    0000 Phonetic SEGMENT     2462.5
## 183        12     i: 2408.575 2502.175    0000 Phonetic SEGMENT     2467.5
## 184        12     i: 2408.575 2502.175    0000 Phonetic SEGMENT     2472.5
## 185        12     i: 2408.575 2502.175    0000 Phonetic SEGMENT     2477.5
## 186        12     i: 2408.575 2502.175    0000 Phonetic SEGMENT     2482.5
## 187        12     i: 2408.575 2502.175    0000 Phonetic SEGMENT     2487.5
## 188        12     i: 2408.575 2502.175    0000 Phonetic SEGMENT     2492.5
## 189        12     i: 2408.575 2502.175    0000 Phonetic SEGMENT     2497.5
## 190        13     i: 2876.525 2958.075    0000 Phonetic SEGMENT     2877.5
## 191        13     i: 2876.525 2958.075    0000 Phonetic SEGMENT     2882.5
## 192        13     i: 2876.525 2958.075    0000 Phonetic SEGMENT     2887.5
## 193        13     i: 2876.525 2958.075    0000 Phonetic SEGMENT     2892.5
## 194        13     i: 2876.525 2958.075    0000 Phonetic SEGMENT     2897.5
## 195        13     i: 2876.525 2958.075    0000 Phonetic SEGMENT     2902.5
## 196        13     i: 2876.525 2958.075    0000 Phonetic SEGMENT     2907.5
## 197        13     i: 2876.525 2958.075    0000 Phonetic SEGMENT     2912.5
## 198        13     i: 2876.525 2958.075    0000 Phonetic SEGMENT     2917.5
## 199        13     i: 2876.525 2958.075    0000 Phonetic SEGMENT     2922.5
## 200        13     i: 2876.525 2958.075    0000 Phonetic SEGMENT     2927.5
## 201        13     i: 2876.525 2958.075    0000 Phonetic SEGMENT     2932.5
## 202        13     i: 2876.525 2958.075    0000 Phonetic SEGMENT     2937.5
## 203        13     i: 2876.525 2958.075    0000 Phonetic SEGMENT     2942.5
## 204        13     i: 2876.525 2958.075    0000 Phonetic SEGMENT     2947.5
## 205        13     i: 2876.525 2958.075    0000 Phonetic SEGMENT     2952.5
## 206        13     i: 2876.525 2958.075    0000 Phonetic SEGMENT     2957.5
## 207        14      E 1521.175 1587.175    0000 Phonetic SEGMENT     1522.5
## 208        14      E 1521.175 1587.175    0000 Phonetic SEGMENT     1527.5
## 209        14      E 1521.175 1587.175    0000 Phonetic SEGMENT     1532.5
## 210        14      E 1521.175 1587.175    0000 Phonetic SEGMENT     1537.5
## 211        14      E 1521.175 1587.175    0000 Phonetic SEGMENT     1542.5
## 212        14      E 1521.175 1587.175    0000 Phonetic SEGMENT     1547.5
## 213        14      E 1521.175 1587.175    0000 Phonetic SEGMENT     1552.5
## 214        14      E 1521.175 1587.175    0000 Phonetic SEGMENT     1557.5
## 215        14      E 1521.175 1587.175    0000 Phonetic SEGMENT     1562.5
## 216        14      E 1521.175 1587.175    0000 Phonetic SEGMENT     1567.5
## 217        14      E 1521.175 1587.175    0000 Phonetic SEGMENT     1572.5
## 218        14      E 1521.175 1587.175    0000 Phonetic SEGMENT     1577.5
## 219        14      E 1521.175 1587.175    0000 Phonetic SEGMENT     1582.5
## 220        15      E  595.525  708.925    0000 Phonetic SEGMENT      597.5
## 221        15      E  595.525  708.925    0000 Phonetic SEGMENT      602.5
## 222        15      E  595.525  708.925    0000 Phonetic SEGMENT      607.5
## 223        15      E  595.525  708.925    0000 Phonetic SEGMENT      612.5
## 224        15      E  595.525  708.925    0000 Phonetic SEGMENT      617.5
## 225        15      E  595.525  708.925    0000 Phonetic SEGMENT      622.5
## 226        15      E  595.525  708.925    0000 Phonetic SEGMENT      627.5
## 227        15      E  595.525  708.925    0000 Phonetic SEGMENT      632.5
## 228        15      E  595.525  708.925    0000 Phonetic SEGMENT      637.5
## 229        15      E  595.525  708.925    0000 Phonetic SEGMENT      642.5
## 230        15      E  595.525  708.925    0000 Phonetic SEGMENT      647.5
## 231        15      E  595.525  708.925    0000 Phonetic SEGMENT      652.5
## 232        15      E  595.525  708.925    0000 Phonetic SEGMENT      657.5
## 233        15      E  595.525  708.925    0000 Phonetic SEGMENT      662.5
## 234        15      E  595.525  708.925    0000 Phonetic SEGMENT      667.5
## 235        15      E  595.525  708.925    0000 Phonetic SEGMENT      672.5
## 236        15      E  595.525  708.925    0000 Phonetic SEGMENT      677.5
## 237        15      E  595.525  708.925    0000 Phonetic SEGMENT      682.5
## 238        15      E  595.525  708.925    0000 Phonetic SEGMENT      687.5
## 239        15      E  595.525  708.925    0000 Phonetic SEGMENT      692.5
## 240        15      E  595.525  708.925    0000 Phonetic SEGMENT      697.5
## 241        15      E  595.525  708.925    0000 Phonetic SEGMENT      702.5
## 242        15      E  595.525  708.925    0000 Phonetic SEGMENT      707.5
## 243        16      E 1132.525 1230.925    0000 Phonetic SEGMENT     1137.5
## 244        16      E 1132.525 1230.925    0000 Phonetic SEGMENT     1142.5
## 245        16      E 1132.525 1230.925    0000 Phonetic SEGMENT     1147.5
## 246        16      E 1132.525 1230.925    0000 Phonetic SEGMENT     1152.5
## 247        16      E 1132.525 1230.925    0000 Phonetic SEGMENT     1157.5
## 248        16      E 1132.525 1230.925    0000 Phonetic SEGMENT     1162.5
## 249        16      E 1132.525 1230.925    0000 Phonetic SEGMENT     1167.5
## 250        16      E 1132.525 1230.925    0000 Phonetic SEGMENT     1172.5
## 251        16      E 1132.525 1230.925    0000 Phonetic SEGMENT     1177.5
## 252        16      E 1132.525 1230.925    0000 Phonetic SEGMENT     1182.5
## 253        16      E 1132.525 1230.925    0000 Phonetic SEGMENT     1187.5
## 254        16      E 1132.525 1230.925    0000 Phonetic SEGMENT     1192.5
## 255        16      E 1132.525 1230.925    0000 Phonetic SEGMENT     1197.5
## 256        16      E 1132.525 1230.925    0000 Phonetic SEGMENT     1202.5
## 257        16      E 1132.525 1230.925    0000 Phonetic SEGMENT     1207.5
## 258        16      E 1132.525 1230.925    0000 Phonetic SEGMENT     1212.5
## 259        16      E 1132.525 1230.925    0000 Phonetic SEGMENT     1217.5
## 260        16      E 1132.525 1230.925    0000 Phonetic SEGMENT     1222.5
## 261        16      E 1132.525 1230.925    0000 Phonetic SEGMENT     1227.5
## 262        17     u:  585.675  666.675    0000 Phonetic SEGMENT      587.5
## 263        17     u:  585.675  666.675    0000 Phonetic SEGMENT      592.5
## 264        17     u:  585.675  666.675    0000 Phonetic SEGMENT      597.5
## 265        17     u:  585.675  666.675    0000 Phonetic SEGMENT      602.5
## 266        17     u:  585.675  666.675    0000 Phonetic SEGMENT      607.5
## 267        17     u:  585.675  666.675    0000 Phonetic SEGMENT      612.5
## 268        17     u:  585.675  666.675    0000 Phonetic SEGMENT      617.5
## 269        17     u:  585.675  666.675    0000 Phonetic SEGMENT      622.5
## 270        17     u:  585.675  666.675    0000 Phonetic SEGMENT      627.5
## 271        17     u:  585.675  666.675    0000 Phonetic SEGMENT      632.5
## 272        17     u:  585.675  666.675    0000 Phonetic SEGMENT      637.5
## 273        17     u:  585.675  666.675    0000 Phonetic SEGMENT      642.5
## 274        17     u:  585.675  666.675    0000 Phonetic SEGMENT      647.5
## 275        17     u:  585.675  666.675    0000 Phonetic SEGMENT      652.5
## 276        17     u:  585.675  666.675    0000 Phonetic SEGMENT      657.5
## 277        17     u:  585.675  666.675    0000 Phonetic SEGMENT      662.5
## 278        18      E 2480.425 2587.675    0000 Phonetic SEGMENT     2482.5
## 279        18      E 2480.425 2587.675    0000 Phonetic SEGMENT     2487.5
## 280        18      E 2480.425 2587.675    0000 Phonetic SEGMENT     2492.5
## 281        18      E 2480.425 2587.675    0000 Phonetic SEGMENT     2497.5
## 282        18      E 2480.425 2587.675    0000 Phonetic SEGMENT     2502.5
## 283        18      E 2480.425 2587.675    0000 Phonetic SEGMENT     2507.5
## 284        18      E 2480.425 2587.675    0000 Phonetic SEGMENT     2512.5
## 285        18      E 2480.425 2587.675    0000 Phonetic SEGMENT     2517.5
## 286        18      E 2480.425 2587.675    0000 Phonetic SEGMENT     2522.5
## 287        18      E 2480.425 2587.675    0000 Phonetic SEGMENT     2527.5
## 288        18      E 2480.425 2587.675    0000 Phonetic SEGMENT     2532.5
## 289        18      E 2480.425 2587.675    0000 Phonetic SEGMENT     2537.5
## 290        18      E 2480.425 2587.675    0000 Phonetic SEGMENT     2542.5
## 291        18      E 2480.425 2587.675    0000 Phonetic SEGMENT     2547.5
## 292        18      E 2480.425 2587.675    0000 Phonetic SEGMENT     2552.5
## 293        18      E 2480.425 2587.675    0000 Phonetic SEGMENT     2557.5
## 294        18      E 2480.425 2587.675    0000 Phonetic SEGMENT     2562.5
## 295        18      E 2480.425 2587.675    0000 Phonetic SEGMENT     2567.5
## 296        18      E 2480.425 2587.675    0000 Phonetic SEGMENT     2572.5
## 297        18      E 2480.425 2587.675    0000 Phonetic SEGMENT     2577.5
## 298        18      E 2480.425 2587.675    0000 Phonetic SEGMENT     2582.5
## 299        18      E 2480.425 2587.675    0000 Phonetic SEGMENT     2587.5
##     times_rel times_norm  T1   T2   T3   T4
## 1           0 0.00000000 422 1613 2118 2750
## 2           5 0.06666667 434 1651 2195 2824
## 3          10 0.13333333 447 1686 2229 3536
## 4          15 0.20000000 449 1703 2245 3536
## 5          20 0.26666667 445 1712 2275 3224
## 6          25 0.33333333 439 1730 2309 3299
## 7          30 0.40000000 424 1747 2340 3325
## 8          35 0.46666667 412 1765 2370 3340
## 9          40 0.53333333 423 1812 2409 3359
## 10         45 0.60000000 433 1845 2423 3367
## 11         50 0.66666667 415 1844 2432 3366
## 12         55 0.73333333 410 1846 2437 3400
## 13         60 0.80000000 408 1808 2436 3453
## 14         65 0.86666667 421 1793 2443 3458
## 15         70 0.93333333 427 1752 2463 3484
## 16         75 1.00000000 417 1728 2471 3511
## 17          0 0.00000000 297 2041 2470 3383
## 18          5 0.12500000 290 2034 2409 3380
## 19         10 0.25000000 288 2044 2277 3380
## 20         15 0.37500000 288 1998 2143 3347
## 21         20 0.50000000 287 1727 2138 3315
## 22         25 0.62500000 287 1633 2119 3338
## 23         30 0.75000000 295 1600 2114 3361
## 24         35 0.87500000 292 1478 2112 3383
## 25         40 1.00000000 290 1339 2071 3372
## 26          0 0.00000000 306 1985 2454 3361
## 27          5 0.07142857 310 1954 2421 3354
## 28         10 0.14285714 314 1901 2394 3385
## 29         15 0.21428571 319 1850 2374 3441
## 30         20 0.28571429 324 1783 2354 3400
## 31         25 0.35714286 330 1730 2335 3378
## 32         30 0.42857143 334 1693 2317 3402
## 33         35 0.50000000 341 1680 2305 3419
## 34         40 0.57142857 349 1663 2296 3423
## 35         45 0.64285714 355 1642 2292 3418
## 36         50 0.71428571 356 1596 2287 3394
## 37         55 0.78571429 352 1567 2265 3362
## 38         60 0.85714286 342 1535 2245 3314
## 39         65 0.92857143 334 1510 2245 3310
## 40         70 1.00000000 303 1471 2260 3515
## 41          0 0.00000000 305 1945 2367 3361
## 42          5 0.08333333 314 1907 2360 3369
## 43         10 0.16666667 325 1879 2360 3366
## 44         15 0.25000000 333 1864 2334 3367
## 45         20 0.33333333 339 1835 2298 3378
## 46         25 0.41666667 341 1780 2281 3376
## 47         30 0.50000000 342 1739 2273 3377
## 48         35 0.58333333 342 1723 2275 3405
## 49         40 0.66666667 341 1716 2280 3403
## 50         45 0.75000000 338 1700 2278 3383
## 51         50 0.83333333 332 1657 2268 3385
## 52         55 0.91666667 323 1576 2263 3386
## 53         60 1.00000000 308 1532 2293 3373
## 54          0 0.00000000 196 1227 2254 3507
## 55          5 0.05882353 207 1255 2232 3509
## 56         10 0.11764706 215 1258 2275 3456
## 57         15 0.17647059 232 1248 2282 3330
## 58         20 0.23529412 245 1222 2262 3301
## 59         25 0.29411765 259 1176 2220 3272
## 60         30 0.35294118 271 1118 2151 3250
## 61         35 0.41176471 275  999 2027 3240
## 62         40 0.47058824 278  950 1982 3249
## 63         45 0.52941176 285  923 1981 3273
## 64         50 0.58823529 290  901 1958 3307
## 65         55 0.64705882 297  892 1948 3382
## 66         60 0.70588235 297  861 1959 3376
## 67         65 0.76470588 305  852 1972 3301
## 68         70 0.82352941 316  835 1969 3271
## 69         75 0.88235294 332  826 1978 3270
## 70         80 0.94117647 352  827 2000 3255
## 71         85 1.00000000 370  831 2018 3252
## 72          0 0.00000000 330 1120 1596 2851
## 73          5 0.06666667 333 1147 1597 2887
## 74         10 0.13333333 333 1173 1622 2909
## 75         15 0.20000000 330 1200 1665 2955
## 76         20 0.26666667 335 1229 1698 2979
## 77         25 0.33333333 340 1257 1738 2977
## 78         30 0.40000000 352 1295 1786 2979
## 79         35 0.46666667 352 1335 1830 2977
## 80         40 0.53333333 348 1361 1854 2953
## 81         45 0.60000000 342 1407 1880 2912
## 82         50 0.66666667 336 1456 1920 2864
## 83         55 0.73333333 327 1493 1960 2831
## 84         60 0.80000000 314 1531 1995 2779
## 85         65 0.86666667 308 1605 2030 2735
## 86         70 0.93333333 286 1661 2060 2633
## 87         75 1.00000000 263 1625 1983 2541
## 88          0 0.00000000 284 1927 2580 3540
## 89          5 0.07142857 285 2043 2652 3536
## 90         10 0.14285714 288 2185 2683 3546
## 91         15 0.21428571 284 2255 2712 3533
## 92         20 0.28571429 289 2270 2744 3542
## 93         25 0.35714286 294 2286 2749 3551
## 94         30 0.42857143 300 2285 2734 3556
## 95         35 0.50000000 298 2273 2708 3562
## 96         40 0.57142857 293 2266 2698 3573
## 97         45 0.64285714 266 2244 2654 3568
## 98         50 0.71428571 251 2213 2607 3567
## 99         55 0.78571429 239 2162 2540 3453
## 100        60 0.85714286 227 2121 2510 3623
## 101        65 0.92857143 205 2016 2468 3557
## 102        70 1.00000000 199 1910 2416 3460
## 103         0 0.00000000 339 1307 2312 3685
## 104         5 0.04347826 341 1367 2288 3656
## 105        10 0.08695652 347 1425 2299 3655
## 106        15 0.13043478 344 1454 2293 3676
## 107        20 0.17391304 339 1515 2295 3679
## 108        25 0.21739130 364 1555 2300 3665
## 109        30 0.26086957 349 1673 2320 3618
## 110        35 0.30434783 350 1754 2337 3602
## 111        40 0.34782609 334 1773 2346 3601
## 112        45 0.39130435 311 1825 2383 3508
## 113        50 0.43478261 338 1846 2387 3462
## 114        55 0.47826087 329 1878 2416 3471
## 115        60 0.52173913 321 1927 2443 3468
## 116        65 0.56521739 295 1935 2466 3452
## 117        70 0.60869565 261 1950 2478 3409
## 118        75 0.65217391 317 1968 2443 3389
## 119        80 0.69565217 296 1984 2452 3400
## 120        85 0.73913043 239 2031 2441 3443
## 121        90 0.78260870 213 2107 2368 3539
## 122        95 0.82608696 195 2154 2330 3565
## 123       100 0.86956522 187 2069 2409 3465
## 124       105 0.91304348 162 2049 2404 3338
## 125       110 0.95652174   0 2053 2386 3323
## 126       115 1.00000000   0 2030 2411 3357
## 127         0 0.00000000 153 2362 2793 3511
## 128         5 0.07142857 184 2249 2858 3539
## 129        10 0.14285714 240 2182 2804 3538
## 130        15 0.21428571 266 2180 2788 3494
## 131        20 0.28571429 272 2176 2777 3422
## 132        25 0.35714286 263 2210 2800 3462
## 133        30 0.42857143 253 2262 2848 3525
## 134        35 0.50000000 245 2265 2882 3493
## 135        40 0.57142857 246 2254 2870 3518
## 136        45 0.64285714 249 2251 2848 3506
## 137        50 0.71428571 252 2252 2828 3506
## 138        55 0.78571429 257 2254 2804 3501
## 139        60 0.85714286 266 2252 2795 3466
## 140        65 0.92857143 279 2226 2761 3453
## 141        70 1.00000000 291 2161 2686 3453
## 142         0 0.00000000 313 2114 2647 3462
## 143         5 0.07692308 338 2068 2615 3468
## 144        10 0.15384615 368 2035 2589 3447
## 145        15 0.23076923 392 1998 2556 3403
## 146        20 0.30769231 412 1958 2507 3354
## 147        25 0.38461538 419 1921 2466 3317
## 148        30 0.46153846 420 1883 2432 3304
## 149        35 0.53846154 424 1841 2408 3304
## 150        40 0.61538462 425 1795 2387 3305
## 151        45 0.69230769 423 1740 2363 3284
## 152        50 0.76923077 419 1682 2347 3260
## 153        55 0.84615385 419 1594 2324 3253
## 154        60 0.92307692 412 1515 2275 3241
## 155        65 1.00000000 388 1426 2224 3199
## 156         0 0.00000000 419 1567 2049 2801
## 157         5 0.06666667 443 1611 2095 2859
## 158        10 0.13333333 456 1668 2136 2926
## 159        15 0.20000000 458 1737 2183 3004
## 160        20 0.26666667 473 1792 2216 3049
## 161        25 0.33333333 467 1836 2239 3122
## 162        30 0.40000000 416 1861 2251 3251
## 163        35 0.46666667 374 1884 2260 3292
## 164        40 0.53333333 367 1915 2269 3234
## 165        45 0.60000000 377 1942 2283 3214
## 166        50 0.66666667 378 1978 2284 3223
## 167        55 0.73333333 354 1995 2233 3321
## 168        60 0.80000000 371 1963 2216 3390
## 169        65 0.86666667 402 1923 2224 3402
## 170        70 0.93333333 416 1859 2240 3747
## 171        75 1.00000000 417 1925 2257 3755
## 172         0 0.00000000 289 1517 2438 3572
## 173         5 0.05882353 305 1550 2452 3577
## 174        10 0.11764706 317 1605 2453 3549
## 175        15 0.17647059 326 1677 2449 3525
## 176        20 0.23529412 327 1741 2455 3518
## 177        25 0.29411765 323 1785 2466 3512
## 178        30 0.35294118 321 1834 2482 3529
## 179        35 0.41176471 321 1863 2476 3548
## 180        40 0.47058824 320 1894 2468 3455
## 181        45 0.52941176 320 1925 2462 3457
## 182        50 0.58823529 319 1940 2451 3582
## 183        55 0.64705882 316 1946 2441 3583
## 184        60 0.70588235 308 1943 2425 3611
## 185        65 0.76470588 298 1921 2403 3623
## 186        70 0.82352941 296 1888 2368 3620
## 187        75 0.88235294 300 1822 2334 3615
## 188        80 0.94117647 305 1692 2337 3601
## 189        85 1.00000000 311 1586 2354 3591
## 190         0 0.00000000 284  970 2245 3398
## 191         5 0.06250000 302 1077 2252 3380
## 192        10 0.12500000 320 1227 2267 3363
## 193        15 0.18750000 328 1314 2272 3372
## 194        20 0.25000000 335 1403 2279 3388
## 195        25 0.31250000 335 1473 2289 3410
## 196        30 0.37500000 331 1553 2292 3448
## 197        35 0.43750000 328 1725 2308 3424
## 198        40 0.50000000 320 1835 2321 3398
## 199        45 0.56250000 304 1914 2328 3477
## 200        50 0.62500000 286 2012 2344 3489
## 201        55 0.68750000 269 2078 2369 3520
## 202        60 0.75000000 251 2103 2372 3530
## 203        65 0.81250000 230 2140 2357 3540
## 204        70 0.87500000 194 2197 2340 3543
## 205        75 0.93750000 174 2197 2331 3512
## 206        80 1.00000000 176 2115 2331 3505
## 207         0 0.00000000 211 1747 2472 3661
## 208         5 0.08333333 286 1773 2468 3591
## 209        10 0.16666667 371 1776 2463 3411
## 210        15 0.25000000 381 1794 2474 3356
## 211        20 0.33333333 341 1796 2476 3352
## 212        25 0.41666667 345 1795 2473 3341
## 213        30 0.50000000 359 1794 2486 3336
## 214        35 0.58333333 364 1786 2492 3340
## 215        40 0.66666667 378 1783 2481 3331
## 216        45 0.75000000 388 1785 2463 3278
## 217        50 0.83333333 366 1758 2388 3222
## 218        55 0.91666667 356 1665 2239 3064
## 219        60 1.00000000 331 1567 2179 2979
## 220         0 0.00000000 120 1708 2457 3470
## 221         5 0.04545455 133 1696 2466 3552
## 222        10 0.09090909 140 1761 2484 3556
## 223        15 0.13636364 148 2025 2486 3616
## 224        20 0.18181818 159 2113 2466 3630
## 225        25 0.22727273 181 2068 2431 3604
## 226        30 0.27272727 269 1942 2454 3574
## 227        35 0.31818182 270 1931 2451 3559
## 228        40 0.36363636 272 1920 2451 3583
## 229        45 0.40909091 301 1881 2412 3566
## 230        50 0.45454545 333 1870 2389 3554
## 231        55 0.50000000 360 1874 2397 3543
## 232        60 0.54545455 378 1865 2403 3542
## 233        65 0.59090909 387 1852 2398 3524
## 234        70 0.63636364 398 1848 2383 3508
## 235        75 0.68181818 409 1844 2370 3519
## 236        80 0.72727273 406 1851 2407 3510
## 237        85 0.77272727 398 1865 2456 3517
## 238        90 0.81818182 385 1877 2490 3528
## 239        95 0.86363636 363 1882 2499 3523
## 240       100 0.90909091 326 1877 2491 3517
## 241       105 0.95454545 296 1854 2477 3516
## 242       110 1.00000000 279 1830 2470 3528
## 243         0 0.00000000 339 1578 2210 3426
## 244         5 0.05555556 385 1633 2227 3439
## 245        10 0.11111111 406 1672 2265 3460
## 246        15 0.16666667 423 1713 2294 3474
## 247        20 0.22222222 432 1742 2316 3485
## 248        25 0.27777778 438 1757 2347 3499
## 249        30 0.33333333 443 1758 2364 3507
## 250        35 0.38888889 454 1757 2367 3511
## 251        40 0.44444444 463 1754 2369 3525
## 252        45 0.50000000 470 1750 2375 3539
## 253        50 0.55555556 473 1745 2386 3554
## 254        55 0.61111111 476 1744 2391 3581
## 255        60 0.66666667 477 1739 2385 3613
## 256        65 0.72222222 466 1712 2385 3654
## 257        70 0.77777778 447 1684 2390 3685
## 258        75 0.83333333 433 1667 2397 3717
## 259        80 0.88888889 418 1651 2395 3755
## 260        85 0.94444444 396 1624 2384 3748
## 261        90 1.00000000 355 1598 2364 3754
## 262         0 0.00000000 287 2049 2580 3440
## 263         5 0.06666667 289 2059 2556 3427
## 264        10 0.13333333 291 2058 2517 3421
## 265        15 0.20000000 293 2034 2470 3421
## 266        20 0.26666667 295 2011 2446 3426
## 267        25 0.33333333 297 1998 2434 3429
## 268        30 0.40000000 298 1988 2411 3421
## 269        35 0.46666667 298 1955 2341 3432
## 270        40 0.53333333 298 1868 2293 3144
## 271        45 0.60000000 297 1817 2291 3203
## 272        50 0.66666667 296 1793 2302 3414
## 273        55 0.73333333 295 1775 2320 3296
## 274        60 0.80000000 292 1752 2332 3275
## 275        65 0.86666667 289 1727 2335 3200
## 276        70 0.93333333 284 1701 2338 3118
## 277        75 1.00000000 281 1671 2342 3029
## 278         0 0.00000000 291 1485 2396 3586
## 279         5 0.04761905 353 1547 2428 3574
## 280        10 0.09523810 405 1577 2434 3584
## 281        15 0.14285714 423 1605 2440 3588
## 282        20 0.19047619 434 1629 2445 3585
## 283        25 0.23809524 444 1660 2452 3585
## 284        30 0.28571429 449 1688 2459 3583
## 285        35 0.33333333 456 1704 2462 3579
## 286        40 0.38095238 462 1713 2466 3560
## 287        45 0.42857143 467 1717 2465 3312
## 288        50 0.47619048 461 1718 2461 3299
## 289        55 0.52380952 456 1714 2456 3278
## 290        60 0.57142857 455 1705 2447 3263
## 291        65 0.61904762 459 1693 2435 3244
## 292        70 0.66666667 463 1674 2418 3213
## 293        75 0.71428571 462 1648 2398 3250
## 294        80 0.76190476 452 1611 2377 3326
## 295        85 0.80952381 440 1564 2345 3275
## 296        90 0.85714286 428 1515 2308 3217
## 297        95 0.90476190 400 1470 2286 3203
## 298       100 0.95238095 348 1422 2260 3214
## 299       105 1.00000000 278 1376 2232 3274
## 
## NOTE: to reduce the verboseness of the output not all colums of an emuRtrackdata object are printed. Use print.data.frame() to print all columns.
names(vowels_fm_new)
##  [1] "sl_rowIdx"          "labels"             "start"             
##  [4] "end"                "utts"               "db_uuid"           
##  [7] "session"            "bundle"             "start_item_id"     
## [10] "end_item_id"        "level"              "start_item_seq_idx"
## [13] "end_item_seq_idx"   "type"               "sample_start"      
## [16] "sample_end"         "sample_rate"        "times_orig"        
## [19] "times_rel"          "times_norm"         "T1"                
## [22] "T2"                 "T3"                 "T4"

The emuRtrackdata object is an amalgamation of both a segment list and a trackdata object. The first sl_rowIdx column of the iVu object indicates the row index of the segment list the current row belongs to, the times_rel and times_orig (and times_norm in the forthcoming emuR-version) columns represent the relative time and the original time of the samples contained in the current row and T1 (to Tn in n dimensional trackdata) contains the actual signal sample values. It is also worth noting that the emuR package provides a function called create emuRtrackdata(), which allows users to create emuRtrackdata from a segment list and a trackdata object. This is beneficial as it allows trackdata objects to be processed using functions provided by the emuR package (e.g., dcut() and trapply()) and then converts them into a standardized data.table object for further processing (e.g., using R packages such as lme4 or ggplot2 which were implemented to use with data.frame or data.table objects).

Introduction to the package ggplot2

The goal of this chapter is to allow the reader to plot any numeric data from data.frames, whatever their source may be, including the new emuRtrackdata object. In order to do so, we sometimes have to manipulate the data.frame. We therefore will repeat some standard methods that manipulate data.frames.

The plots above can be done with ggplot2 and will look like:

Figure 1: Equivalent to the eplot

Figure 1: Equivalent to the eplot

Figure 2: Equivalent to the dplot

Figure 2: Equivalent to the dplot

Figure 3: Equivalent to the normalized dplot

Figure 3: Equivalent to the normalized dplot

Why ggplot2?

Advantages of ggplot2

  • consistent underlying grammar of graphics (Wilkinson, 2005)
  • plot specification at a high level of abstraction
  • very flexible
  • theme system for polishing plot appearance
  • mature and complete graphics system
  • many users, active mailing list

That said, there are some things you cannot (or should not) do With ggplot2:

  • 3-dimensional graphics (see the rgl package)
  • Graph-theory type graphs (nodes/edges layout; see the igraph package)
  • Interactive graphics (see the ggvis package)

What Is The Grammar Of Graphics?

The basic idea: independently specify plot building blocks and combine them to create just about any kind of graphical display you want. Building blocks of a graph include:

  • data
  • aesthetic mapping
  • geometric object
  • statistical transformations
  • scales
  • coordinate system
  • position adjustments
  • faceting

The structure of a ggplot

The ggplot() function is used to initialize the basic graph structure, then we add to it. The structure of a ggplot looks like this:

  ggplot(data = <default data set>, 
         aes(x = <default x axis variable>,
             y = <default y axis variable>,
             ... <other default aesthetic mappings>),
         ... <other plot defaults>) +

         geom_<geom type>(aes(size = <size variable for this geom>, 
                        ... <other aesthetic mappings>),
                    data = <data for this point geom>,
                    stat = <statistic string or function>,
                    position = <position string or function>,
                    color = <"fixed color specification">,
                    <other arguments, possibly passed to the _stat_ function) +

    scale_<aesthetic>_<type>(name = <"scale label">,
                       breaks = <where to put tick marks>,
                       labels = <labels for tick marks>,
                       ... <other options for the scale>) +

    theme(plot.background = element_rect(fill = "gray"),
          ... <other theme elements>)

The basic idea is that you specify different parts of the plot, and add them together using the + operator.

Examples

See e.g. Handbook on R and figures: http://www.cookbook-r.com/ and the introduction to ggplot2 (gg = grammar of graphics) in http://docs.ggplot2.org/current/

Let’s try with a few datasets from Jonathan Harrington’s statistics seminar:

# if necessary, install.packages(ggplot2)
library(ggplot2)

pfadu = "http://www.phonetik.uni-muenchen.de/~jmh/lehre/Rdf"
asp = read.table(file.path(pfadu, "asp.txt"))
coronal = read.table(file.path(pfadu, "coronal.txt"))
int.df = read.table(file.path(pfadu, "intdauer.txt"))
v.df = read.table(file.path(pfadu, "vdata.txt"))


# check class (data.frame or not):
class(asp)
## [1] "data.frame"
# the first few lines:
head(coronal)
##   Fr Region Vpn Socialclass
## 1 sh     R2  S1           W
## 2  s     R2  S2           W
## 3 sh     R1  S3           W
## 4  s     R3  S4           W
## 5  s     R2  S5           W
## 6 sh     R3  S6           W
# 'ai[m,]' = row m
# 'ai[,m]' = column m
# You can use '$Name' to access column "Name"

#############################################################################
# 1. Numerical und categorical variables
############################################################################
# In a data.frame, columns can consist of numerical or categorical variables.
# In a matrix, you can only have one or the other class of variables.

# Numerical variables: continuous
#
class(asp$d)
## [1] "numeric"
# or
with(asp, class(d))
## [1] "numeric"
# [1] "numeric"

class(int.df$Dauer)
## [1] "integer"
# [1] "integer"

# Categorical variables will be treated as factors (that have two or more levels, or categories; this is different to objects of the class "character"):
class(coronal$Socialclass)
## [1] "factor"
# [1] "factor"

# first 10
coronal$Socialclass[1:10]
##  [1] W W W W W W W W W W
## Levels: LM UM W
# asks which levels are given
levels(coronal$Socialclass)
## [1] "LM" "UM" "W"
##########################################################
# 2. Typical example in phonetics
##########################################################
# Is there an influence of x on y?
# 
# 1. y = numerical, x = categorical
# 1.1 difference in duration in /i, e, a/ ?
# 1.2 = influence of x (=vowel) on y (=duration)?
# 1.3 possible geoms: geom_boxplot() 
# or: geom_histogram()  or stat_density()

# 2. y = categorical, x = categorical
# 2.1 words like Sohn, Sonne... can be produced either with /s/ or /z/.
# /s/ more likely in Bavaria or in Hamburg?
# 2.2 possible geom: geom_barchart()

# 3. y = numerical, x = numerical 
# 3.1 bigger mouth opening related to a longer duration?
# 3.2 possible geom: geom_point(), geom_line()

Summary:

  • y = numerical, x = categorical: geom_boxplot(), geom_histogram(), stat_density()
  • y = categorical, x = categorical: geom_bar()
  • y = numerical, x = numerical: geom_point(), geom_line()
  • y = categorical, x = numerical: geom_point()

Boxplots

############################################################################
# 3. geom_boxplot(): y = numerical, x = categorical
############################################################################

head(asp)
##        d             Wort Vpn Kons Bet
## 1 26.180 Fruehlingswetter k01    t  un
## 2 23.063          Gestern k01    t  un
## 3 26.812           Montag k01    t  un
## 4 14.750            Vater k01    t  un
## 5 42.380            Tisch k01    t  be
## 6 21.560           Mutter k01    t  un
# Influence of place of articulation (Kons) on duration of aspiration (d)?
# y: d    (numerical)
# x: Kons (categorical)

# Syntax in ggplot()
# A + B + C + D + ...
# A, B, C... are modules.
# Here:
# A. data-frame + B. Variables + C. kind of plot
ggplot(asp) + aes(y = d, x = Kons) + geom_boxplot()

# or
# A
p1 = ggplot(asp)
# B
p2 = aes(y = d, x = Kons)
# C
p3 = geom_boxplot()
# A + B + C
p1 + p2 + p3

# oder A + B + C ablegen
erg = p1 + p2 + p3
# Bild
erg

# boxplot.
# thick line = median;  'Box': interquartile range
# 

Barplots

############################################################################
# 4. geom_bar(): y ist kategorial, x ist kategorial
############################################################################
head(coronal)
##   Fr Region Vpn Socialclass
## 1 sh     R2  S1           W
## 2  s     R2  S2           W
## 3 sh     R1  S3           W
## 4  s     R3  S4           W
## 5  s     R2  S5           W
## 6 sh     R3  S6           W
# Influence of region (Region) in place of articulation (F1)?
# y: Fr (categorical)
# x: Region (categorical)

p1 = ggplot(coronal)
p2 = aes(fill = Fr, x = Region)
# to print frequencies of occurance
p3 = geom_bar()
p1 + p2 + p3

# place bars side by side
p4 = geom_bar(position="dodge")
p1 + p2 + p4

# print proportions
p5 = geom_bar(position="fill")
p1 + p2 + p5

Scatterplots

############################################################################
# 5. geom_point(), geom_line():  y ist numerisch, x ist numerisch
############################################################################  
# Inwiefern wird die Dauer (Dauer) von der Intensität (dB) beeinflusst in dem Data-Frame int.df()
# y: Dauer (numerisch) 
# x: dB (numerisch)
head(int.df)
##   Vpn    dB Dauer
## 1  S1 24.50   162
## 2  S2 32.54   120
## 3  S2 38.02   223
## 4  S2 28.38   131
## 5  S1 23.47    67
## 6  S2 37.82   169
# Nur Linie
ggplot(int.df) +  aes(x = dB, y = Dauer) + geom_line() 

# Nur Punkte
ggplot(int.df, aes(x = dB, y = Dauer)) + geom_point() 

# Beide
ggplot(int.df, aes(x = dB, y = Dauer)) + geom_line() + geom_point()

Titles and axes

############################################################################
# 6. + xlab() + ylab() + ggtitle()
############################################################################  
# same boxplot as above
p1 = ggplot(asp) + aes(y = d, x = Kons) + geom_boxplot()
# label for x-axis
p2 = xlab("Place of Articulation")
# label for x-axis
p3 = ylab("Duration (ms)")
# Titel
p4 = ggtitle("Boxplot")
p1 + p2 + p3 + p4

# same barchart as above
bar.p = ggplot(coronal) + aes(x = Region, fill = Fr) + geom_bar(position = "fill")
x.p = xlab("Region")
y.p = ylab("Proportion")
t.p = ggtitle("Proportional Distribution of Fricatives")
bar.p + x.p + y.p + t.p

############################################################################
# 7. Limits on axes +xlim() + ylim()
############################################################################

# same geom_bar() as above
p1 = ggplot(int.df, aes(dB, Dauer)) + geom_point() 
# xlim
p2 = xlim(c(10, 60))
# ylim
p3 = ylim(c(30, 280))
p1 + p2 + p3

#reverse axes:
p4 = scale_x_reverse()
p5 = scale_y_reverse()

p1 + p4 + p5

Colors

(see http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf)

colors()
##   [1] "white"                "aliceblue"            "antiquewhite"        
##   [4] "antiquewhite1"        "antiquewhite2"        "antiquewhite3"       
##   [7] "antiquewhite4"        "aquamarine"           "aquamarine1"         
##  [10] "aquamarine2"          "aquamarine3"          "aquamarine4"         
##  [13] "azure"                "azure1"               "azure2"              
##  [16] "azure3"               "azure4"               "beige"               
##  [19] "bisque"               "bisque1"              "bisque2"             
##  [22] "bisque3"              "bisque4"              "black"               
##  [25] "blanchedalmond"       "blue"                 "blue1"               
##  [28] "blue2"                "blue3"                "blue4"               
##  [31] "blueviolet"           "brown"                "brown1"              
##  [34] "brown2"               "brown3"               "brown4"              
##  [37] "burlywood"            "burlywood1"           "burlywood2"          
##  [40] "burlywood3"           "burlywood4"           "cadetblue"           
##  [43] "cadetblue1"           "cadetblue2"           "cadetblue3"          
##  [46] "cadetblue4"           "chartreuse"           "chartreuse1"         
##  [49] "chartreuse2"          "chartreuse3"          "chartreuse4"         
##  [52] "chocolate"            "chocolate1"           "chocolate2"          
##  [55] "chocolate3"           "chocolate4"           "coral"               
##  [58] "coral1"               "coral2"               "coral3"              
##  [61] "coral4"               "cornflowerblue"       "cornsilk"            
##  [64] "cornsilk1"            "cornsilk2"            "cornsilk3"           
##  [67] "cornsilk4"            "cyan"                 "cyan1"               
##  [70] "cyan2"                "cyan3"                "cyan4"               
##  [73] "darkblue"             "darkcyan"             "darkgoldenrod"       
##  [76] "darkgoldenrod1"       "darkgoldenrod2"       "darkgoldenrod3"      
##  [79] "darkgoldenrod4"       "darkgray"             "darkgreen"           
##  [82] "darkgrey"             "darkkhaki"            "darkmagenta"         
##  [85] "darkolivegreen"       "darkolivegreen1"      "darkolivegreen2"     
##  [88] "darkolivegreen3"      "darkolivegreen4"      "darkorange"          
##  [91] "darkorange1"          "darkorange2"          "darkorange3"         
##  [94] "darkorange4"          "darkorchid"           "darkorchid1"         
##  [97] "darkorchid2"          "darkorchid3"          "darkorchid4"         
## [100] "darkred"              "darksalmon"           "darkseagreen"        
## [103] "darkseagreen1"        "darkseagreen2"        "darkseagreen3"       
## [106] "darkseagreen4"        "darkslateblue"        "darkslategray"       
## [109] "darkslategray1"       "darkslategray2"       "darkslategray3"      
## [112] "darkslategray4"       "darkslategrey"        "darkturquoise"       
## [115] "darkviolet"           "deeppink"             "deeppink1"           
## [118] "deeppink2"            "deeppink3"            "deeppink4"           
## [121] "deepskyblue"          "deepskyblue1"         "deepskyblue2"        
## [124] "deepskyblue3"         "deepskyblue4"         "dimgray"             
## [127] "dimgrey"              "dodgerblue"           "dodgerblue1"         
## [130] "dodgerblue2"          "dodgerblue3"          "dodgerblue4"         
## [133] "firebrick"            "firebrick1"           "firebrick2"          
## [136] "firebrick3"           "firebrick4"           "floralwhite"         
## [139] "forestgreen"          "gainsboro"            "ghostwhite"          
## [142] "gold"                 "gold1"                "gold2"               
## [145] "gold3"                "gold4"                "goldenrod"           
## [148] "goldenrod1"           "goldenrod2"           "goldenrod3"          
## [151] "goldenrod4"           "gray"                 "gray0"               
## [154] "gray1"                "gray2"                "gray3"               
## [157] "gray4"                "gray5"                "gray6"               
## [160] "gray7"                "gray8"                "gray9"               
## [163] "gray10"               "gray11"               "gray12"              
## [166] "gray13"               "gray14"               "gray15"              
## [169] "gray16"               "gray17"               "gray18"              
## [172] "gray19"               "gray20"               "gray21"              
## [175] "gray22"               "gray23"               "gray24"              
## [178] "gray25"               "gray26"               "gray27"              
## [181] "gray28"               "gray29"               "gray30"              
## [184] "gray31"               "gray32"               "gray33"              
## [187] "gray34"               "gray35"               "gray36"              
## [190] "gray37"               "gray38"               "gray39"              
## [193] "gray40"               "gray41"               "gray42"              
## [196] "gray43"               "gray44"               "gray45"              
## [199] "gray46"               "gray47"               "gray48"              
## [202] "gray49"               "gray50"               "gray51"              
## [205] "gray52"               "gray53"               "gray54"              
## [208] "gray55"               "gray56"               "gray57"              
## [211] "gray58"               "gray59"               "gray60"              
## [214] "gray61"               "gray62"               "gray63"              
## [217] "gray64"               "gray65"               "gray66"              
## [220] "gray67"               "gray68"               "gray69"              
## [223] "gray70"               "gray71"               "gray72"              
## [226] "gray73"               "gray74"               "gray75"              
## [229] "gray76"               "gray77"               "gray78"              
## [232] "gray79"               "gray80"               "gray81"              
## [235] "gray82"               "gray83"               "gray84"              
## [238] "gray85"               "gray86"               "gray87"              
## [241] "gray88"               "gray89"               "gray90"              
## [244] "gray91"               "gray92"               "gray93"              
## [247] "gray94"               "gray95"               "gray96"              
## [250] "gray97"               "gray98"               "gray99"              
## [253] "gray100"              "green"                "green1"              
## [256] "green2"               "green3"               "green4"              
## [259] "greenyellow"          "grey"                 "grey0"               
## [262] "grey1"                "grey2"                "grey3"               
## [265] "grey4"                "grey5"                "grey6"               
## [268] "grey7"                "grey8"                "grey9"               
## [271] "grey10"               "grey11"               "grey12"              
## [274] "grey13"               "grey14"               "grey15"              
## [277] "grey16"               "grey17"               "grey18"              
## [280] "grey19"               "grey20"               "grey21"              
## [283] "grey22"               "grey23"               "grey24"              
## [286] "grey25"               "grey26"               "grey27"              
## [289] "grey28"               "grey29"               "grey30"              
## [292] "grey31"               "grey32"               "grey33"              
## [295] "grey34"               "grey35"               "grey36"              
## [298] "grey37"               "grey38"               "grey39"              
## [301] "grey40"               "grey41"               "grey42"              
## [304] "grey43"               "grey44"               "grey45"              
## [307] "grey46"               "grey47"               "grey48"              
## [310] "grey49"               "grey50"               "grey51"              
## [313] "grey52"               "grey53"               "grey54"              
## [316] "grey55"               "grey56"               "grey57"              
## [319] "grey58"               "grey59"               "grey60"              
## [322] "grey61"               "grey62"               "grey63"              
## [325] "grey64"               "grey65"               "grey66"              
## [328] "grey67"               "grey68"               "grey69"              
## [331] "grey70"               "grey71"               "grey72"              
## [334] "grey73"               "grey74"               "grey75"              
## [337] "grey76"               "grey77"               "grey78"              
## [340] "grey79"               "grey80"               "grey81"              
## [343] "grey82"               "grey83"               "grey84"              
## [346] "grey85"               "grey86"               "grey87"              
## [349] "grey88"               "grey89"               "grey90"              
## [352] "grey91"               "grey92"               "grey93"              
## [355] "grey94"               "grey95"               "grey96"              
## [358] "grey97"               "grey98"               "grey99"              
## [361] "grey100"              "honeydew"             "honeydew1"           
## [364] "honeydew2"            "honeydew3"            "honeydew4"           
## [367] "hotpink"              "hotpink1"             "hotpink2"            
## [370] "hotpink3"             "hotpink4"             "indianred"           
## [373] "indianred1"           "indianred2"           "indianred3"          
## [376] "indianred4"           "ivory"                "ivory1"              
## [379] "ivory2"               "ivory3"               "ivory4"              
## [382] "khaki"                "khaki1"               "khaki2"              
## [385] "khaki3"               "khaki4"               "lavender"            
## [388] "lavenderblush"        "lavenderblush1"       "lavenderblush2"      
## [391] "lavenderblush3"       "lavenderblush4"       "lawngreen"           
## [394] "lemonchiffon"         "lemonchiffon1"        "lemonchiffon2"       
## [397] "lemonchiffon3"        "lemonchiffon4"        "lightblue"           
## [400] "lightblue1"           "lightblue2"           "lightblue3"          
## [403] "lightblue4"           "lightcoral"           "lightcyan"           
## [406] "lightcyan1"           "lightcyan2"           "lightcyan3"          
## [409] "lightcyan4"           "lightgoldenrod"       "lightgoldenrod1"     
## [412] "lightgoldenrod2"      "lightgoldenrod3"      "lightgoldenrod4"     
## [415] "lightgoldenrodyellow" "lightgray"            "lightgreen"          
## [418] "lightgrey"            "lightpink"            "lightpink1"          
## [421] "lightpink2"           "lightpink3"           "lightpink4"          
## [424] "lightsalmon"          "lightsalmon1"         "lightsalmon2"        
## [427] "lightsalmon3"         "lightsalmon4"         "lightseagreen"       
## [430] "lightskyblue"         "lightskyblue1"        "lightskyblue2"       
## [433] "lightskyblue3"        "lightskyblue4"        "lightslateblue"      
## [436] "lightslategray"       "lightslategrey"       "lightsteelblue"      
## [439] "lightsteelblue1"      "lightsteelblue2"      "lightsteelblue3"     
## [442] "lightsteelblue4"      "lightyellow"          "lightyellow1"        
## [445] "lightyellow2"         "lightyellow3"         "lightyellow4"        
## [448] "limegreen"            "linen"                "magenta"             
## [451] "magenta1"             "magenta2"             "magenta3"            
## [454] "magenta4"             "maroon"               "maroon1"             
## [457] "maroon2"              "maroon3"              "maroon4"             
## [460] "mediumaquamarine"     "mediumblue"           "mediumorchid"        
## [463] "mediumorchid1"        "mediumorchid2"        "mediumorchid3"       
## [466] "mediumorchid4"        "mediumpurple"         "mediumpurple1"       
## [469] "mediumpurple2"        "mediumpurple3"        "mediumpurple4"       
## [472] "mediumseagreen"       "mediumslateblue"      "mediumspringgreen"   
## [475] "mediumturquoise"      "mediumvioletred"      "midnightblue"        
## [478] "mintcream"            "mistyrose"            "mistyrose1"          
## [481] "mistyrose2"           "mistyrose3"           "mistyrose4"          
## [484] "moccasin"             "navajowhite"          "navajowhite1"        
## [487] "navajowhite2"         "navajowhite3"         "navajowhite4"        
## [490] "navy"                 "navyblue"             "oldlace"             
## [493] "olivedrab"            "olivedrab1"           "olivedrab2"          
## [496] "olivedrab3"           "olivedrab4"           "orange"              
## [499] "orange1"              "orange2"              "orange3"             
## [502] "orange4"              "orangered"            "orangered1"          
## [505] "orangered2"           "orangered3"           "orangered4"          
## [508] "orchid"               "orchid1"              "orchid2"             
## [511] "orchid3"              "orchid4"              "palegoldenrod"       
## [514] "palegreen"            "palegreen1"           "palegreen2"          
## [517] "palegreen3"           "palegreen4"           "paleturquoise"       
## [520] "paleturquoise1"       "paleturquoise2"       "paleturquoise3"      
## [523] "paleturquoise4"       "palevioletred"        "palevioletred1"      
## [526] "palevioletred2"       "palevioletred3"       "palevioletred4"      
## [529] "papayawhip"           "peachpuff"            "peachpuff1"          
## [532] "peachpuff2"           "peachpuff3"           "peachpuff4"          
## [535] "peru"                 "pink"                 "pink1"               
## [538] "pink2"                "pink3"                "pink4"               
## [541] "plum"                 "plum1"                "plum2"               
## [544] "plum3"                "plum4"                "powderblue"          
## [547] "purple"               "purple1"              "purple2"             
## [550] "purple3"              "purple4"              "red"                 
## [553] "red1"                 "red2"                 "red3"                
## [556] "red4"                 "rosybrown"            "rosybrown1"          
## [559] "rosybrown2"           "rosybrown3"           "rosybrown4"          
## [562] "royalblue"            "royalblue1"           "royalblue2"          
## [565] "royalblue3"           "royalblue4"           "saddlebrown"         
## [568] "salmon"               "salmon1"              "salmon2"             
## [571] "salmon3"              "salmon4"              "sandybrown"          
## [574] "seagreen"             "seagreen1"            "seagreen2"           
## [577] "seagreen3"            "seagreen4"            "seashell"            
## [580] "seashell1"            "seashell2"            "seashell3"           
## [583] "seashell4"            "sienna"               "sienna1"             
## [586] "sienna2"              "sienna3"              "sienna4"             
## [589] "skyblue"              "skyblue1"             "skyblue2"            
## [592] "skyblue3"             "skyblue4"             "slateblue"           
## [595] "slateblue1"           "slateblue2"           "slateblue3"          
## [598] "slateblue4"           "slategray"            "slategray1"          
## [601] "slategray2"           "slategray3"           "slategray4"          
## [604] "slategrey"            "snow"                 "snow1"               
## [607] "snow2"                "snow3"                "snow4"               
## [610] "springgreen"          "springgreen1"         "springgreen2"        
## [613] "springgreen3"         "springgreen4"         "steelblue"           
## [616] "steelblue1"           "steelblue2"           "steelblue3"          
## [619] "steelblue4"           "tan"                  "tan1"                
## [622] "tan2"                 "tan3"                 "tan4"                
## [625] "thistle"              "thistle1"             "thistle2"            
## [628] "thistle3"             "thistle4"             "tomato"              
## [631] "tomato1"              "tomato2"              "tomato3"             
## [634] "tomato4"              "turquoise"            "turquoise1"          
## [637] "turquoise2"           "turquoise3"           "turquoise4"          
## [640] "violet"               "violetred"            "violetred1"          
## [643] "violetred2"           "violetred3"           "violetred4"          
## [646] "wheat"                "wheat1"               "wheat2"              
## [649] "wheat3"               "wheat4"               "whitesmoke"          
## [652] "yellow"               "yellow1"              "yellow2"             
## [655] "yellow3"              "yellow4"              "yellowgreen"
############################ geom_boxplot()
ggplot(asp) + aes(y = d, x = Kons) + geom_boxplot()

# Default colors
# filled with different colors
ggplot(asp) + aes(y = d, x = Kons, fill = Kons) + geom_boxplot()

# different line colors
ggplot(asp) + aes(y = d, x = Kons, col = Kons) + geom_boxplot()

# or chose your own colors
farben = c("green", "red")
# filled
ggplot(asp) + aes(y = d, x = Kons) + geom_boxplot(fill = farben)

# line colors
ggplot(asp) + aes(y = d, x = Kons) + geom_boxplot(col = farben)

############################ geom_bar()
########## 
p1 = ggplot(coronal) + aes(x = Region, fill = Fr) + geom_bar()
p1

# Eigene Farben wählen
farben = c("yellow", "green")
p2 = scale_fill_manual(values = farben) 
p1 + p2

Plotting characters (pch) and character sizes (cex); line width (lwd)

(see http://www.endmemo.com/program/R/pchsymbols.php)

########## 
ggplot(int.df, aes(x = dB, y = Dauer)) +  geom_point() + geom_line()

# col: color. 
# pch: plotting character. 
# cex: character expansion:cex =2 means 2*standard size
ggplot(int.df, aes(x = dB, y = Dauer)) +  geom_point(col="purple", pch=0, cex=2) + geom_line(col = "pink")

# lwd: Liniendichte
ggplot(int.df, aes(x = dB, y = Dauer)) +  geom_point(col="purple", pch=0, cex=2) + geom_line(col = "pink", lwd=2)

Size of Labels

# Default size ist 11 (Legende: 10 (??))

p1 = ggplot(asp) + aes(y = d, x = Kons) + geom_boxplot() + xlab("Artikulationsstelle") + ylab("Dauer (ms)") + ggtitle("Boxplot-Daten")
p1

# size 16
p16 = theme(text = element_text(size=16))
p1 + p16

# change only on axes
q24 = theme(axis.text = element_text(size=24))
p1 + q24

# Different values on axes labels and title
p30 = theme(text = element_text(size=30))
p1 + q24 + p30

Two (or three) independent variables: Facets and (once again) colors

#create one boxplot per stress pattern (Bet: levels "be" and "un")
pf = facet_grid(~Bet)
p1 + pf

# or add col to aes():
pc = ggplot(asp) + aes(y = d, x = Kons,col=Bet) + geom_boxplot() + xlab("Artikulationsstelle") + ylab("Dauer (ms)") + ggtitle("Boxplot-Daten")
pc

You can, of course combine facets and colors and therefore plot the influences of up to three independent variables.

Arrange several figures on one plane

# if necessary, install.packages(gridExtra)
library(gridExtra)

p1 = ggplot(asp, aes(y = d, x = Kons))  + geom_boxplot()
p2 = ggplot(coronal) + aes(x = Region, fill = Fr) + geom_bar()
p3 = ggplot(int.df, aes(dB, Dauer)) + geom_line() + geom_point()
grid.arrange(p1, p2, p3,  ncol=3, nrow =1)

More things to change with theme

# see
help(theme)

Adding statistical measures to a plot

p1 = ggplot(int.df, aes(dB, Dauer)) + geom_point()
int.lm =  geom_smooth(method="lm",se=FALSE)
p1 + int.lm

#by default, geom_smooth shows the standard error:
int.lmse =  geom_smooth(method="lm")
p1 + int.lmse

# you can calculate this stat (here lm() ) for each facet (e.g. for each subject (Vpn)) separately

p1 + int.lmse + facet_grid(~Vpn)

Instead of geom_smooth(), you could also add lines with geom_abline(intercept=..., slope=... ), and horizontal and vertical lines with geom_hline() and geom_vline.

geom_smooth() can be used with several smoothing methods, like lm, but also glm (for sigmoidal curves fitting binary perceptual data), and some others (it can fit e.g. splines with loess). One example of method glm (in which you have to add the information that it is binomial data) would be:

bat.df = read.table("Rgraphics/dataSets/bat.df.txt")
bat.plot = ggplot(bat.df) + aes(y = p, x = steps) + geom_point(col = "red") +  facet_wrap(~participant)  + ggtitle("bat")

#add listener-specific sigmoids
bat.plot + geom_smooth(method = "glm",se=FALSE,method.args = list(family=binomial))

In phonetics, we often draw ellipses around two-dimensional data points, representing F2 and F1 values of vowels. We can add an ellipse by stat_ellipse().

ell = stat_ellipse()
p1 + ell

By default, this adds an ellipse representing the 95%-confidence interval (under the assumption of a multivariate t-distribution). While it is not extremely useful with the given data, it is useful in segregating vowel categories. However - be careful: at low numbers of tokens, one or two outliers can produce somehow “silly” ellipses:

td_mid = read.table("Rgraphics/dataSets/td_mid.txt")
p1 = ggplot(td_mid, aes(y = T1, x  = T2, col = labels, label=labels)) 

#add data.points as text labels, defined by their value
p2 = geom_text()
p1 + p2

p3 = stat_ellipse() 
p4 = scale_y_reverse() 
p5 = scale_x_reverse() 
p6 =labs(x = "F2(Hz)", y = "F1(Hz)") 
p7 = theme(legend.position="none")

p1 + p2 + p3 + p4 + p5 + p6 + p7

# only ellipses (do NOT plot data.points)
p1  + p3 + p4 + p5 + p6 + p7

#plot the label-specific means of F1 and F2 (here: T1 and T2)
p2_centroid = geom_text(data = aggregate(cbind(T1,T2)~labels,data=td_mid,FUN=mean))
p1 + p2_centroid + p3 + p4 + p5 + p6 + p7

#btw, we could also vary the linetype
p1_alt = ggplot(td_mid, aes(y = T1, x  = T2, col = labels, label=labels,linetype=labels)) 
p1_alt + p2_centroid + p3 + p4 + p5 + p6

It is also very easy to do the replacement of the dplot shown at the beginning of this document.

ggplot(vowels_fm_new) +
  aes(x=times_rel,y=T2,col=labels,group=sl_rowIdx) +
  geom_line() +
  labs(x = "vowel duration (ms)", y = "F2 (Hz)")

However, it is much more difficult to produce the time-normalized and by-vowel averaged version. We will need the function normalizeLength() (that will be available with the next release of emuR). We can, however, use a prepared version of a length-normalized emuRtrackdata object that contains normalized times:

td_norm = read.table("Rgraphics/dataSets/td_norm.txt")
ggplot(aggregate(T2~times_norm+labels, data = td_norm,FUN=mean)) +
  aes(x=times_norm,y=T2,col=labels) +
  geom_line() +
  labs(x = "vowel duration (normalized)", y = "F2 (Hz)")

Conclusion

This chapter gave a very short introduction into the package ggplot2. More information can be found at e.g. http://r-statistics.co/Complete-Ggplot2-Tutorial-Part1-With-R-Code.html, or https://opr.princeton.edu/workshops/Downloads/2015Jan_ggplot2Koffman.pdf, or any other website you may find (there are numerous introductions to ggplot2).