在 R 中创建一个箱线图,用样本大小 (N) 标记一个框

发布于 2024-09-14 04:40:06 字数 235 浏览 7 评论 0原文

有没有办法在 R 中创建一个箱线图,该箱线图将与框(某处)一起显示“N =(样本大小)”? varwidth 逻辑根据样本大小调整框的宽度,但这不允许在不同图之间进行比较。

FWIW,我按以下方式使用 boxplot 命令,其中“f1”是一个因素:

boxplot(xvar ~ f1, data=frame, xlab="input values", horizontal=TRUE)

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)

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

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

发布评论

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

评论(5

爱情眠于流年 2024-09-21 04:40:06

这是一些 ggplot2 代码。它将以样本平均值显示样本大小,使标签具有多功能性!

首先是 fun.data 的一个简单函数

give.n <- function(x){
   return(c(y = mean(x), label = length(x)))
}

现在,用钻石数据进行演示

ggplot(diamonds, aes(cut, price)) + 
   geom_boxplot() + 
   stat_summary(fun.data = give.n, geom = "text")

您可能需要调整文本大小以使其看起来不错,但现在您有一个样本大小的标签也给人一种倾斜的感觉。

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.

叶落知秋 2024-09-21 04:40:06

您可以使用 names 参数在每个因子名称旁边写入 n

如果您不想自己计算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, ")"))

You can use the names parameter to write the n next to each factor name.

If you don't want to calculate the n yourself you could use this little trick:

# 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, ")"))
梦里泪两行 2024-09-21 04:40:06

要获得条形图顶部的 n ,您可以将 text 与 boxplot 提供的 stat 详细信息结合使用,如下所示

b <- boxplot(xvar ~ f1, data=frame, plot=0)
text(1:length(b$n), b$stats[5,]+1, paste("n=", b$n))

b 的 stats 字段为
一个矩阵,每列包含一组/图的下须线的极值、下铰链、中值、上铰链和上须线的极值。

To get the n on top of the bar, you could use text with the stat details provided by boxplot as follows

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.

近箐 2024-09-21 04:40:06

gplots 包提供 boxplot.n,根据文档生成一个带有观察数量注释的箱线图

The gplots package provides boxplot.n, which according to the documentation produces a boxplot annotated with the number of observations.

2024-09-21 04:40:06

我找到了使用 Envstats 包的解决方法。该包需要使用以下方法下载、加载和激活:

library(Envstats)

stripChart(与 stripchart 不同)确实向图表添加了一些值,例如 n 值。首先,我绘制了箱线图。然后我在 stripChart 中使用了 add=T 。显然,很多东西都隐藏在 stripChart 代码中,因此它们不会显示在箱线图上。这是我用于 stripChart 隐藏大多数项目的代码。

Boxplot 带有集成的 stripChart 来显示 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")

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

然后 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")

你可以随时调整 的高点数字(n 个值),以便它们适合您想要的位置。

I figured out a workaround using the Envstats package. 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. First I plotted my boxplot. Then I used the add=T in the stripChart. Obviously, many things were hidden in the stripChart code so that they do not show up on the boxplot. Here is the code I used for the stripChart to hide most items.

Boxplot with integrated stripChart to show n values:

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(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(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.

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