[R] ggplot2: How to remove legend component for geom_errorbar() ?

Carsten Jaeger cajaeg at googlemail.com
Mon Jun 16 17:58:02 CEST 2008


Hi Hadley,

thanks for your tip! It worked fine (though I had to use "tile" instead
of "point"). I see your point to handle legends the way you do, which is
a very convenient feature of ggplot. It's great for points, lines, bars
etc., but in the particular case of error bars, I think it would be
reasonable to omit them by default and only turn them on if someone
really wants them (by using something like "legend=TRUE"). Just my two
cents, for now, I'm happy with your workaround.

Thanks again,
Carsten


On Mon, 2008-06-16 at 09:49 -0500, hadley wickham wrote:
> Hi Carsten,
> 
> In my eagerness to get every component of the legend working
> automatically, I've completed neglected a way to turn bits off of you
> don't want them (the philosophy is that every geom that uses an
> aesthetic should appear in the legend in some way).  The best thing I
> can suggest at the moment is:
> 
> GeomErrorbar$guide_geom <- function(.) "point"
> 
> to use points for the errorbar legend, which you won't see because
> they'll overlap with the points from the points geom.  In the next
> version, you'll be able to do
> 
> GeomLine$guide_geom <- function(.) "blank"
> 
> Or I might come up with a better way of controlling the appearance.
> 
> Regards,
> 
> Hadley
> 
> On Mon, Jun 16, 2008 at 2:55 AM, Carsten Jaeger <cajaeg at googlemail.com> wrote:
> > Dear list (probably Hadley),
> >
> > I'm trying to do a plot like the following, composed of bars and error
> > bars:
> >
> > df <- data.frame(factor1=gl(2,5), factor2=gl(5,1), y=rnorm(10),
> > err=0.1)
> > ggplot(df, aes(x=factor1, y=y, fill=factor2)) +
> >    geom_bar(position="dodge", stat="identity") +
> >    geom_errorbar(aes(min=y-err, max=y+err), position="dodge",
> > width=0.1)
> >
> > The plot obtained has a legend named "factor2", giving as expected the
> > colours used for the five factor levels of factor2. So far so good.
> > However, the legend also contains solid horizontal lines, obviously
> > representing the error bars. While I do not immediately see the sense of
> > a legend symbol for error bars, is there any easy way to turn these off?
> >
> > Thanks,
> > Carsten
> >
> > ______________________________________________
> > 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.
> >
> 
> 
>



More information about the R-help mailing list