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)
})
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.
>
Hello Shawn,
Does this do what you want? I'm assuming you want to look at each
plot, so I added a call to par().
#######
my.data <- data.frame(var1=rnorm(1000), var2=rnorm(1000),
TimePeriod=factor(rep((LETTERS[1:4]), 250)))
str(my.data)
lapply(names(my.data[ , 1:2]), function(y) {
old.par <- par(no.readonly = TRUE)
on.exit(par(old.par))
par("ask"=TRUE)
boxplot(my.data[, y] ~ my.data[, "TimePeriod"],
main = y)
})
######
HTH,
Josh
On Tue, Jun 22, 2010 at 9:45 AM, Shawn Morrison
<shawn.morrison at dryasresearch.com> 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.
>
--
Joshua Wiley
Ph.D. Student
Health Psychology
University of California, Los Angeles
Try this: library(lattice) bwplot(values ~ TimePeriod | ind, cbind(stack(my.data), TimePeriod my.data$TimePeriod)) On Tue, Jun 22, 2010 at 1:45 PM, Shawn Morrison < shawn.morrison@dryasresearch.com> 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@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]