标签包括 R 箱线图中使用的 cut 函数中的表达式
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
cat
函数不会连接,您应该使用paste
(是的,两次)。用一个可重现的例子
The
cat
function does not concatenate, you should usepaste
for that (yes, twice).With a reproducible example