在 R 中创建一个箱线图,用样本大小 (N) 标记一个框
有没有办法在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一些 ggplot2 代码。它将以样本平均值显示样本大小,使标签具有多功能性!
首先是
fun.data
的一个简单函数现在,用钻石数据进行演示
您可能需要调整文本大小以使其看起来不错,但现在您有一个样本大小的标签也给人一种倾斜的感觉。
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
Now, to demonstrate with the diamonds data
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.
您可以使用
names
参数在每个因子名称旁边写入n
。如果您不想自己计算
n
,您可以使用这个小技巧:You can use the
names
parameter to write then
next to each factor name.If you don't want to calculate the
n
yourself you could use this little trick:要获得条形图顶部的
n
,您可以将text
与 boxplot 提供的stat
详细信息结合使用,如下所示b 的 stats 字段为
一个矩阵,每列包含一组/图的下须线的极值、下铰链、中值、上铰链和上须线的极值。
To get the
n
on top of the bar, you could usetext
with thestat
details provided by boxplot as followsThe 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.
gplots
包提供boxplot.n
,根据文档生成一个带有观察数量注释的箱线图。The
gplots
package providesboxplot.n
, which according to the documentation produces a boxplot annotated with the number of observations.我找到了使用 Envstats 包的解决方法。该包需要使用以下方法下载、加载和激活:
stripChart(与 stripchart 不同)确实向图表添加了一些值,例如 n 值。首先,我绘制了箱线图。然后我在 stripChart 中使用了 add=T 。显然,很多东西都隐藏在 stripChart 代码中,因此它们不会显示在箱线图上。这是我用于 stripChart 隐藏大多数项目的代码。
Boxplot 带有集成的 stripChart 来显示 n 个值:
所以 boxplot
然后 stripChart
你可以随时调整 的高点数字(n 个值),以便它们适合您想要的位置。
I figured out a workaround using the Envstats package. This package needs to be downloaded, loaded and activated using:
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:
So boxplot
Then stripChart
You can always adjust the high of the numbers (n values) so that they fit where you want.