简体   繁体   中英

Combine scatter plot with text using ggplot2

I have this data for speech ( Utterance ), duration of pupil movement ( pp_Duration ) and averaged pupil diameter ( Diam_ave ) (reproducible data below):

df
# A tibble: 5 x 3
  Utterance                  pp_Duration                  Diam_av                 
  <chr>                      <chr>                        <chr>                   
1 "budget deficit, >its max… 11,17,16,17,17,16,17,17,16,… 29.1333648495164,29.315…
2 "°governmental°"           3,17,16,17,17,16,17,17,16,1… 28.3905779963636,28.295…
3 "tt >I don't know so ther… 7,17,17,16,17,17,16,17,17,1… 0,0,0,0,0,22.4466071458…
4 "the fisc[al cliff]"       0,17,16,17,17,16,17,17,16,1… 105.361017280303,105.51…
5 "a:nd the [!fiscal! yeah]… 7,17,17,16,17,17,16,17,17,1… 105.570349229848,106.41…

I want to visualize how Diam_av develops over the time course of each Utterance and I want to do it in ggplot2 and in a more elegant way than I am doing it so far.

The way I'm doing it is this: First I separate_rows so that each pair of pp_Duration and Diam_av are on their own row; also, I convert pp_Duration into a Time line:

library(tidyr)
f <- df %>%
  # separate into rows:
  separate_rows(c(pp_Duration, Diam_av), sep = ",", convert = TRUE) %>%
  # convert pp_Durations into timeline:
  mutate(Time = cumsum(pp_Duration))

This part maybe okay but the next part is definitely not. To plot how Diam_av develops over the time course of the Utterance s I make subsets one by one:

utt1 <- f[f$Utterance=="budget deficit, >its maximum budget deficit< °what's° (.) °what's that called again?°",]
utt2 <- f[f$Utterance=="°governmental°",]
utt3 <- f[f$Utterance=="tt >I don't know so there's a government shutdown<",]
utt4 <- f[f$Utterance=="the fisc[al cliff]",]
utt5 <- f[f$Utterance=="a:nd the [!fiscal! yeah] °yeah° (.) and uh (.) and so as a result all new positions are cut ",]

and then I plot each subset one by one; here I show only the code for the first Utterance :

plot(utt1$Time, log2(utt1$Diam_av), type = "l", ylim = c(4,7), 
     cex.axis = 0.7, cex.lab = 0.7)
text((range(utt1$Time)[1] + range(utt1$Time)[2])/2, 
     6.5, 
     utt1$Utterance[1], col = "blue", cex = 0.7)

Repeating this step for all other Utterance s gives this plot (with a few additional tweaks, not shown here): 在此处输入图片说明

How can roughly the same type of visualization be accomplished using ggplot2 and in a more economical and elegant way?

Reproducible data ( clipped to the first three Utterance s):

