[R] several boxplots or bwplots into one graphic

Chuck Cleland ccleland at optonline.net
Tue Jan 25 11:23:31 CET 2005


How about something like this?

mydata <- matrix(runif(180), ncol=9)
mydata <- as.data.frame(mydata)
mydata$V10 <- rnorm(20, 50, 10)
mydata$GROUP <- as.factor(rep(c("G1","G2"), c(10,10)))

par(mfrow=c(2,5))

for(i in 1:10){
boxplot(mydata[,i] ~ mydata$GROUP, main=names(mydata)[i])
}

hope it helps,

Chuck Cleland

Christoph Lehmann wrote:
> many thanks for your great tip. I didn't know reshape.
> 
> Unfortunately in my real data, the values of my variables are ont all 
> within the same range. Therefore, what shall I change in the code to get 
> for each plot a scale, which is adjusted to the range of my variable?
> 
> thanks a lot
> 
> christoph
> 
> Chuck Cleland wrote:
> 
>>   Your variables (var.*) seem to be on the same scale.  How about 
>> reshaping the data into a univariate layout and then using bwplot as 
>> follows:
>>
>> mydata <- data.frame(ID = 1:20, A = runif(20), B = runif(20),
>>                      C = runif(20), GROUP = rep(c(0,1), c(10,10)))
>>
>> mydata.uni <- reshape(mydata, varying = list(c("A", "B", "C")),
>>                       v.names = "Y", timevar = "VAR", times = c("A",
>>                       "B", "C"), direction = "long")
>>
>> library(lattice)
>>
>> bwplot(Y ~ as.factor(GROUP) | VAR, data = mydata.uni, layout=c(3,1,1),
>>        xlab="Group")
>>
>> hope this helps,
>>
>> Chuck Cleland
>>
>> Christoph Lehmann wrote:
>>
>>> Hi
>>> I have 10 variables and 2 groups. I know how to plot a bwplot for ONE 
>>> var. e.g.
>>>
>>> var.a var.b var.c .. GROUP
>>> 0.2   0.5   0.2   .. 0
>>> 0.3   0.2   0.2   .. 0
>>> ..
>>> 0.1   0.8   0.7   .. 1
>>> 0.5   0.5   0.1   .. 1
>>> ..
>>>
>>>
>>> bwplot(var.a ~ GROUP, data = my.data)
>>>
>>> How can I plot 10 bwplots (or boxplots) automatically into one 
>>> graphic? is there any function from lattice which can do this?
>>>
>>> thanks for a short hint
>>>
>>> christoph
>>>
>>> ______________________________________________
>>> R-help at stat.math.ethz.ch mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide! 
>>> http://www.R-project.org/posting-guide.html
>>>
>>
> 

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 452-1424 (M, W, F)
fax: (917) 438-0894




More information about the R-help mailing list