Search code examples
rplotpolygon

R - base plot - shaded area around graph


I have a simple line plot with fast data (grey) and a running mean of that data as the black solid line. What I would like to do is to shade the area along that black line 0.5 into the one and 0.5 into the other direction. The two dashed lines mark the boundaries of the wanted shaded area. I am trying it using polygon, but for some reason this does not work and I am clueless why.

Sample data (100 datapoints from the original data set, I hope that is sufficient):

xval,yval,runm
6.92232415850722,10223.1125440397,NA
6.97567055289054,10223.7940848759,NA
6.91987356947236,10224.4756257122,NA
6.76408889711752,10225.1571665485,NA
6.69498880703767,10225.8387073847,NA
6.73407232556043,10226.520248221,NA
6.77665179394136,10227.2017890573,NA
6.84275534062916,10227.8833298935,NA
6.8879116658316,10228.5648707298,NA
6.859664327829,10229.2464115661,NA
6.86841847225654,10229.9279524023,NA
6.87241424834036,10230.6094932386,NA
6.78738263324557,10231.2910340749,NA
6.7050107966374,10231.9725749112,NA
6.69077532290084,10232.6541157474,6.73282226315015
6.69285494365725,10233.3356565837,6.72268220267283
6.62670064675813,10234.01719742,6.71179512591555
6.53093184908463,10234.6987382562,6.70253770914134
6.58924635285394,10235.3802790925,6.69925694996729
6.65040546246993,10236.0618199288,6.69745327131799
6.58506323087209,10236.743360765,6.69173700699822
6.56402697098527,10237.4249016013,6.68353590094177
6.64572157610832,10238.1064424376,6.67203615987758
6.64300749410918,10238.7879832738,6.65723867033863
6.60448531596631,10239.4695241101,6.64390128414206
6.68744482436378,10240.1510649464,6.62987409509451
6.77653122345591,10240.8326057827,6.61481449411185
6.77982596642602,10241.5141466189,6.60349559698926
6.70034395646111,10242.1956874552,6.59484601818669
6.60607516873503,10242.8772282915,6.58630743931765
6.61812234418761,10243.5587691277,6.57777301300167
6.64905825017204,10244.240309964,6.57106905111653
6.64215106624629,10244.9218508003,6.56731069350767
6.6656661218959,10245.6033916365,6.56115889417799
6.64087844755865,10246.2849324728,6.55206561208198
6.56258439596747,10246.9664733091,6.54615687116913
6.53061861224771,10247.6480141453,6.54364352434563
6.49776310870341,10248.3295549816,6.53753626694185
6.44398697966327,10249.0110958179,6.5303866690769
6.45954274193178,10249.6926366542,6.5253855499697
6.44760280082999,10250.3741774904,6.51706498590221
6.42062621886073,10251.0557183267,6.50518371164912
6.44781571956776,10251.737259163,6.49279896061664
6.44552343256037,10252.4187999992,6.48280479268503
6.4346179568295,10253.1003408355,6.47654760683055
6.43682215417811,10253.7818816718,6.46887102304387
6.42558179020368,10254.463422508,6.45866609866778
6.41818112081901,10255.1449633443,6.44936275229822
6.40469237296339,10255.8265041806,6.44035921303931
6.37760699958962,10256.5080450168,6.43222122081491
6.40780100348678,10257.1895858531,6.42757789813851
6.48862656628022,10257.8711266894,6.42546284496523
6.46250385399501,10258.5526675257,6.42427938251218
6.42851955816049,10259.2342083619,6.42336656112931
6.45445174275044,10259.9157491982,6.42218255079228
6.43782790233887,10260.5972900345,6.42271832686576
6.42009299586329,10261.2788308707,6.42411200946439
6.40828343545173,10261.960371707,6.42358845888727
6.40051891851261,10262.6419125433,6.42288666527028
6.41835959310065,10263.3234533795,6.42330761378413
6.38782483058741,10264.0049942158,6.42433897848796
6.34291051888921,10264.6865350521,6.42359033379822
6.3630506751594,10265.3680758883,6.42102732130494
6.39555994412865,10266.0496167246,6.42059540146731
6.39673868082688,10266.7311575609,6.42200966068561
6.42328471567517,10267.4126983972,6.42010528403423
6.46716701704941,10268.0942392334,6.41246137794253
6.46225923511201,10268.7757800697,6.40430946548378
6.41660233817708,10269.457320906,6.39819636499828
6.42402243182104,10270.1388617422,6.39399224749757
6.46367608303422,10270.8204025785,6.39208376519963
6.4624366968196,10271.5019434148,6.39065706450328
6.43210920225426,10272.183484251,6.38782977792963
6.42446962405071,10272.8650250873,6.38305528599779
6.44724641224506,10273.5465659236,6.37801017143837
6.4677630952928,10274.2281067599,6.37457292997379
6.40312244951144,10274.9096475961,6.37452990112091
6.34129074602072,10275.5911884324,6.37638709638177
6.39173477783444,10276.2727292687,6.37667201948967
6.42003477613879,10276.9542701049,6.37556556763871
6.35066970394522,10277.6358109412,6.37332223702913
6.25930938352939,10278.3173517775,6.3701624857799
6.21794648023232,10278.9988926137,6.36742649250264
6.24512654359568,10279.68043345,6.36439918202126
6.32832821772913,10280.3619742863,6.35976149094678
6.38057343340059,10281.0435151225,6.3548329663133
6.37729197497268,10281.7250559588,NA
6.32346483824232,10282.4065967951,NA
6.25728416055731,10283.0881376314,NA
6.26700615631804,10283.7696784676,NA
6.28470758665022,10284.4512193039,NA
6.34161965330271,10285.1327601402,NA
6.41876653298508,10285.8143009764,NA
6.40410763736566,10286.4958418127,NA
6.36354512529815,10287.177382649,NA
6.35598479738773,10287.8589234852,NA
6.37237447957265,10288.5404643215,NA
6.38017943679403,10289.2220051578,NA
6.32578302373567,10289.903545994,NA
6.28489169958693,10290.5850868303,NA
6.31582034402957,10291.2666276666,NA

