Home > OS >  Boxplot: label an outlier
Boxplot: label an outlier

Time:09-17

I'm working with the airquality dataset and I saw this in another post. I'd like to print the number of the observation for the 3 outliers instead of the value of those points, is there a way to do that? I mean, what I want is to print the index within the Wind vector of those 3 outliers, not their values. Thanks

library(datasets)
library(ggplot2)
a=boxplot(airquality$Wind,plot=FALSE)
qplot(y=airquality$Wind,geom='boxplot') 
annotate(geom="text",x=rep(0.1,length(a$out)),
y=a$out,label=a$out,size=2.5)

enter image description here

CodePudding user response:

Not sure what you mean by printing the values nicely; here's a plot with the observation index printed; you can adjust appearance with the annotate arguments.

library(datasets)
library(ggplot2)

a = boxplot(airquality$Wind, plot = FALSE)

qplot(y = airquality$Wind, geom = 'boxplot')  
  annotate(geom = "text",
           x = rep(0.1, length(a$out)),
           y = a$out,
           label = which(airquality$Wind  %in%  a$out),
           size = 2.5)

Created on 2021-09-09 by the reprex package (v2.0.0)

  • Related