Re: [R] lapply and boxplots with variable names

From: Shawn Morrison <shawn.morrison_at_dryasresearch.com>
Date: Tue, 22 Jun 2010 12:55:19 -0600

Many thanks Phil, that does help.

If I could ask a follow-up, how do I put each plot in its own device window? Right now, all I get is the boxplot for var2 (var1 gets overwritten?). I tried putting quartz() before the boxplot command but got an error message.

Cheers,
Shawn

On 22/06/10 11:13 AM, Phil Spector wrote:
> Shawn -
> Does this example help? (Please don't use cbind when creating
> a data frame, since it first creates a matrix, which means everything
> must be of the same mode.)
>
>> var1 = rnorm(1000)
>> var2 = rnorm(1000)
>> TimePeriod = rep((LETTERS[1:4]), 250)
>> my.data = data.frame(var1,var2,TimePeriod)
>> lapply(names(my.data)[1:2],
> + function(y)boxplot(formula(paste(y,'TimePeriod',sep='~')),
> + main=y,data=my.data))
>
>
> - Phil Spector
> Statistical Computing Facility
> Department of Statistics
> UC Berkeley
> spector_at_stat.berkeley.edu
>
>
> On Tue, 22 Jun 2010, Shawn Morrison wrote:
>
>> Hi all,
>>
>> I have a dataset with several variables, each of which is a separate
>> column. For each variable, I want to produce a boxplot and include
>> the name of the variable (ie, column name) on each plot.
>>
>> I have included a sample dataset below. Can someone tell me where I
>> am going wrong?
>>
>> Thank you for your help,
>> Shawn Morrison
>>
>> # Generate a sample dataset
>> var1 = rnorm(1000)
>> var2 = rnorm(1000)
>> TimePeriod = rep((LETTERS[1:4]), 250)
>>
>> my.data = as.data.frame(cbind(var1, var2, TimePeriod)); summary(my.data)
>> attach(my.data)
>>
>> # Create box plots for var1 and var2 using TimePeriod on the x-axis
>> lapply(my.data[,1:2], function(y) {
>> boxplot(y~TimePeriod,
>> main = y
>> data = my.data)
>> })
>>
>> ______________________________________________
>> R-help_at_r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue 22 Jun 2010 - 19:04:38 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 22 Jun 2010 - 19:30:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive