Polygon Shading Based on Relative Line Position

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Polygon Shading Based on Relative Line Position

emorway
Forum,

I have two time series that I plot up using the data/code below.  The x-axis shows weekly time step  starting on 6/3/1999 (SP=10) and ending on 10/25/2007 (SP=448).  I'm trying to shade between the two lines in a similar fashion as the graph found here:

http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=122

Can the polygon function be used to do this?  There are lots of posts pertaining to shading between two lines on this forum, but none with alternating shading.  I did look over the ?polygon discussion, specifically the brownian motion plot, but the shading is constant regardless of which line is greater than/above the other.  Due to the 20 or so times the lines cross each other I wasn't sure how to modify the last example in ?plot

DS.Load.filtered<-read.table(textConnection("SP       JMLoad CoolidgeLoad
10 6287371153.3   5383351151
11 5016438684.4   5149840395
12 4020238617.8   4097757349
18 1681570096.3   2330476184
19 2011084698.2   1568941725
21 1145032462.8    959165782
22 1063307043.8    645435740
24  925120077.1    667723509
25  587189249.9    707324096
26  541535137.0    639181098
27  421481931.6    692405402
28  268198706.4    599814619
30  490052206.1    509190477
32  429672101.9    518539938
33  479360154.8    518084664
34  332705173.5    627963150
40    7437921.6    520362947
41    7272780.2    511155928
44  189312657.1    461023436
46  159935695.6    834850739
48  580467416.1    909478429
49  711678748.9    825542943
54  820999693.4    602141144
55 1281245122.3    539526986
56 1234525377.3    607948868
57 1107369351.4    761832777
59  930102499.8    513626069
60  934106161.7    525646818
61 1074012858.8    436276835
62 1856920332.5    660276684
64 2564136121.9   1610652560
65 2404760972.5   1465864246
66 2035806517.6   1591773461
68 1886124760.8   1611344996
71 2287805156.8   1382811156
73 1376909678.5    791595277
74 1265209973.9    638691715
75  777429216.0    404560430
76  595882924.8    315035344
77  521183273.0    253026361
78  533726296.6    245560652
82  267965540.5    295694926
94    3645170.3    334233598
95    3644636.1    338526100
96    3616835.0    357388387
97    3478475.0    360407980
98    3395990.1    383170168
99    3424680.2    375567185
103    7706454.2    338718083
104    7484685.1    341367271
105   12100851.7    350978464
106   43374657.9    326563936
109 1172149909.6    263931016
110 1281917018.7    345653054
113  835813366.3    682986351
114  524765450.1    717050861
115  220652819.6    948602823
116 1147516084.1    349630240
117 2205837663.4    750724733
118 2506424170.0   1178190964
119 2265639020.8   1075464253
120 2068263612.8   1413273356
121 2156640167.4   1066482651
123 1910796858.1   1037178735
124 1611203132.5   1073785193
125  778788726.3    542689397
126  833341620.3    306049833
127  761683183.2    237702159
128  719063683.5    195396505
129 1146047977.4    285866547
130  801138377.3    316385292
131  657429028.1    213995657
133  118871474.0    211083899
134  204384729.1    192625209
135  260090209.9    199517710
136   24201037.7    194351081
137    8417831.9    198857821
138   15899802.8    191076815
139    9772533.4    199805343
140    3616601.1    203149621
141    3893325.1    207562895
146    2789497.9    256905051
147    2624087.4    239137572
148    2320129.9    233871812
149    2375061.3    244789361
150    2623139.9    230084008
151    2679009.1    235954773
152    2623771.6    221495933
153    2679186.6    222735406
155    2651789.1    172518801
156    2679171.0    161670825
157   13314442.4    144901548
158   56080331.1    125125488
159  786640402.5    184918943
160 1069335727.1    260721047
161 1077616331.7    124086059
163  183185160.3    118822561
164  143488200.9     69403023
165  200176394.9     76669321
166  229339546.2     57984637
167  270128506.1     58218605
168  794854222.2     63732496
169 1043519643.8    429644515
170  987053037.5    480691194
173  196554934.9     52094336
174  208620907.2     45293829
175   79198798.3     33293628
176   91447590.0     29101572
181  511657497.8    143139470
187   90245277.8    101068632
188   15889549.8    116258690
189    3182084.1    112911409
190    3042679.0    113186554
191    2903819.8    113216345
192    2903116.0    114596143
193    2710229.9    116519095
194    2710557.5    116534186
195    2599733.3    110767298
196    2516739.6    111859396
197    2543807.5    116030474
198    2322890.6    116630304
201    2681845.9    114896789
202    2488111.8    115222450
203    2626004.0    117142088
204    2238467.7    113530229
205    2293570.7    119880375
206    2266600.2    113747430
207    2570052.8    104537669
208    2515095.9    100642204
210    2432006.7     70423676
211  158252693.6     67914034
212  339920353.6     59404637
213  337973820.1     74727730
214  403331802.8     75101010
219 1278541396.5    244833157
220 1134609769.0    204191503
221 1137461413.1    255674658
222 1101354706.1    117538411
223  509565793.1     40451505
224  147178862.1     21781015
225   86798309.4     13992561
226  111531702.6     12036328
227  182979947.0      8125793
229   96398448.6     13121466
230   51948743.1      8392788
231   47290934.8     34536658
232   65168032.6     27907799
233   82718982.7      6434625
234   81608382.4      4479503
235  139857750.2     25370160
236   36313512.1     30118257
237   85011696.7     25158945
238   55853780.8     35793112
239   54475444.5     43675540
240   12792881.9     17923627
241    1941540.2     15409209
242    1752351.3     32238612
243    1524646.5     55471057
244    1488897.0     70278368
245    1436595.0     65551659
251    1898173.2     82756362
252   11290154.2     78558320
253    1547688.5     82184528
254    1597493.8     83879654
255    1815598.2     77736746
256    1851701.8     82177802
257    1675996.0     87766365
258    1880507.1     86339892
259    1894715.5     74348367
260    2013665.3     52219133
261  921397448.2    182038337
262 1559105226.5    871289893
263 1027693667.4    943572531
264  277702712.0    178941051
265  720168314.2    209602180
266 1210791105.7    239824540
267 1192002857.1    161955326
268 1010773196.7     95707330
269 1048954337.5     74392145
270 1017324603.1     31675124
271  805517148.8     29667342
272  897896143.3     79525472
273  985830250.2    718319742
274  916743759.8    454735376
275  795084927.0    330364176
276  412039604.0    171735029
281  769334261.0    237591603
282  579641252.2    197510042
283  843633501.1    121953616
287   55745715.2    193251444
288   38015504.2    119242137
289  116468886.4     98878373
290  131025939.9    159756460
291  147003988.2    133490201
297    3280122.3    144028991
298    2398596.5    140349498
299    2123418.6    136903409
301    2289427.7    141509444
302    2318129.5    140140369
303    2263264.3    135110986
304    2456117.0    140077707
305   13602576.8    135913083
306    3145059.1    137534375
307    2925087.6    138097142
308    2842998.4    133574318
310    2704743.6    127130998
311    2566929.4    122079817
312    2567188.7    118723217
313    2677574.2    120652798
314  107279721.5    112600313
315  122656372.0    112042926
316 1044824315.2    105078562
317 1614494241.2    159066974
318 1738274587.2    313138863
319 1617208465.7    394785547
320 1542035056.1    380419783
323 1070129493.0    511976610
324 1268248424.6    478024769
325 1524984575.0    639069141
326 2060900488.6    911054226
329 1545943034.7    997978324
330  266402246.5    304718811
331  110384370.8    110904826
332  111015922.0     43081071
333  346779608.7     57483585
334  331150294.3     46039094
339   45387966.5     24889396
340   96038621.1     24327447
342  300077481.0    115827952
344  342908439.3    151235413
345    4213146.1    145625141
346    3966911.8    124749028
347    2314957.4    130907202
348    2075689.4    113401240
349    2059344.9    104496512
350    2012869.4    106086239
351    1869981.0    129549142
352    2151772.4    134255185
353    2263262.4    133722501
354    2291190.2    132042154
355    2346542.8    132045156
356    2346306.8    128657439
357    2181060.1    129198838
358    2125970.7    125374224
361    2208609.7    114232445
362    3313505.2     87767106
364    4363859.3     92712712
365    7760730.6     95822594
366    8478959.3     88866769
367  134394049.7     25534185
368  231802639.4     25536466
369  381298948.9     48091289
370  749718559.6     33875716
371  863959024.0     40005620
372  592262676.2     26047451
373  392724539.4     16842350
374 1057314159.0     26626186
375 1091981416.4     49214718
376 1104499251.3     28794823
377  941569043.1     40644043
378  613379421.8    206794489
379 1364506832.9    275737322
380 2243000800.4   1020383280
381 1894472781.7    952568515
382  435205944.4    283937642
385  305716654.4    122152087
386  848578594.2    104308360
387  994094167.0    190789140
388 1180721005.4    219635531
389  674272352.1    169209369
390  830906036.3    167226124
391  533696791.4    138317988
392  475979240.8    118396883
393  329290668.2    137302141
394  850734068.1    171079711
395  846564304.5    225969203
396  740580814.8    475504362
397    5024835.4    340831336
398    1530541.9    259128570
399    1487483.6    225245261
400    1537779.0    212739066
401    1299007.9    200260401
402    1398589.7    190776938
403    1586410.3    177650481
404    1487936.0    177965165
405    1072057.5    125214566
406     909457.5    141071590
407     995313.6    132966708
408    1022884.7    137963240
409    2875656.9    174222514
410    2234363.6    174296308
411    3145318.4    214107684
412    3172908.9    216767469
413    2455555.6    277107023
414    2318094.4    389786969
415    2457858.1    498099321
416    2071610.5    417376439
417    4336914.1    346813246
418    4225912.6    299205521
419    3893272.5    269308695
420    4168522.6    310885686
421   33720279.1    370615397
422  157787470.7    331416916
423  719721919.2    272185807
424 1160118314.7    385616138
426 1300801343.3    435145324
429 1307667671.1    438274612
430 1454467059.5    253580607
431 2481987487.5   1040989531
432 2364563792.6   1225227104
433 2351592747.5   1252835025
434 2296206619.2   1093071796
435 1566249160.2    867070236
436 1047081513.6    340170423
439  984367040.3    317322411
440 1014452561.5    225564954
441  869373700.2    177871152
442  769468528.9    176725527
443  785858208.5    181415466
444  720226929.5    154720574
445  679013852.9    146137315
446  503064715.0    147048612
447  271837860.3    171039812
448  593002005.4    189534093"),header=T)
closeAllConnections()
plot(log10(DS.Load.filtered$CoolidgeLoad)~DS.Load.filtered$SP,type="l",lwd=2,col="red",ylim=c(5,ceiling(max(log10(DS.Load.filtered$CoolidgeLoad),na.rm=T))))
points(log10(DS.Load.filtered$JMLoad)~DS.Load.filtered$SP,type="l",lwd=2,col="blue")
Reply | Threaded
Open this post in threaded view
|

Re: Polygon Shading Based on Relative Line Position

emorway
The last sentence should read:

Due to the 20 or so times the lines cross each other I wasn't sure how to modify the last example in ?polygon
Reply | Threaded
Open this post in threaded view
|

Re: Polygon Shading Based on Relative Line Position

Joris FA Meys
Looking at the source code of the function diagwl() (which is used to
produce that one), the "shading" appears to be drawn using the function
segments(). Basically, the difference d between both lines is calculated,
and then the shading is done by using different lty and col when d is
positive or negative

# part of the code of diagwl
  pi <- approx(xl[1:n2],yl[1:n2],n=66)$y
  ti <- approx(x,c(tm[12],tm[1:12],tm[1]),n=66)$y
  d <- pi - ti
  xi <- (1:66)/5-0.7
  xw <- subset(xi,d>0) #periodo húmedo
  y1 <- subset(pi,d>0)
  y2 <- subset(ti,d>0)
  if(length(xw)>0) segments(xw,y1,xw,y2,col=pcol,lty=1,lwd=1)
  xw <- subset(xi,d<0) #periodo seco
  y1 <- subset(pi,d<0)
  y2 <- subset(ti,d<0)
  if(length(xw)>0) segments(xw,y1,xw,y2,col=tcol,lty=3,lwd=2)
# end part of the code

Maybe not exactly what you're looking for, but a nice work-around if you ask
me. It won't allow you to use full-colored surfaces though, but I can see a
similar reasoning working for that as well.

On Thu, May 6, 2010 at 4:32 PM, emorway <[hidden email]> wrote:

>
> The last sentence should read:
>
> Due to the 20 or so times the lines cross each other I wasn't sure how to
> modify the last example in ?polygon
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Polygon-Shading-Based-on-Relative-Line-Position-tp2132718p2132720.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


--
Joris Meys
Statistical Consultant

Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control

Coupure Links 653
B-9000 Gent

tel : +32 9 264 59 87
[hidden email]
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.