开发者

labels including an expression in cut function used in a boxplot in R

开发者 https://www.devze.com 2023-01-14 23:22 出处:网络
I\'m using the cut function to convert a numeric variab开发者_JAVA百科le into a factor with two levels and using this in a boxplot like this:

I'm using the cut function to convert a numeric variab开发者_JAVA百科le 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.


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))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号