[R] Show name of dataset in graph
S Ellison
S.Ellison at lgc.co.uk
Thu May 7 15:35:28 CEST 2009
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
>>> <Richard.Cotton at hsl.gov.uk> 07/05/2009 13:26:46 >>>
> I?ve written a script to run several multivariate statistical
analysis
> automatically.
> As one result a biplot and screeplot is produced.
> Now I?d like to display the name of the inputdatset as part of the
title
of
> these graphics and I do not want to enter it each time I run the
script.
> How can I extract the name of a dataset?
> An (shortened) extraction from the script:
>
> >Test1 <- function(pri, sec, Pca, Ca, Dca){
> >#pri - primary Parameters (biological Dataset (abundances))
> >#sec ? secondary Parameters
> >#Pca, Ca, Dca ? Default=F, If set to TRUE these Methods are active
> ?several mathematical Operations?
> >biplot(pri, pc.biplot=T, main=paste(?PCA - ?, ???)?.
>
> Instead of the ??? I? d like to have a variable.
> For example if
> Test1(dataset.typ5, sec.typ5, Pca=TRUE)
> then the title of the plots should be
> ?PCA ? dataset.typ5?
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}}
More information about the R-help
mailing list