Re: [R] label outliers in geom_boxplot (ggplot2)

From: Mihalicza Péter <mihalicza.peter_at_eski.hu>
Date: Fri, 06 Jun 2008 11:28:50 +0200

hadley wickham írta:
> 2008/5/27 Mihalicza Péter <mihalicza.peter@eski.hu>:
>
>> 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?
>>
>
> I don't think so. This type of problem (where you need to
> independently access the statistics generated by ggplot) does come up
> fairly often, but I don't have any particularly good solution for it.
>
It's too obvious, so I am positive that there is a good reason for not doing this, but still:
why is it not possible, to have an "outlier" output in stat_boxplot that can be used at geom_text()?

Something like this, with "upper":

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

    > ggplot(dat, aes(y=val, x=num))+stat_boxplot(outlier.size=4,     + outlier.colour="green")+geom_text(aes(y=..upper..), label="This is upper hinge")

Unfortunately, this does not work and gives the error message:

    Error in eval(expr, envir, enclos) : object "upper" not found

Is it because you can only use stat outputs within the stat statements? Could it be possible to make them available outside the statements too?

> P.S. Sorry for taking so long to respond, I've been at my sister's
> wedding in New Zealand
>
Thanks for the answer and happy marriage to your sister!

Peter

-- 
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 Fri 06 Jun 2008 - 09:36:13 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 Fri 06 Jun 2008 - 18:30:36 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