ONeillMB1 ONeillMB1 - 2 months ago 14
R Question

Moving average from range data in R

I have a series of time ranges visualized below (note the y axis does not matter). I want to 'smooth' this data so that for any give time along the x axis I know how many segments span that moment. I have found several posts on how to calculate a moving average in R; however, they are all based on time series data. I am seeking advice on how to summarize my data which is in the format of a 'start' and 'stop' into the form of a 'at time(x) there are y events spanning that time'.

enter image description here

Here is the sample dataset plotted above:

> dput(d3)
structure(list(y = c(344L, 347L, 340L, 716L, 736L, 1061L, 911L,
299L, 296L, 294L, 295L, 292L, 290L, 291L, 591L, 592L, 199L, 198L,
196L, 192L, 270L, 271L, 272L, 275L, 276L, 277L, 278L, 279L, 524L,
523L, 442L, 441L, 446L, 100L, 106L, 905L, 33L, 31L, 30L, 37L,
642L, 438L, 339L, 338L, 334L, 337L, 336L, 333L, 332L, 744L, 745L,
857L, 6L, 98L, 844L, 95L, 557L, 556L, 552L, 238L, 235L, 236L,
231L, 1050L, 1053L, 143L, 140L, 141L, 613L, 617L, 148L, 149L,
951L, 941L, 689L, 683L, 132L, 134L, 139L, 490L, 26L, 27L, 22L,
28L, 379L, 378L, 371L, 370L, 373L, 374L, 705L, 702L, 392L, 83L,
84L, 85L, 797L, 790L, 7L, 584L, 245L, 246L, 240L, 924L, 926L,
511L, 512L, 515L, 452L, 453L, 456L, 457L, 179L, 178L, 176L, 174L,
170L, 182L, 183L, 181L, 187L, 184L, 185L, 886L, 189L, 10L, 15L,
14L, 17L, 16L, 19L, 864L, 884L, 881L, 950L, 323L, 321L, 326L,
327L, 201L, 200L, 202L, 208L, 77L, 76L, 75L, 72L, 71L, 70L, 78L,
2L, 1048L, 664L, 662L, 699L, 542L, 547L, 68L, 869L, 123L, 124L,
765L, 1010L, 920L, 923L, 776L, 499L, 319L, 313L, 312L, 314L,
921L, 832L, 3L, 725L, 366L, 364L, 365L, 363L, 360L, 381L, 382L,
383L, 384L, 387L, 389L, 788L, 579L, 576L, 259L, 64L, 65L, 66L,
67L, 252L, 250L, 251L, 257L, 254L, 255L, 603L, 508L, 509L, 506L,
507L, 502L, 503L, 500L, 468L, 463L, 164L, 165L, 166L, 162L, 163L,
1102L, 9L, 893L, 894L, 357L, 355L, 353L, 351L, 350L, 807L, 358L,
214L, 215L, 218L, 1033L, 1074L, 1070L, 1003L, 281L, 280L, 283L,
282L, 285L, 284L, 287L, 286L, 678L, 679L, 675L, 263L, 261L, 264L,
1031L, 269L, 58L, 57L, 51L, 50L, 53L, 52L, 537L, 533L, 532L,
531L, 539L, 981L, 986L, 985L, 953L, 301L, 824L, 826L, 820L, 1038L,
1039L, 1046L, 569L, 737L, 560L, 562L, 564L, 229L, 227L, 226L,
222L, 221L, 220L, 1001L, 1024L, 391L, 723L, 728L, 151L, 158L,
635L, 48L, 49L, 45L, 43L, 40L, 5L, 484L, 483L, 481L, 473L, 471L,
479L), med1 = c(368.946054867084, 291.897692486097, 686.551291017994,
375.671887635388, 501.150181751425, 1632.64480524179, 320.220059124658,
935.065925414262, 528.919542438531, 261.151001000102, 296.697708329904,
493.42302269819, 627.780232158787, 493.42302269819, 177.583807543134,
72.7078418069536, 566.889975368825, 573.222905004834, 416.1980571264,
167.662601125897, 439.789286568842, 482.706249756357, 482.706249756357,
464.585778027563, 394.237135327563, 394.237135327563, 437.874912052593,
511.931469081634, 593.138212655188, 499.694916209964, 987.649912212124,
681.48357294428, 861.863386887195, 368.418124724488, 236.465797054011,
368.946054867084, 84.5644646808382, 351.560834895982, 373.060292380781,
333.487546395183, 343.956719824158, 149.27381942208, 421.818147772037,
421.818147772037, 472.218398688553, 504.080212384862, 476.8894460605,
472.218398688553, 530.078552009203, 2283.84282466004, 1256.13764195129,
531.856634917506, 616.120468354171, 20.6888972224936, 464.585778027563,
68.1535379191005, 865.333184046004, 501.894506744091, 359.421135214661,
860.119004262127, 452.386758287036, 687.246054743733, 377.062322047126,
1579.53209399355, 1078.28212090992, 357.396135881107, 144.659764185851,
144.659764185851, 348.371459003868, 344.286421591311, 431.056246654705,
445.387821613564, 1098.06540398953, 338.097004919986, 103.346419349034,
276.156428704474, 88.2361894833218, 259.763127895686, 36.6402501681159,
1033.30059558483, 179.704099736262, 286.793504544989, 230.973431897317,
252.146941101945, 389.937432751327, 338.097004919986, 451.760711631279,
812.343817687462, 259.944624253393, 265.870628628874, 24.2060322743025,
97.5630276432801, 71.5164956426049, 63.6633694922466, 73.0889909848329,
76.7660834978547, 692.220546028846, 294.602836904093, 67.1432835867822,
611.883974996428, 458.424240700748, 152.42643258125, 138.613227738621,
430.711634705192, 464.743286282379, 778.874106395508, 1632.64480524179,
582.561160820872, 849.726543551087, 304.087902126991, 418.531564881009,
418.531564881009, 497.621872910499, 497.621872910499, 618.718289709735,
586.588482917965, 368.44513779363, 688.042386176933, 271.258100351862,
732.372816752337, 426.856562850548, 271.258100351862, 313.847016657358,
1298.52330195319, 346.148403288736, 492.977331902279, 370.083871916793,
389.012696704257, 401.482731769436, 370.083871916793, 12.5341747219229,
619.71859994423, 967.055102889789, 1108.94429180952, 831.186376961025,
503.304967310127, 967.055102889789, 495.809837197111, 814.195055402879,
581.012263017256, 566.889975368825, 708.956511139747, 98.631335954673,
104.108593469476, 102.371427215996, 60.5764116719274, 63.6985066343227,
63.6985066343227, 70.127908932847, 80.9301606341051, 430.225519702263,
712.05670185939, 79.2006624971791, 76.7660834978547, 74.727006266432,
708.962458123489, 664.596397815476, 85.7400117854313, 974.60025714224,
74.727006266432, 97.5630276432801, 358.209664224869, 612.020236159809,
899.681910031094, 458.085887228637, 587.412810034177, 1132.54398137967,
333.244273558609, 866.439023248492, 866.439023248492, 979.854655783467,
469.938245675386, 882.984214454269, 639.029128677423, 32.7204383490066,
894.282873043937, 631.613900497221, 631.613900497221, 469.938245675386,
316.61466421411, 273.374729548883, 296.944858892382, 296.944858892382,
652.642824408741, 180.602980309193, 85.027459714511, 667.839336402536,
492.977331902279, 661.560016581062, 232.042178972458, 159.735908778631,
404.331646707662, 404.331646707662, 343.956719824158, 132.5401492497,
803.203868935649, 536.233331817024, 582.432298144919, 458.411027323615,
190.174640246333, 433.200958781613, 431.395485521417, 306.504635592015,
373.881627748759, 431.395485521417, 346.942291151142, 346.942291151142,
1132.54398137967, 92.0708768788675, 674.493208966483, 671.12148984204,
459.026795332829, 455.04549028781, 660.33733340859, 956.13813542517,
3997.4301699219, 95.5639665826079, 858.020741785472, 858.020741785472,
427.026058788007, 199.714331200633, 317.603855929808, 784.157415931327,
784.157415931327, 1201.66195244627, 427.026058788007, 387.14501255323,
378.391904588357, 469.975759957832, 606.309875063964, 935.158184896916,
576.507044635476, 336.473072699556, 466.08988453481, 466.08988453481,
742.245898522691, 474.993578051296, 647.165726041114, 742.245898522691,
289.336271404564, 289.336271404564, 248.954088630692, 248.954088630692,
357.396135881107, 318.679166035926, 215.463571984583, 318.679166035926,
652.199052646016, 439.789286568842, 94.9177745635627, 94.9177745635627,
235.495313350644, 234.739496859824, 208.683665151238, 224.967707240973,
782.976261855666, 591.318335103584, 602.545598216988, 787.05450936268,
1217.48806744917, 3951.97397981284, 1271.19179175426, 1271.19179175426,
1086.7331143726, 288.657151872425, 641.880175939599, 513.448663631554,
536.233331817024, 1085.13366613081, 412.005752342262, 1102.64280103289,
761.79612221564, 301.156872476219, 4130.01113177392, 3636.49647739135,
2283.84282466004, 227.255824930042, 171.451878242148, 171.451878242148,
77.5562969955613, 473.285380419838, 515.823314677668, 560.62136401321,
470.072223539501, 1086.7331143726, 502.951389943578, 1087.00389974226,
606.162979600095, 31.3526554160512, 141.51658272768, 258.582096798224,
234.739496859824, 82.4150762804732, 193.006007370882, 385.525743098602,
517.792954848677, 475.212437582284, 729.636269127372, 799.573084076505,
1085.10956517537, 254.782666913692, 1085.13366613081), med2 = c(5.9094418247696e-10,
5.9026206145063e-10, 5.9026206145063e-10, 179.928092448528, 301.156872476219,
778.874106395508, 41.6982282937699, 5.90034687775187e-10, 5.92081050854176e-10,
5.92081050854176e-10, 5.91171556152403e-10, 5.92081050854176e-10,
5.9026206145063e-10, 5.92081050854176e-10, 72.7078418069536,
12.5341747219229, 6.12089934293181e-10, 6.11180439591408e-10,
6.12999428994954e-10, 6.13908923696727e-10, 5.89352566748857e-10,
5.91171556152403e-10, 5.91171556152403e-10, 5.91171556152403e-10,
5.9026206145063e-10, 5.9026206145063e-10, 5.90716808801517e-10,
5.92081050854176e-10, 6.09361450187862e-10, 6.10270944889635e-10,
5.6979843066074e-10, 5.62977220397443e-10, 5.6206772569567e-10,
5.92990545555949e-10, 5.93900040257722e-10, 148.37447303732,
5.92081050854176e-10, 5.92990545555949e-10, 5.92990545555949e-10,
5.93900040257722e-10, 336.779474319648, 5.71162672713399e-10,
5.91171556152403e-10, 5.91171556152403e-10, 5.90489435126074e-10,
5.90489435126074e-10, 5.9026206145063e-10, 5.90489435126074e-10,
5.91171556152403e-10, 1256.13764195129, 1201.66195244627, 143.820378219854,
5.91398929827847e-10, 5.92081050854176e-10, 18.0578118741103,
5.92081050854176e-10, 6.15727913100272e-10, 6.14818418398499e-10,
6.15727913100272e-10, 6.11180439591408e-10, 6.12089934293181e-10,
6.11180439591408e-10, 6.12999428994954e-10, 1128.5849841858,
830.330465171175, 5.92990545555949e-10, 5.93900040257722e-10,
5.93900040257722e-10, 262.855361520932, 193.006007370882, 5.93900040257722e-10,
5.92081050854176e-10, 85.027459714511, 271.512533289177, 4.40771726481739,
116.280381261191, 5.95719029661268e-10, 5.92990545555949e-10,
5.92990545555949e-10, 6.18001649854705e-10, 5.93445292906836e-10,
5.94809534959495e-10, 5.94809534959495e-10, 5.93445292906836e-10,
5.89352566748857e-10, 5.90716808801517e-10, 5.87533577345312e-10,
5.88443072047085e-10, 5.89352566748857e-10, 5.89807314099744e-10,
14.696963239845, 70.6343870644591, 6.11635186942294e-10, 5.92990545555949e-10,
5.92990545555949e-10, 5.92990545555949e-10, 452.386758287036,
166.535720816878, 5.91171556152403e-10, 453.15187309193, 6.10725692240521e-10,
6.04359229328111e-10, 6.12089934293181e-10, 199.714331200633,
456.915214125292, 5.62295099371113e-10, 6.28006091574207e-10,
6.08451955486089e-10, 5.61158230993897e-10, 5.63886715099216e-10,
5.69343683309853e-10, 5.69343683309853e-10, 5.9867488744203e-10,
5.9867488744203e-10, 6.00266503170133e-10, 5.9935700846836e-10,
5.92081050854176e-10, 6.13908923696727e-10, 6.12999428994954e-10,
6.00266503170133e-10, 6.13908923696727e-10, 6.12999428994954e-10,
6.12999428994954e-10, 814.195055402879, 6.12089934293181e-10,
5.90716808801517e-10, 5.92990545555949e-10, 5.93900040257722e-10,
5.94809534959495e-10, 5.92990545555949e-10, 5.94582161284052e-10,
175.007987968935, 337.819321648658, 292.942892808184, 180.602980309193,
5.88897819397971e-10, 5.91398929827847e-10, 5.88443072047085e-10,
5.89807314099744e-10, 6.11180439591408e-10, 6.13908923696727e-10,
6.12544681644067e-10, 6.10270944889635e-10, 5.94809534959495e-10,
5.92990545555949e-10, 5.92990545555949e-10, 5.92990545555949e-10,
5.92990545555949e-10, 5.92990545555949e-10, 5.92535798205063e-10,
5.9026206145063e-10, 425.036556400271, 35.98546337373, 2.86016765982458,
46.9208715756442, 6.09361450187862e-10, 6.14818418398499e-10,
5.92990545555949e-10, 652.507072685924, 5.96173777012154e-10,
5.95719029661268e-10, 167.662601125897, 375.261535909629, 469.938245675386,
430.711634705192, 128.777045136448, 6.30279828328639e-10, 5.91171556152403e-10,
5.89807314099744e-10, 5.89807314099744e-10, 5.94809534959495e-10,
464.743286282379, 32.3722423912727, 5.9026206145063e-10, 3.88751825289819,
5.87988324696198e-10, 5.9026206145063e-10, 5.9026206145063e-10,
5.88897819397971e-10, 5.9026206145063e-10, 5.88443072047085e-10,
5.86169335292652e-10, 5.86169335292652e-10, 5.88443072047085e-10,
5.89352566748857e-10, 6.07542460784316e-10, 473.285380419838,
95.5639665826079, 563.485296227425, 5.91171556152403e-10, 5.94809534959495e-10,
5.91171556152403e-10, 5.91171556152403e-10, 5.94809534959495e-10,
5.91171556152403e-10, 6.02540239924565e-10, 5.9026206145063e-10,
5.9026206145063e-10, 5.89352566748857e-10, 5.91171556152403e-10,
393.679367001997, 5.66615199204534e-10, 5.66615199204534e-10,
5.6843418860808e-10, 5.66615199204534e-10, 5.66615199204534e-10,
5.6843418860808e-10, 6.30279828328639e-10, 5.64796209800988e-10,
5.6206772569567e-10, 5.9026206145063e-10, 5.92081050854176e-10,
5.9026206145063e-10, 5.9935700846836e-10, 5.9935700846836e-10,
1026.02188411319, 5.92081050854176e-10, 617.628362503441, 20.2162425363044,
5.88897819397971e-10, 5.89352566748857e-10, 5.9026206145063e-10,
5.9026206145063e-10, 5.9026206145063e-10, 458.424240700748, 5.89352566748857e-10,
6.12089934293181e-10, 6.12089934293181e-10, 6.12999428994954e-10,
540.874405445984, 33.2823647426485, 306.504635592015, 149.27381942208,
5.91171556152403e-10, 5.9026206145063e-10, 5.89807314099744e-10,
5.9026206145063e-10, 5.9026206145063e-10, 5.89352566748857e-10,
5.92990545555949e-10, 5.92990545555949e-10, 119.618497227977,
236.465797054011, 290.969520110033, 5.9026206145063e-10, 5.90489435126074e-10,
5.9026206145063e-10, 92.0708768788675, 5.89352566748857e-10,
5.93900040257722e-10, 5.93900040257722e-10, 5.93900040257722e-10,
5.92081050854176e-10, 5.93445292906836e-10, 5.92990545555949e-10,
6.08224581810646e-10, 6.10043571214192e-10, 6.08451955486089e-10,
6.08451955486089e-10, 6.0572347138077e-10, 1993.46125096711,
891.18648034266, 1.49003914297919, 71.5164956426049, 5.9026206145063e-10,
232.042178972458, 440.893230387429, 132.5401492497, 412.005752342262,
1.4471257259197, 475.212437582284, 430.225519702263, 32.8873311707098,
3951.97397981284, 2535.66125304385, 1713.09859035141, 6.14818418398499e-10,
6.1345417634584e-10, 6.1345417634584e-10, 6.12999428994954e-10,
6.14818418398499e-10, 6.13908923696727e-10, 336.473072699556,
16.377896678119, 6.2118488131091e-10, 38.3288998547109, 722.521996474829,
5.91171556152403e-10, 5.9162630350329e-10, 86.1396602370546,
5.92081050854176e-10, 5.92081050854176e-10, 5.93900040257722e-10,
5.92990545555949e-10, 5.93900040257722e-10, 5.91171556152403e-10,
5.64796209800988e-10, 5.65705704502761e-10, 5.67069946555421e-10,
6.33008312433958e-10, 5.6843418860808e-10, 6.32098817732185e-10
)), .Names = c("y", "med1", "med2"), class = "data.frame", row.names = c(NA,
-302L))

Answer

Maybe you can build up something on this:

library(ggplot2)
library(data.table)
library(cowplot)

range <- range(d3[-1])
dt1 <- data.table(start = floor(seq(range[1], range[2]+1, 1)))[,end := start + 1]
setkey(dt1, start, end)
dt2 <- data.table(start = d3$med2, end = d3$med1)
dt <- foverlaps(dt2, dt1)[,.N,by="start"]  

p1 <- ggplot(d3, aes(x=med1,xend=med2,y=y,yend=y)) + geom_segment()
p2 <- ggplot(dt, aes(x=start, y=N)) + geom_line() 
plot_grid(
  p1 + xlim(0,4500), 
  p2 + xlim(0,4500), 
  ncol = 1, align = "h"
)

enter image description here