[R] label outliers in geom_boxplot (ggplot2)
mihalicza.peter at eski.hu
Fri Jun 6 11:28:50 CEST 2008
hadley wickham írta:
> 2008/5/27 Mihalicza Péter <mihalicza.peter at 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),
>> > p=ggplot(dat, aes(y=val, x=num))+geom_boxplot(outlier.size=4,
>> > 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),
> ggplot(dat, aes(y=val, x=num))+stat_boxplot(outlier.size=4,
+ outlier.colour="green")+geom_text(aes(y=..upper..), label="This is
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!
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
More information about the R-help