[R] Aligning Grid Lines with Date Ticks in Trellis Plot

Elliot Joel Bernstein elliot.bernstein at fdopartners.com
Wed Nov 24 15:59:44 CET 2010


I know this issue has been discussed before, but I hoped the advent of pretty.Date would resolve it. In the following code, the first plot produces misaligned vertical grid lines, while in the second plot they are properly aligned. Is there any way to get something along the lines of the first call to produce properly aligned grid lines?

X <- data.frame(date=seq(as.Date("1990/01/01"), as.Date("2010/11/24"), by=1))                                                                                                      
X$value <- rnorm(length(X$date))                                                                                                                                                   
                                                                                                                                                                                   
## This produces grid lines not aligned with the date ticks                                                                                                                        
                                                                                                                                                                                   
xyplot(value ~ date, data=X,                                                                                                                                                       
       panel = function(...) {                                                                                                                                                     
         panel.grid(v=-1,h=-1,col="black")                                                                                                                                         
         panel.xyplot(...)                                                                                                                                                         
       })                                                                                                                                                                          
                                                                                                                                                                                   
## This produces grid lines aligned with the date ticks                                                                                                                            
                                                                                                                                                                                   
xyplot(value ~ date, data=X,                                                                                                                                                       
       panel = function(...) {                                                                                                                                                     
         panel.abline(v=pretty(X$date, 5))                                                                                                                                         
         panel.grid(v=0,h=-1,col="black")                                                                                                                                          
         panel.xyplot(...)                                                                                                                                                         
       })                                        

Thanks.

- Elliot



More information about the R-help mailing list