如何将值放在 R 中的 Boxplot 上,用于一张图像中的多个箱线图 [英] How to put Values on Boxplot in R for several boxplot in one image
问题描述
我想在 R 中绘制 Delta~Project.Types.我有 10 个项目类型.我知道如何做箱线图:箱线图(Delta~Project.Types).但是,如何在每个箱线图上放置五位数(最小值、最大值、第 1、第 2 和第 3 分位数)?我该怎么做才能使图像的每个箱线图都显示其五个数字?显示值时比较箱线图会更容易
I want to plot Delta~Project.Types in R. I have 10 Project Types. I know how to do the boxplot : boxplot(Delta~Project.Types). However, how can I put the fivenum (min, max, 1st, 2nd, and 3rd quantile) on each boxplot? How can I do for that every boxplot of the image will have its five number shown? That would be easier to compare the boxplots when the values are shown
谢谢!
推荐答案
你想要的统计也可以用fivenum
five <- by(InsectSprays$count, InsectSprays$spray, fivenum)
do.call(cbind, five)
# A B C D E F
# [1,] 7.0 7.0 0.0 2.0 1.0 9
# [2,] 11.0 12.0 1.0 3.5 2.5 12
# [3,] 14.0 16.5 1.5 5.0 3.0 15
# [4,] 18.5 18.0 3.0 5.0 5.0 23
# [5,] 23.0 21.0 7.0 12.0 6.0 26
或者,这些统计数据是 boxplot
的返回值之一(请注意,您需要使用 range = 0
来获取最小值和最大值,因为有一些值偏远):
Alternatively, these stats are one of the return values of boxplot
(note that you need to use range = 0
to get the min and max since there are some values which are outlying):
bp <- boxplot(count ~ spray, data = InsectSprays, col = "lightgray", range = 0)
bp$stats
# [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] 7.0 7.0 0.0 2.0 1.0 9
# [2,] 11.0 12.0 1.0 3.5 2.5 12
# [3,] 14.0 16.5 1.5 5.0 3.0 15
# [4,] 18.5 18.0 3.0 5.0 5.0 23
# [5,] 23.0 21.0 7.0 12.0 6.0 26
然后只需添加到每个框:
Then just add to each box:
text(x = col(bp$stats) - .5, y = bp$stats, labels = bp$stats)
这篇关于如何将值放在 R 中的 Boxplot 上,用于一张图像中的多个箱线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!