Gavin Simpson
2005-Apr-29 12:00 UTC
[R] Automating plot labelling in custom function in lapply() ?
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 -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [T] +44 (0)20 7679 5522 ENSIS Research Fellow [F] +44 (0)20 7679 7565 ENSIS Ltd. & ECRC [E] gavin.simpsonATNOSPAMucl.ac.uk UCL Department of Geography [W] http://www.ucl.ac.uk/~ucfagls/cv/ 26 Bedford Way [W] http://www.ucl.ac.uk/~ucfagls/ London. WC1H 0AP. %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Marc Schwartz
2005-Apr-29 13:16 UTC
[R] Automating plot labelling in custom function in lapply() ?
On Fri, 2005-04-29 at 13:00 +0100, Gavin Simpson wrote:> 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, > > GavGavin, To paraphrase John Fox from a recent thread, "this is one of those times where trying to avoid using a for() loop is counterproductive": oldpar <- par(mfrow = c(2,2), no.readonly = TRUE) for (i in 1:4) { plot(density(dat[, i]), main = colnames(dat)[i]) } par(oldpar) HTH, Marc Schwartz
Sundar Dorai-Raj
2005-Apr-29 13:17 UTC
[R] Automating plot labelling in custom function in lapply() ?
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, > > GavGav, 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