[R] Plotting dates and grid lines on the X-Axis of xyplot

demac jwingard at demac.com
Thu Sep 9 22:26:37 CEST 2010


I have a plotting function that is plots a multi-panel plot, with the x-axis
as a date and various y-axes.
I would like to control the frequency of the X-axis labels, ticks and grid
lines. However with the following code I get no annotation on the X-axis at
all.

Here is a minimal data.frame

         Case Days Well Stream       Value
1    Observed    0  P-1    Oil 1000.807785
2    Observed   31  P-1    Oil  998.378103
3    Observed   60  P-1    Oil  974.083290
4    Observed   91  P-1    Oil  972.484199
5    Observed  121  P-1    Oil  953.325810
6    Observed  152  P-1    Oil  939.349538
7    Observed  182  P-1    Oil  927.391942
8    Observed  213  P-1    Oil  915.837406
9    Observed  244  P-1    Oil  902.613395
10   Observed  274  P-1    Oil  896.191278
11   Observed  305  P-1    Oil  879.572936
12   Observed  335  P-1    Oil  873.241785
13   Observed  366  P-1    Oil  872.532526
14   Observed  397  P-1    Oil  844.608688
15   Observed  425  P-1    Oil  845.666635
16   Observed  456  P-1    Oil  832.753743
17   Observed  486  P-1    Oil  815.536647
18   Observed  517  P-1    Oil  809.317984
19   Observed  547  P-1    Oil  800.057954
20   Observed  578  P-1    Oil  786.320913
21   Observed  609  P-1    Oil  782.945092
22   Observed  639  P-1    Oil  769.241908
23   Observed  670  P-1    Oil  760.265467
24   Observed  700  P-1    Oil  749.068712
25   Observed  731  P-1    Oil  735.676912
26   Observed  762  P-1    Oil  733.704963
27   Observed  790  P-1    Oil  719.816182
28   Observed  821  P-1    Oil  719.418231
29   Observed  851  P-1    Oil  706.352434
30   Observed  882  P-1    Oil  695.619862
31   Observed  912  P-1    Oil  685.927100
32   Observed  943  P-1    Oil  681.596321
33   Observed  974  P-1    Oil  678.068159
241    Case 1    0  P-1    Oil  607.119101
242    Case 1   31  P-1    Oil  605.645185
243    Case 1   60  P-1    Oil  590.907245
244    Case 1   91  P-1    Oil  589.937191
245    Case 1  121  P-1    Oil  578.315155
246    Case 1  152  P-1    Oil  569.836742
247    Case 1  182  P-1    Oil  562.582916
248    Case 1  213  P-1    Oil  555.573599
249    Case 1  244  P-1    Oil  547.551529
250    Case 1  274  P-1    Oil  543.655686
251    Case 1  305  P-1    Oil  533.574517
252    Case 1  335  P-1    Oil  529.733857
253    Case 1  366  P-1    Oil  529.303599
254    Case 1  397  P-1    Oil  512.364188
255    Case 1  425  P-1    Oil  513.005969
256    Case 1  456  P-1    Oil  505.172633
257    Case 1  486  P-1    Oil  494.728242
258    Case 1  517  P-1    Oil  490.955820
259    Case 1  547  P-1    Oil  485.338417
260    Case 1  578  P-1    Oil  477.005129
261    Case 1  609  P-1    Oil  474.957258
262    Case 1  639  P-1    Oil  466.644507
263    Case 1  670  P-1    Oil  461.199138
264    Case 1  700  P-1    Oil  454.406860
265    Case 1  731  P-1    Oil  446.283005
266    Case 1  762  P-1    Oil  445.086764
267    Case 1  790  P-1    Oil  436.661425
268    Case 1  821  P-1    Oil  436.420016
269    Case 1  851  P-1    Oil  428.493924
270    Case 1  882  P-1    Oil  421.983234
271    Case 1  912  P-1    Oil  416.103322
272    Case 1  943  P-1    Oil  413.476146
273    Case 1  974  P-1    Oil  411.335860
481    Case 2    0  P-1    Oil  527.609448
482    Case 2   31  P-1    Oil  526.328560
483    Case 2   60  P-1    Oil  513.520733
484    Case 2   91  P-1    Oil  512.677718
485    Case 2  121  P-1    Oil  502.577730
486    Case 2  152  P-1    Oil  495.209669
487    Case 2  182  P-1    Oil  488.905820
488    Case 2  213  P-1    Oil  482.814458
489    Case 2  244  P-1    Oil  475.842977
490    Case 2  274  P-1    Oil  472.457342
491    Case 2  305  P-1    Oil  463.696424
492    Case 2  335  P-1    Oil  460.358745
493    Case 2  366  P-1    Oil  459.984836
494    Case 2  397  P-1    Oil  445.263847
495    Case 2  425  P-1    Oil  445.821579
496    Case 2  456  P-1    Oil  439.014114
497    Case 2  486  P-1    Oil  429.937543
498    Case 2  517  P-1    Oil  426.659166
499    Case 2  547  P-1    Oil  421.777430
500    Case 2  578  P-1    Oil  414.535488
501    Case 2  609  P-1    Oil  412.755810
502    Case 2  639  P-1    Oil  405.531716
503    Case 2  670  P-1    Oil  400.799484
504    Case 2  700  P-1    Oil  394.896738
505    Case 2  731  P-1    Oil  387.836801
506    Case 2  762  P-1    Oil  386.797222
507    Case 2  790  P-1    Oil  379.475284
508    Case 2  821  P-1    Oil  379.265491
509    Case 2  851  P-1    Oil  372.377417
510    Case 2  882  P-1    Oil  366.719381
511    Case 2  912  P-1    Oil  361.609516
512    Case 2  943  P-1    Oil  359.326400
513    Case 2  974  P-1    Oil  357.466411
2401 Observed    0  P-1  Water    0.000000
2402 Observed   31  P-1  Water    0.000000
2403 Observed   60  P-1  Water    0.000000
2404 Observed   91  P-1  Water    0.000000
2405 Observed  121  P-1  Water    0.000000
2406 Observed  152  P-1  Water    0.000000
2407 Observed  182  P-1  Water    0.000000
2408 Observed  213  P-1  Water    0.000000
2409 Observed  244  P-1  Water    0.000000
2410 Observed  274  P-1  Water   17.552503
2411 Observed  305  P-1  Water   36.738416
2412 Observed  335  P-1  Water   54.021215
2413 Observed  366  P-1  Water   66.311462
2414 Observed  397  P-1  Water   88.733529
2415 Observed  425  P-1  Water  101.520756
2416 Observed  456  P-1  Water  112.487415
2417 Observed  486  P-1  Water  127.618004
2418 Observed  517  P-1  Water  131.975539
2419 Observed  547  P-1  Water  163.789654
2420 Observed  578  P-1  Water  184.113855
2421 Observed  609  P-1  Water  188.285539
2422 Observed  639  P-1  Water  201.018004
2423 Observed  670  P-1  Water  205.401133
2424 Observed  700  P-1  Water  221.661059
2425 Observed  731  P-1  Water  242.460137
2426 Observed  762  P-1  Water  237.244107
2427 Observed  790  P-1  Water  254.144911
2428 Observed  821  P-1  Water  273.470766
2429 Observed  851  P-1  Water  273.679008
2430 Observed  882  P-1  Water  269.876483
2431 Observed  912  P-1  Water  305.409405
2432 Observed  943  P-1  Water  332.370923
2433 Observed  974  P-1  Water  337.689196
2641   Case 1    0  P-1  Water    0.000000
2642   Case 1   31  P-1  Water    0.000000
2643   Case 1   60  P-1  Water    0.000000
2644   Case 1   91  P-1  Water    0.000000
2645   Case 1  121  P-1  Water    0.000000
2646   Case 1  152  P-1  Water    0.000000
2647   Case 1  182  P-1  Water    0.000000
2648   Case 1  213  P-1  Water    0.000000
2649   Case 1  244  P-1  Water    0.000000
2650   Case 1  274  P-1  Water    9.921527
2651   Case 1  305  P-1  Water   20.766337
2652   Case 1  335  P-1  Water   30.535415
2653   Case 1  366  P-1  Water   37.482459
2654   Case 1  397  P-1  Water   50.156500
2655   Case 1  425  P-1  Water   57.384462
2656   Case 1  456  P-1  Water   63.583350
2657   Case 1  486  P-1  Water   72.135894
2658   Case 1  517  P-1  Water   74.598985
2659   Case 1  547  P-1  Water   92.581868
2660   Case 1  578  P-1  Water  104.070093
2661   Case 1  609  P-1  Water  106.428132
2662   Case 1  639  P-1  Water  113.625139
2663   Case 1  670  P-1  Water  116.102697
2664   Case 1  700  P-1  Water  125.293597
2665   Case 1  731  P-1  Water  137.050246
2666   Case 1  762  P-1  Water  134.101893
2667   Case 1  790  P-1  Water  143.655048
2668   Case 1  821  P-1  Water  154.578959
2669   Case 1  851  P-1  Water  154.696668
2670   Case 1  882  P-1  Water  152.547296
2671   Case 1  912  P-1  Water  172.632230
2672   Case 1  943  P-1  Water  187.872189
2673   Case 1  974  P-1  Water  190.878336
2881   Case 2    0  P-1  Water    0.000000
2882   Case 2   31  P-1  Water    0.000000
2883   Case 2   60  P-1  Water    0.000000
2884   Case 2   91  P-1  Water    0.000000
2885   Case 2  121  P-1  Water    0.000000
2886   Case 2  152  P-1  Water    0.000000
2887   Case 2  182  P-1  Water    0.000000
2888   Case 2  213  P-1  Water    0.000000
2889   Case 2  244  P-1  Water    0.000000
2890   Case 2  274  P-1  Water    8.946559
2891   Case 2  305  P-1  Water   18.725672
2892   Case 2  335  P-1  Water   27.534763
2893   Case 2  366  P-1  Water   33.799135
2894   Case 2  397  P-1  Water   45.227725
2895   Case 2  425  P-1  Water   51.745410
2896   Case 2  456  P-1  Water   57.335146
2897   Case 2  486  P-1  Water   65.047249
2898   Case 2  517  P-1  Water   67.268297
2899   Case 2  547  P-1  Water   83.484039
2900   Case 2  578  P-1  Water   93.843341
2901   Case 2  609  P-1  Water   95.969660
2902   Case 2  639  P-1  Water  102.459432
2903   Case 2  670  P-1  Water  104.693525
2904   Case 2  700  P-1  Water  112.981254
2905   Case 2  731  P-1  Water  123.582602
2906   Case 2  762  P-1  Water  120.923977
2907   Case 2  790  P-1  Water  129.538364
2908   Case 2  821  P-1  Water  139.388805
2909   Case 2  851  P-1  Water  139.494947
2910   Case 2  882  P-1  Water  137.556790
2911   Case 2  912  P-1  Water  155.668018
2912   Case 2  943  P-1  Water  169.410378
2913   Case 2  974  P-1  Water  172.121117

