在离群值点绘制观察数(标签)

时间:2018-03-06 19:53:36

标签: r boxplot

我有这个带有异常值的箱图,我需要绘制包含异常值观察的行数,以便在数据集中轻松找到值,有人可以帮助我吗?

set.seed(1)
a <- runif(10,1,100)
b <-c("A","A","A","A","A","B","B","B","B","B")
t <- cbind(a,b)
bp <- boxplot(a~b)
text(x = 1, y = bp$stats[,1] + 2, labels = round(bp$stats[,1], 2))
text(x = 2, y = bp$stats[,2] + 2, labels = round(bp$stats[,2], 2))

How

1 个答案:

答案 0 :(得分:0)

t <- cbind(a, b)有什么意义?这会产生一个字符矩阵,并将您的数字转换为字符串?你无论如何都不能使用它。如果您想要单个数据结构,请data.frame(a, b)使a成为一个因子并保留b数字。我没有得到你用set.seed(1)做的情节,所以我会提供稍微不同的数据。请注意在pos中使用offset= =和text()参数。请务必阅读手册页以了解他们正在做什么:

a <- c(99.19, 59.48, 48.95, 18.17, 75.73, 45.94, 51.61, 21.55, 37.41, 
59.98, 57.91, 35.54, 4.52, 64.64, 75.03, 60.21, 56.53, 53.08, 
98.52, 51.26)
b <- c("A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B")
bp <- boxplot(a~b)
text(x = 1, y = bp$stats[,1], labels = round(bp$stats[, 1], 2), 
     pos=c(1, 3, 3, 1, 3), offset=.2)
text(x = 2, y = bp$stats[, 2], labels = round(bp$stats[, 2], 2), 
     pos=c(1, 3, 3, 1, 3), offset=.2)
obs <- which(a %in% bp$out)
text(bp$group, bp$out, obs, pos=4)

enter image description here