enter image description here

The code is as follows:

plot(dat$xval, dat$yval, pch=".", col="grey", ylab = "", xlab = "", type="l", xlim = c(5,8))
lines(rollmean(dat$xval, 30, fill=NA), dat$yval, col="black", lwd=2)

xmin <- c(rollmean(dat$xval, 30, fill=NA) - 0.5)
xmax <- c(rollmean(dat$xval, 30, fill=NA) + 0.5)

lines(xmin, dat$yval, col="black", lwd=2, lty = 2)
lines(xmax, dat$yval, col="black", lwd=2, lty = 2)

polygon(c(xmin, rev(xmax)), c(dat$yval, rev(dat$yval)), col = rgb(0, 0, 1, alpha = 0.3), border = NA)

And the outcome of the polygon command is this:

enter image description here

I don't have minimal example data, but maybe it is possible to see the error I am doing without it. It looks like that the upper end is connected to beginning instead of moving along the xaxis towards the end of xmax and then moving in reverse order back down how I thought it is supposed to.


Solution

  • The rollmean function inevitably produces NA values at the "edges" of xmin which polygon can't handle well.

    If you leave them out explicitly, everything works as expected:

    plot(dat$xval, dat$yval, pch=".", col="grey", ylab="", xlab="", 
         type="l", xlim=c(5,8))
    lines(rollmean(dat$xval, 30, fill=NA), dat$yval, col="black", lwd=2)
    
    xmin <- c(rollmean(dat$xval, 30, fill=NA) - 0.5)
    xmax <- c(rollmean(dat$xval, 30, fill=NA) + 0.5)
    
    lines(xmin, dat$yval, col="black", lwd=2, lty=2)
    lines(xmax, dat$yval, col="black", lwd=2, lty=2)
    
    not.na <- !is.na(xmin)
    polygon(c(xmin[not.na], rev(xmax[not.na])), 
            c(dat$yval[not.na], rev(dat$yval[not.na])), 
            col=rgb(0, 0, 1, alpha=0.3), border=NA)
    

    graph