And here is the code that I am using

simPlot<-function(theData, theWell, start="2000-01-01", obs="Observed",
streams, 
				order=(1:nStreams), heights=1, headings=streams, xby="years", ...){

# process streams		
	if(missing(streams)) {
		pData <- subset(theData, theData$Well==theWell)
		streams<-levels(pData$Stream)
	} else {
		pData <- subset(theData, theData$Well==theWell & (theData$Stream %in%
streams))
	}
	nStreams <- length(streams)

# process cases
	pData$Case<-relevel(pData$Case, ref=obs)
	theCases <- levels(pData$Case)
	nCases <- nlevels(pData$Case)
	theObsCase <- which(theCases==obs)[1]
	types<-ifelse(1:nCases==theObsCase,"p","l")
	theLegendLines <- 1:nCases==theObsCase
	

myFill.color=c("red","orange","ForestGreen","black","blue","DodgerBlue")[pData$Stream]
	myPch.shape=c(23,24,21,22,15:19)[pData$Stream]
	theDates<-as.Date(pData$Days,origin=start)
	x.marks<-prettyDates(theDates, origin=start, by=xby)
	print(x.marks)

	xyplot(Value~theDates | Stream, 
		data=pData, groups=Case, type=types, distribute.type=T,
		fill.color=myFill.color, pch.shape=myPch.shape, marks=x.marks,
		panel=function(x, y, fill.color, pch.shape, marks, ..., subscripts) {
			fill<-fill.color[subscripts]
			pch<-pch.shape[subscripts]
			panel.xyplot(x, y, fill=fill, pch=pch, subscripts=subscripts, ...)
			panel.grid(h = -1, v = 0)
			panel.abline(v=marks$at)
		},
		index.cond=list(order),
		strip=strip.custom(factor.levels=headings),
		main=sprintf("Performance Plot\n%s Well\nRhourde El Baguel
Field",theWell),
		layout=c(1,nStreams),
		scales=list(
			x=list(relation="same",at=x.marks$at, labels=x.marks$labels),
			y=list(relation="free")),
		par.settings=list(
			layout.heights=list(panel=rev(heights)), 
			superpose.line=list(col=brewer.pal(nCases,"Set1")),
			superpose.symbol=list(col="black")),
		auto.key=list(side="top", columns=min(5,nCases), points=F, lines=T),
		xlab="Date", ylab=NULL, ...)
}

prettyDates<-function(x,origin="1970-01-01", by="years"){
	s<-range(as.integer(format(x,'%Y')))
	d1<-as.Date(sprintf("%d-01-01",s[1]))
	d2<-as.Date(sprintf("%d-01-01",s[2]+1))
	d<-seq(as.Date(d1,origin=origin),to=as.Date(d2,origin=origin),by=by)
	list(x=as.numeric(d),lab=format(d,'%Y'))
}

The call

> simPlot(t,"P-1",streams=c("Oil","Water"))
$x
[1] 10957 11323 11688 12053

$lab
[1] "2000" "2001" "2002" "2003"

shows the correct "at" and "labels" values, but there is no annotation,
except for the xlab at the bottom of the plot.

What do I need to change to get the grid-lines, ticks, and labels from my
prettyDates function on the plot?

Thanks in advance

Jeffrey

-- 
View this message in context: http://r.789695.n4.nabble.com/Plotting-dates-and-grid-lines-on-the-X-Axis-of-xyplot-tp2533549p2533549.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list