[英]create a boxplot in R that labels a box with the sample size (N)
Is there a way to create a boxplot in R that will display with the box (somewhere) an "N=(sample size)"? 有没有办法在R中创建一个框图,它将在框中显示(某处)“N =(样本大小)”? The varwidth logical adjusts the width of the box on the basis of sample size, but that doesn't allow comparisons between different plots.
varwidth logical根据样本大小调整框的宽度,但不允许在不同的图之间进行比较。
FWIW, I am using the boxplot command in the following fashion, where 'f1' is a factor: FWIW,我以下列方式使用boxplot命令,其中'f1'是一个因素:
boxplot(xvar ~ f1, data=frame, xlab="input values", horizontal=TRUE)
Here's some ggplot2 code. 这是一些ggplot2代码。 It's going to display the sample size at the sample mean, making the label multifunctional!
它将在样本均值处显示样本大小,使标签多功能化!
First, a simple function for fun.data
首先,
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. 您可能需要使用文本大小来使其看起来很好,但现在您有一个样本大小的标签,这也给出了倾斜感。
You can use the names
parameter to write the n
next to each factor name. 您可以使用
names
参数在每个因子名称旁边写入n
。
If you don't want to calculate the n
yourself you could use this little trick: 如果你不想自己计算
n
你可以使用这个小技巧:
# Do the boxplot but do not show it
b <- boxplot(xvar ~ f1, data=frame, plot=0)
# Now b$n holds the counts for each factor, we're going to write them in names
boxplot(xvar ~ f1, data=frame, xlab="input values", names=paste(b$names, "(n=", b$n, ")"))
To get the n
on top of the bar, you could use text
with the stat
details provided by boxplot as follows 要获得条形图顶部的
n
,您可以使用带有boxplot提供的stat
详细信息的text
,如下所示
b <- boxplot(xvar ~ f1, data=frame, plot=0)
text(1:length(b$n), b$stats[5,]+1, paste("n=", b$n))
The stats field of b is a matrix, each column contains the extreme of the lower whisker, the lower hinge, the median, the upper hinge and the extreme of the upper whisker for one group/plot. b的统计字段是矩阵,每列包含下部晶须的极端,下部铰链,中间,上部铰链和上部晶须的极端,用于一组/图。
gplots
包提供了boxplot.n
,根据文档生成一个用观察数量注释的 boxplot.n
。
I figured out a workaround using the Envstats package. 我找到了使用Envstats包的解决方法。 This package needs to be downloaded, loaded and activated using:
需要使用以下命令下载,加载和激活此包:
library(Envstats)
The stripChart (different from stripchart) does add to the chart some values such as the n values. stripChart(与stripchart不同)确实会向图表添加一些值,例如n值。 First I plotted my boxplot.
首先,我绘制了我的箱线图。 Then I used the add=T in the stripChart.
然后我在stripChart中使用了add = T. Obviously, many things were hidden in the stripChart code so that they do not show up on the boxplot.
显然,很多东西都隐藏在stripChart代码中,因此它们不会出现在boxplot上。 Here is the code I used for the stripChart to hide most items.
这是我用于stripChart隐藏大多数项目的代码。
Boxplot with integrated stripChart to show n values: 箱线集成带状图显示n个值:
stripChart(data.frame(T0_G1,T24h_G1,T96h_G1,T7d_G1,T11d_G1,T15d_G1,T30d_G1), show.ci=F,axes=F,points.cex=0,n.text.line=1.6,n.text.cex=0.7,add=T,location.scale.text="none")
So boxplot 所以boxplot
boxplot(data.frame(T0_G1,T24h_G1,T96h_G1,T7d_G1,T11d_G1,T15d_G1,T30d_G1),main="All Rheometry Tests on Egg Plasma at All Time Points at 0.1Hz,0.1% and 37 Set 1,2,3", names=c("0h","24h","96h","7d ", "11d", "15d", "30d"),boxwex=0.6,par(mar=c(8,4,4,2)))
Then stripChart 然后stripChart
stripChart(data.frame(T0_G1,T24h_G1,T96h_G1,T7d_G1,T11d_G1,T15d_G1,T30d_G1), show.ci=F,axes=F,points.cex=0,n.text.line=1.6,n.text.cex=0.7,add=T,location.scale.text="none")
You can always adjust the high of the numbers (n values) so that they fit where you want. 您可以随时调整数字的高位(n值),使它们适合您想要的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.