[R] boxplot in subgroups

Annette Heisswolf annette.heisswolf at utu.fi
Thu Mar 26 07:59:18 CET 2009


Hei,

the error message below just tells you that you have specified nine places along the axis where you want to have labels (i.e. at=1:9) but 
obviously you have only specified three labels (i.e. labels = c("C","M","S")). Thus, R is complaining that there are not enough labels for 
all positions. However, I guess from your description that you want to have only three labels below your plot, one for each group of three 
boxes that share the same ASA1 value. Thus, you could for example do like this:

axis(side=1,at=c(2,5,8),labels=c("C","S","M"))

Then you would get the label always below the middle one of the three boxes of each group.

Now the boxes are, however, still distributed evenly across the plot, but in case you want to have those that belong to the same subgroup a 
bit closer together, you can specify their position via the "at" option on the boxplot function (see ?boxplot).

An example with random data might look like this:

ASA1=factor(rep(c("C","S","M"),each=100))
ASA2=factor(rep(c("C","S","M"),100))
C1_C2=runif(300)
boxplot(C1_C2~ASA2*ASA1,xlim=c(0,12),at=c(1:3,5:7,9:11),xaxt="n")
axis(side=1,at=c(2,6,10),labels=c("C","S","M"))

See ?axis for further options, e.g. tcl=0 in case you don't want to have a tick at the axis at the position of the label.

Hope that helps,

Annette

Pooja Jain schrieb:
> Hi,
> 
> I have data that looks like this:
> 
> ASA1    ASA2    C1_C2
> C       M       9.0225
> S       S       2.4315
> M       C       3.4894
> M       S       4.5282
> C       M       1.3183
> C       S       1.3735
> S       C       1.0488
> S       M       7.948
> M       C       4.5827
> 
> I need to plot Boxplots for a given ASA1 (either C,S, or M) with respect 
> to C1_C2. However, instead of one boxplot I want to plot three boxplots 
> for a given ASA1 value such that each sub-boxplot represent the 
> distribution for C1-C2 for each of the three possible values of ASA2 
> (C,S or M). I want to show this subgroup with a single xtics (either C, 
> S or M) corresponding to the ASA1 value selected.
> 
> I tried to do it, but ending with the following error. I am not sure how 
> exactly I should build the object to plot as subgroup.
> 
> 
> Error in axis(side = 1, at = 1:9, labels = c("C", "M", "S" :
>   'at' and 'labels' lengths differ, 9 != 3
> In addition: Warning message:
> In bxp(list(stats = c(1e-04, 2.82745, 6.0193, 10.5957, 22.2476,  :
>   some notches went outside hinges ('box'): maybe set notch=FALSE
> 
> 
> I would greatly appreciate any help in this matter.
> 
> Thank you.
> 
> 
> -Kanu

-- 
Annette Heisswolf
Section of Ecology
Department of Biology
University of Turku
20014 Turku, Finland

phone	+ 358 2 333 6006
fax	+ 358 2 333 6550
mail	annette.heisswolf at utu.fi




More information about the R-help mailing list