Skip to content
Prev 179580 / 398503 Next

Show name of dataset in graph

The usual way of doing this in R functions is via deparse, as in the
examples in ?deparse:

fd<-function(x) plot(x, main=deparse(substitute(x)))
q<-1:10
fd(q)

as.character works for simple cases but is probably not the best
option. Compare 

fd(log(q)) 

with

fc<-function(x) plot(x, main=as.character(substitute(x)))
fc(log(q))

In the case of an expression, substitute returns a 'call' object, and
applying as.character to that returns the individual elements of the
call ("log" and "q" in this case) and that is plotted as a multi-line
string. deparse() returns a the character string "log(q)" which is what
you normally want.

Steve E
analysis
title 
of
script.
This demonstrates the basic idea:

dfr1 <- data.frame(x=1:10, y=runif(10))
dfr2 <- data.frame(x=11:20, y=runif(10))

library(lattice)
myplot <- function(dfr)
{
   xyplot(y~x, 
   data=dfr, 
   main=as.character(substitute(dfr))) 
}

myplot(dfr1)
myplot(dfr2)

Regards,
Richie.

Mathematical Sciences Unit
HSL

------------------------------------------------------------------------
ATTENTION:

This message contains privileged and confidential\ infor...{{dropped:19}}