[R] Automating plot labelling in custom function in lapply() ?
Sundar Dorai-Raj
sundar.dorai-raj at pdf.com
Fri Apr 29 15:17:33 CEST 2005
Gavin Simpson wrote on 4/29/2005 5:00 AM:
> Dear List,
>
> Consider the following example:
>
> dat <- data.frame(var1 = rnorm(100), var2 = rnorm(100),
> var3 = rnorm(100), var4 = rnorm(100))
> oldpar <- par(mfrow = c(2,2), no.readonly = TRUE)
> invisible(lapply(dat,
> function(x) {
> plot(density(x),
> main = deparse(substitute(x))) }
> )
> )
> par(oldpar)
>
> I want to the main title in each of the density plots to be var1, var2,
> etc. The above code produces x[[1]], x[[2]] etc.
>
> What do I need to modify to be able to use the name of x as the plot
> label within in the above situation?
>
> Thanks in advance,
>
> Gav
Gav,
I think a `for' loop would be more useful here (not to mention, more
readable):
dat <- data.frame(var1 = rnorm(100), var2 = rnorm(100),
var3 = rnorm(100), var4 = rnorm(100))
oldpar <- par(mfrow = c(2,2), no.readonly = TRUE)
for(v in names(dat))
plot(density(dat[[v]]), main = v)
par(oldpar)
HTH,
--sundar
More information about the R-help
mailing list