[R] How to stamp my graphs with date and time

David Winsemius dwinsemius at comcast.net
Sun Apr 25 03:00:29 CEST 2010


On Apr 24, 2010, at 6:26 PM, Jun Shen wrote:

> Hi, David,
>
> Thanks. It is working perfectly now. But I still don't understand  
> the argument "page", I couldn't find it in text() or par().

text and par are base graphics functions. page is an argument to  
lattice functions:

  (Page 26 of Sarkar;'s book says):

"Another type of annotation directly supported by lattice functions is  
through the page argument. If specified, it has to be a function, and  
is called after each page is drawn. It can be used, for example to  
mark the page numbers in a multipage display."

The code for Figure 12.1 uses it to locate a legend. (See the website.)


> Also in the "page = function(n){......}" the argument "n" does not  
> appear anywhere and it seems OK to be replaced with any other  
> letter. Can anyone explain it a little bit?

That "n" could be replaced, not just with any single letter but with  
any string, as long as it was used properly (or not used at all)  
within the function body. That's just programming. Given what I know  
about R programming (not a huge amount) , I strongly suspect you could  
leave it out all together. Lot's of R functions have no formal  
arguments.


>
> Jun
>
> On Sat, Apr 24, 2010 at 4:14 PM, David Winsemius <dwinsemius at comcast.net 
> > wrote:
>
> On Apr 24, 2010, at 4:52 PM, Jun Shen wrote:
>
> Hi,
>
> I also want to show user's name at the lower-left corner. So I added  
> one more line to Deepayan's code but it didn't work. I am not sure  
> how to use the "page" argument here. Appreciate any comment.
>
> lattice.options(default.args = list(page = function(n) {
>   panel.text(lab = sprintf("%s", date()), x = 0.99, y = 0.02, adj = 1)
>   panel.text(lab = sprintf("%s",  
> paste("User:",Sys.getenv("USERNAME"))), x = 0.01, y = 0.02)
> }))
>
>
> Using 0 for the adj argument:
>
>
> lattice.options(default.args = list(page = function(n) {
>    panel.text(lab = sprintf("%s", date()), x = 0.99, y = 0.02, adj =  
> 1)
>    panel.text(lab = sprintf("%s",  
> paste("User:",Sys.getenv("USERNAME"))),
>                x = 0.01, y = 0.02, adj=0)
>  }))
>
> > xyplot(1~1)  # works
>
> Seems to work.  Panel.text did not help me, but it pointed to the  
> base graphics functions  which I interpreted to be text() and in the  
> Details section it does.
>
> -- 
> David
>
>
> Jun
>
> On Fri, Apr 23, 2010 at 10:32 AM, Deepayan Sarkar <deepayan.sarkar at gmail.com 
> > wrote:
> On Wed, Apr 21, 2010 at 4:02 PM, David Winsemius <dwinsemius at comcast.net 
> > wrote:
> >
> > On Apr 21, 2010, at 6:58 PM, David Winsemius wrote:
> >
> >> Sarkar offers a worked example of taking user input regarding  
> location for
> >> locating a grid viewport outside the plot area.
> >>
> >> http://lmdvr.r-forge.r-project.org/figures/figures.html
> >>
> >> See Figure 12.1
> >>
> >> state <- data.frame(state.x77, state.region)
> >> trellis.vpname("xlab", prefix = "plot1")
> >> trellis.vpname("strip", column = 2, row = 2, prefix = "plot2")
> >>
> >>
> >> data(Chem97, package = "mlmRev")
> >> qqmath(~ gcsescore | factor(score), Chem97, groups = gender,
> >>      f.value = function(n) ppoints(100),
> >>      aspect = "xy",
> >>      page = function(n) {
> >>          cat("Click on plot to place legend", fill = TRUE)
> >>          ll <- grid.locator(unit = "npc")
> >>          if (!is.null(ll))
> >>              draw.key(simpleKey(levels(factor(Chem97$gender))),
> >>                       vp = viewport(x = ll$x, y = ll$y),
> >>                       draw = TRUE)
> >>      })
> >>
> >
> > Furthermore when I try:
> >
> > mtext(date(), side=3, line=4, adj=0)
> >
> > .... I get a datetime stamp even though I am mixing graphic  
> paradigms. This
> > would seem to be one of those instances where you needn't worry  
> about
> > getting the internal coordinates to match up.
>
> Appearances can be deceiving. Try to create a PDF file with this, and
> you will get
>
> > pdf()
> > xyplot(1 ~ 1)
> > mtext(date(), side=1, line=4, adj=0)
> Error in mtext(date(), side = 1, line = 4, adj = 0) :
>  plot.new has not been called yet
> > dev.off()
>
> Try to copy the screen device to PDF, and you will get
>
> > dev.copy(pdf, file = "/tmp/foo.pdf")
> Error in dev.copy(pdf, file = "/tmp/foo.pdf") : invalid graphics state
>
> For a footnote in lattice plots, page is the appropriate argument to
> use. Adapting an example from the book (section 9.3), the following
> will add a timestamp to all subsequent lattice plots:
>
> lattice.options(default.args = list(page = function(n) {
>   panel.text(lab = sprintf("%s", date()), x = 0.99, y = 0.05, adj = 1)
> }))
>
> xyplot(1 ~ 1)
>
>
> -Deepayan
>
>
> David Winsemius, MD
> West Hartford, CT
>
>

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list