标签包括 R 箱线图中使用的 cut 函数中的表达式

发布于 2024-09-18 08:21:52 字数 577 浏览 2 评论 0原文

我使用 cut 函数将数值变量转换为具有两个级别的因子,并在箱线图中使用它,如下所示:

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

我想在 cut 中使用的每个标签下方包含样本大小作为“n=...”功能。我可以使用带有子集的长度来获取样本大小,如下所示,

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

并使用 cat 和粘贴来获取标签下方的样本大小

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

我的问题是我无法将其插入到 cut 函数的 labels 参数中。简单地说,将上述内容插入到标签向量中可以正常打印箱线图,但会在 R 控制台中打印标签。我想我可能需要使用表达式函数,但我也没有让它工作。任何帮助或替代方法表示赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假情假意假温柔 2024-09-25 08:21:52

cat 函数不会连接,您应该使用 paste (是的,两次)。
用一个可重现的例子

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

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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文