在R中创建一个boxplot,用样本大小(N)标记一个框, [英] create a boxplot in R that labels a box with the sample size (N)
问题描述
有没有一种方法可以在R中创建一个boxplot,并在框中(某处)显示N =(样本大小)? varwidth逻辑根据样本大小调整框的宽度,但不允许在不同图之间进行比较。
FWIW,我正在使用boxplot命令以下面的方式,'f1'是一个因素:
boxplot(xvar〜f1,data = frame,xlab =输入值,水平=真)
这里有一些ggplot2码。它会显示样本大小,使标签多功能!
首先,一个简单的函数 fun.data $ c($)
$ b
give.n < - function(x){
return(c(y = mean (x),label = length(x)))
}
现在,与钻石数据
ggplot(钻石,aes(cut,price))+
geom_boxplot()+
stat_summary(fun.data = give.n,geom =text)
您可能有玩文字大小,使它看起来不错,但现在你有一个样本大小的标签,这也给出了歪斜的感觉。
Is there a way to create a boxplot in R that will display with the box (somewhere) an "N=(sample size)"? The varwidth logical adjusts the width of the box on the basis of sample size, but that doesn't allow comparisons between different plots.
FWIW, I am using the boxplot command in the following fashion, where 'f1' is a factor:
boxplot(xvar ~ f1, data=frame, xlab="input values", horizontal=TRUE)
Here's some ggplot2 code. It's going to display the sample size at the sample mean, making the label multifunctional!
First, a simple function for fun.data
give.n <- function(x){
return(c(y = mean(x), label = length(x)))
}
Now, to demonstrate with the diamonds data
ggplot(diamonds, aes(cut, price)) +
geom_boxplot() +
stat_summary(fun.data = give.n, geom = "text")
You may have to play with the text size to make it look good, but now you have a label for the sample size which also gives a sense of the skew.
这篇关于在R中创建一个boxplot,用样本大小(N)标记一个框,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!