[R] label outliers in geom_boxplot (ggplot2)

From: Mihalicza Péter <mihalicza.peter_at_eski.hu>
Date: Tue, 27 May 2008 16:58:40 +0200


Dear List and Hadley,

I would like to have a boxplot with ggplot2 and have the outlier values labelled with their "name" attribute. So I did    

> library(ggplot2)
> dat=data.frame(num=rep(1,20), val=c(runif(18),3,3.5),
name=letters[1:20])

> p=ggplot(dat, aes(y=val, x=num))+geom_boxplot(outlier.size=4,
outlier.colour="green")

> p+geom_text(label=dat$name)

But this -of course- labels all the data points. So I searched high and low to find the way to only label the outliers, but I couldn't find any solution. Probably my keywords were inappropriate, but I looked at the ggplot website and the book also. So I did this:    

> boxout=boxplot(dat$val)$out
> outname=as.character(dat$name)
> outname[(dat$val %in% boxout)==FALSE]="\n"
> p+geom_text(label=outname)

This works, but seems like a hack to me. Is there an obvious solution that I am missing?

Another thing: it seems, that if there is only one outlier, ggplot doesn't show it, although it adjusts the axis to it, and also plots the label, when doing geom_text():

>library(ggplot2)
>dat=data.frame(num=rep(1,20), val=c(runif(19),3), name=letters[1:20])
>ggplot(dat, aes(y=val, x=num))+geom_boxplot(outlier.size=4,
outlier.colour="green")

>p
>p+geom_text(label=dat$name)

 > sessionInfo()
R version 2.6.2 (2008-02-08)
i386-pc-mingw32

locale:
LC_COLLATE=Hungarian_Hungary.1250;LC_CTYPE=Hungarian_Hungary.1250;LC_MONETARY=Hungarian_Hungary.1250;LC_NUMERIC=C;LC_TIME=Hungarian_Hungary.1250

attached base packages:
[1] splines grid stats graphics grDevices utils datasets [8] methods base

other attached packages:

[1] ggplot2_0.6        colorspace_0.95    RColorBrewer_1.0-2 
MASS_7.2-41      
[5] proto_0.3-8        reshape_0.8.0      Hmisc_3.4-3      

loaded via a namespace (and not attached): [1] cluster_1.11.10 lattice_0.17-6 tools_2.6.2

Thank you for your help!

Best wishes,
Peter

-- 
Peter Mihalicza
economist

National Institute for Strategic Health Research
6-8. Arany János utca, Budapest-1051, Hungary
www.eski.hu

Tel.: +36-1-354-5320
E-mail: mihalicza.peter_at_eski.hu



-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Tue 27 May 2008 - 17:10:54 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 05 Jun 2008 - 20:32:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive