Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract outliers from box plot in R

Tags:

plot

r

Could you explain me if there is a way to extract outliers from box plot. I have plotted a box plot and I want to extract only the outliers.

Here is the code for the box plot.

# melting down
require(reshape)
melt_nx <- melt(nx, id.vars = c("x", "y"))
boxplot(data = melt_nx, main = "NX", value ~ variable, las = 2,    
par(mar = c(15, 5, 4, 2) + 0.1),
names = c("We1", "We2", "we3"))

Is it possible from the box plot to extract the outliers only?

like image 515
jjjjjjj jjjjjjj Avatar asked Oct 21 '25 06:10

jjjjjjj jjjjjjj


2 Answers

The boxplot function returns a list with one of it node-names as "out". These are the values that are beyond the "whiskers". I don't know about executing par within the argument list but if you want these particular values, then use this:

vals <- boxplot(data = melt_nx, main = "NX", value ~ variable, las = 2,    
      names = c("We1", "We2", "we3"))
vals$out

And do read all these help pages:

?boxplot
?boxplot.stats
?bxp
?fivenum
like image 90
IRTFM Avatar answered Oct 23 '25 20:10

IRTFM


I know this has been answered, but for me there is an alternative method using the Boxplot method from the car package. Note the capital B in the Boxplot function call.

This is the code that does it for me, it returns the row numbers of the outliers which you can then use in your dataframe to filter out or extract, etc...

outliers<-Boxplot(x~y, data=df, id.method="y")

Note that the extracted values are of type Character. Then to exclude them you could do something like:

df2 <- df[-as.numeric(outliers),]

Hope this helps a little

like image 26
Ioannis Baltzakis Avatar answered Oct 23 '25 19:10

Ioannis Baltzakis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!