简体   繁体   中英

Remove outlier from five-number summary statistics

How can I force fivenum function to not put outliers as my maximum/minimum values?

I want to be able to see uppper and lower whisker numbers on my boxplot.

My code:

boxplot(data$`Weight(g)`)
text(y=fivenum(data$`Weight(g)`),labels=fivenum(data$`Weight(g)`),x=1.25, title(main = "Weight(g)"))

在此处输入图像描述

boxplot returns a named-list that includes things you can use to remove outliers in your call to fivenum :

  • $out includes the literal outliers. It can be tempting to use setdiff(data$`Weight(g)`) , but that may be prone to problems due to R FAQ 7.31 (and floating-point equality), so I recommend against this; instead,

  • $stats includes the numbers used for the boxplot itself without the outliers. I suggest we work with this.

(BTW, title(.) does its work via side-effect, and it is not used by text(.) , I suggest you move that call.)

Reproducible data/code:

vec <- c(1, 10:20, 30)
bp <- boxplot(vec)
str(bp)
# List of 6
#  $ stats: num [1:5, 1] 10 12 15 18 20
#  $ n    : num 13
#  $ conf : num [1:2, 1] 12.4 17.6
#  $ out  : num [1:2] 1 30
#  $ group: num [1:2] 1 1
#  $ names: chr "1"

five <- fivenum(vec[ vec >= min(bp$stats) & vec <= max(bp$stats)])
text(x=1.25, y=five, labels=five)
title("Weight(g)")

带有更正的 fivenum 标签的基本箱线图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM