[R] Legend issue with ggplot2

hadley wickham h.wickham at gmail.com
Mon Sep 3 15:15:07 CEST 2007


On 9/3/07, ONKELINX, Thierry <Thierry.ONKELINX at inbo.be> wrote:
> Dear useRs,
>
> I'm struggling with the new version of ggplot2. In the previous version
> I did something like this. But now this yield an error (object "fill"
> not found).
>
> library(ggplot2)
> dummy <- data.frame(x = rep(1:10, 4), group = gl(4, 10))
> dummy$y <- dummy$x * rnorm(4)[dummy$group] + 5 * rnorm(4)[dummy$group]
> dummy$min <- dummy$y - 5
> dummy$max <- dummy$y + 5
> ggplot(data = dummy, aes(x = x, max = max, min = min, fill = group)) +
> geom_ribbon() + geom_line(aes(y = max, colour = fill)) + geom_line(aes(y
> = min, colour = fill))

Strange - I'm not sure why that ever worked.

> When I adjust the code to the line below, it works again. But this time
> with two legend keys for "group". Any idea how to display only one
> legend key for group? The ggplot-code aboved yielded only on legend key.
>
> ggplot(data = dummy, aes(x = x, max = max, min = min, colour = group,
> fill = group)) + geom_ribbon() + geom_line(aes(y = max)) +
> geom_line(aes(y = min))

You can manually turn off one of the legends:

sc <- scale_colour_discrete()
sc$legend <- FALSE
.last_plot + sc

It's not very convenient though, so I'll think about how to do this
automatically.  The legends need to be more intelligent about only
displaying the minimum necessary.

Hadley



More information about the R-help mailing list