1

How can I display outliers as percentages in a boxplot?

I do not want to see the outliers displayed as dots. I would like to see the outliers displayed as number percentages in the boxplot. Below image of how I want outliers:

enter image description here

zx8754
  • 52,746
  • 12
  • 114
  • 209
Mary
  • 67
  • 7
  • Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Jan 25 '16 at 08:11
  • Being an outlier means (roughly) it is outside of a given percentage of the total of dots. So I don't see what you mean by display outliers as percentages. Could you provide a numeric example maybe ? – JulienD Jan 25 '16 at 08:18

1 Answers1

1

Here is a solution: we compute the number of outliers with the output of boxplot, remove the outliers from the graph, and plot the proportion instead.

# simulate data
x <- rnorm(1000)
# first boxplot to get the stats
p <- boxplot(x)

# computing the proportion of outliers
outsiders <- data.frame(text= paste0(c(length(p$out < p$stats[1])/p$n,length(p$out > p$stats[5])/p$n)*100,"%"),
                        y = p$stats[c(1,5)])

# real plot
boxplot(x,outline=FALSE,ylim=c(p$stats[1]-1,p$stats[5]+1))
# we add the text in two steps because we need different adj
text(x=1,labels=outsiders$text[1],y=outsiders$y[1],adj=c(0,1))
text(x=1,labels=outsiders$text[2],y=outsiders$y[2],adj=c(0,-.5))

enter image description here

scoa
  • 19,359
  • 5
  • 65
  • 80
  • Thanks so much for your help! How would I round the percentages to one decimal place or to the nearest whole number? – Mary Jan 26 '16 at 03:26
  • use `round()`: `round(c(length(p$out < p$stats[1])/p$n,length(p$out > p$stats[5])/p$n)*100,1)` for 1 decimal place ; change 1 to 0 for nearest whole number – scoa Jan 26 '16 at 09:29