structure(list(Utterance = c("budget deficit, >its maximum budget deficit< °what's° (.) °what's that called again?°", 
"°governmental°", "tt >I don't know so there's a government shutdown<"
), pp_Duration = c("11,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,2", 
"3,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,8", 
"7,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,17,16,17,3"
), Diam_av = c("29.1333648495164,29.3151896365551,29.469147069034,29.4501818079504,29.2060285827929,29.5828683444776,29.1002573920905,29.2448155163236,28.9217762445333,28.4204407900411,24.6047401703714,36.1803831160107,0,20.5536673493043,0,0,0,23.2356532912825,26.4402592401269,26.6311495331712,26.6794903185086,26.7923874646865,26.7923903884212,26.9590358281491,26.9191176019264,27.0920732579174,27.0031239502966,27.0843606411252,27.1920904371408,27.2182848065971,27.5572336587757,27.4301518162332,27.4522555237956,27.5767158800675,28.0861229577817,28.0437135494608,27.9840332857746,28.0548776220808,27.9562668459212,28.116890560621,28.0112533348821,28.0268567965584,27.8793828823837,28.0174872919957,28.3456355629285,28.206363040224,28.2493296373718,27.8152971748186,28.0373756619494,28.0279560709977,27.516366903918,27.7507778425614,27.7577169019092,27.6980105712492,27.5113881220599,27.4422511480718,26.8776430670448,0,0,0,0,0,0,27.6515985860228,25.8673841014927,28.2897140125813,28.5902768970104,29.0111206143065,28.6797563946519,28.8662625166879,28.998775614607,29.1110456146793,29.1454190648297,29.191148322632,29.1186077425713,29.1344902527304,29.2356997510467,29.0550688572295,29.0958574204122,28.8089925476971,28.715855273909,28.5695475801588,28.4243662946824,28.1140536158124,27.9934930849171,27.6507017529648,27.4840232140962,27.1527507737006,27.0399849909983,26.9409418399153,26.7685338976787,26.6702492859676,26.7506506767269,26.6686279074704,26.5857741755924,26.6073218661184,26.5853127179327,26.6197000083842,26.5826254690547,26.7139755270495,26.6488066719343,26.7608353894869,26.5995876867481,26.803227484103,26.7145052606546,26.7767773146679,26.7893039778903,26.8901662773326,27.1228925866818,27.0828284327114,27.0636514553402,27.1476005009206,26.8285152911294,27.198360750039,26.8302702674121,26.9629738773038,27.0116419521018,27.3549848813271,26.9672055263537,27.2287602372547,27.5919892194418,27.1938081488275,27.3657133358968,27.1987991317014,27.2726880322049,27.407576177622,27.3665393430753,26.7532513091111,26.8391051051298,27.2294435322209,27.1793319959869,26.5989667807249,26.9826614006998,25.4128749530033,24.3163170159325,25.2243641502411,20.7856586100089,0,0,0,0,33.1381351784604,0,0,0,19.3998866952547,20.8107437296547,24.6757816887055,22.9061499742421,25.8242582813758,27.1831904497356,24.8450515821627,28.2909433637992,25.6530234245526,27.2779526939676,29.0823275637352,29.5965528539413,29.1619764439375,28.193203709794,29.9707605466123,29.9151670934281,30.149934168557,30.2180213294814,30.3180953653216,30.3399346140569,30.4823436459679,30.6085345403275,27.4924241571947,29.7753290948799,29.284044893838,30.5122634479867,30.9218171446896,30.6610074889142,30.846103165338,30.8025254379491,30.7538696369974,30.8494241625727,30.7827232537022,30.7705603621408,30.4188854273965,29.6284135907812,29.6135341836616,30.3369742489621,30.0328937533985,30.020998927493,29.78747887794,30.06059189153,30.3488022204071,29.7960130997285,29.7002385015011,29.6792548812048,29.128527026515,27.9216702691271,27.2051327182226,29.8933753081842,30.0744568343287,29.9237400827413,29.9754183121878,29.8248387588031,29.9932027204574,30.0984089022895,29.9400949503928,30.1388659394992,29.6011479978699,30.2331877739862,29.7991863489259,29.7904893232509,29.8916786056118,29.8054548881929,29.565362160592,29.604701749014,26.6110578565056,27.0347400442692,28.3558499197459,28.6696509138578,28.5561949198258", 
"28.3905779963636,28.2953606298876,28.5528397868147,28.6861830757977,28.7770660098806,28.7104382637732,28.6251778999798,28.9396986789674,27.7973554988981,26.8133498829269,27.6826301348484,28.2460029264776,28.3855868003233,28.1757543322105,28.1502919642341,28.0592071637994,28.0834883548836,28.1946895111778,28.300277102133,28.1364853099266,28.2185384101709,27.9957397798337,28.2032632269284,28.181537391636,28.2279470951109,28.1341497508107,26.5207303794602,26.1516019796849,26.058747240219,26.7739075188245,27.2534162866932,27.0156588832473,26.9194858174196,27.0073926045028,27.1350527744504,27.1793332872534,27.1361140646999,27.1068165592961,27.1575583133407,27.2663307720419,27.4372324982363,27.1967404698426,27.5683034737959,27.3765322007179,27.2779726666605,27.7254118489658,27.7349581416337,27.9101998939363,27.7465803782934,27.6440033212994,28.0049674777644,27.932591183728,27.8493828270154,27.0670653313353,25.8162708744594,26.5918979641149,28.845295249461,28.8286163456642,28.961698068184,28.8746263379437,28.8977131264575,28.8407313953671,28.8933042713692,28.8715012206713,28.6621115871792,28.7444009201158,28.6586824268679,28.4474189852398,28.5680859609526,28.4045477392881", 
"0,0,0,0,0,22.4466071458747,0,0,0,20.4374668775023,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41.3031782137797,54.1667070572805,62.0613339224113,54.2915792547477,60.2390885916101,31.9952909352879,86.8125303116705,90.5803211222064,81.8122482431379,81.5260880049391,82.8877273284523,89.6179727847784,84.6682282926776,64.9403907732926,92.9746914497752,93.141390689068,61.8808002137411,87.118510963503,55.2425897615853,87.8946192417597,87.6099680759948,53.6615701309437,93.1484760258723,98.8315037222354,93.3507236814968,87.4589362810239,93.1541371600161,65.1628738970507,98.6837369927226,92.7534425632098,93.520048009659,59.5908355163762,59.5600215363434,94.3076241817203,94.577717701043,94.5276757651292,93.1689634242008,92.4107060047454,92.9543731852422,92.4382786630791,92.2166472054473,92.3828850433316,92.1011749031159,91.6835412477367,91.8906660122502,92.0708348444159"
)), row.names = c(NA, -3L), class = c("tbl_df", "tbl", "data.frame"
))

What about using 'facet_wrap'. With your dataset, it could be something like that:

df %>%
  # separate into rows:
  separate_rows(c(pp_Duration, Diam_av), sep = ",", convert = TRUE) %>%
  # convert pp_Durations into timeline:
  mutate(Time = cumsum(pp_Duration)) %>%
  mutate(Utterance = factor(Utterance, levels = unique(Utterance))) %>%
  ggplot(aes(x = Time, y = log2(Diam_av)))+
  geom_line()+
  facet_wrap(~Utterance,ncol = 1, scales= "free_x")

giving this kind of graph:

在此处输入图片说明

Does it answer your question ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM