Skip to content
Prev 248607 / 398503 Next

boxplot - code for labeling outliers - any suggestions for improvements?

Try:

library(TeachingDemos)

plot(Sepal.Length~Sepal.Width, data=iris)

tmp.y <- iris$Sepal.Length
for( i in unique(iris$Sepal.Width) ) {
	tmp <- iris$Sepal.Width == i
	tmp.y[ tmp ] <- spread.labs( tmp.y[tmp], .6*strheight('A'),
		maxiter=1000 )
}

# optional
with(iris, segments(Sepal.Width, Sepal.Length, Sepal.Width+0.025, tmp.y) )

with(iris, text(Sepal.Width+0.05, tmp.y, seq_along(tmp.y), cex=.5 ) )


There is also thigmophobe.labels in the plotrix package which is simpler and works well for some plots

Also look at dynIdentify (Windows only) and TkIdentify (all platforms) in the TeachingDemosp package for a way to interactively place the labels (little more work, but labels end up where you think they look best).

I have experimented with spreading simultaneously in 2 directions, but what works well for one case does lousy in another and what ends up working for the other doesn't work in the first case.

But I would argue against labeling all the points in a plot of that many points, they make it too busy and distract more than help.  HWidentify (windows) and HTKidentify (all platforms) in TeachingDemos give another option.  Sometimes just using different colors/symbols/etc. for groups of points gives more useful information than labels.

Hope this helps,