views:

54

answers:

1

I'm using the cut function to convert a numeric variable into a factor with two levels and using this in a boxplot like this:

boxplot(Sp$Var1 ~ cut(Spt$Var5, breaks = c(0,50,100), labels =c("below 50%", "above 50%")), ...)

I want to include sample size as "n=..." below each of the labels used in the cut function. I can get the sample size using length with a subset, as this,

length(subset(Sp$Var1, SpDet$Var5<50)

And use cat and paste to get the sample size below the label

cat(paste("above 50%", "\n", "n =", length(subset(Sp$Var1, Sp$Var5<50)), sep=""))

My problem is that I've not been able to insert this into the labels argument of the cut function. Simply, inserting the above into the labels vector prints the boxplot ok, but prints the labels in the R console. I think I may need to use the expression function, but I haven't got this to work either. Any help or alternative methods appreciated.

+1  A: 

The cat function does not concatenate, you should use paste for that (yes, twice). With a reproducible example

y <- rnorm(20)
ns <- tapply(y,y>0,length)
labs <- paste(c("0 pr below", "above 0"), paste("n =",ns), sep="\n")
boxplot(y ~ cut(y, breaks=c(-Inf,0,Inf), labels=labs))
Aniko
Many thanks, this is what I wanted to do.
CCID