customising legend

9 views
Skip to first unread message

Vikas Rawal

unread,
Jan 22, 2009, 4:38:38 PM1/22/09
to ggp...@googlegroups.com
I have been struggling to get the legends in ggplot2 right or do away
with them altogether.

Take the following two plots. The first one produces a broken line. In
the second one, the variable Phase is numeric and therefore it does not
break the line between two "Phases". I do not want the line to be
broken. That is why I am trying to do it by assigning a number to the
"Phase" and then use different labels in the legend. If I put the text
of those labels in the variable Phase itself, as done in the first
example, it gives me a broken line.

# Plot 1

data.frame(Year=c(1:20),CDR=rnorm(20))->b3
c(rep("a",10),rep("b",10))->b3$Phase
qplot(Year,CDR,data=b3,colour=Phase,geom=c("point","line"))

#Plot 2

data.frame(Year=c(1:20),CDR=rnorm(20))->b3
c(rep(1,10),rep(2,10))->b3$Phase
qplot(Year,CDR,data=b3,colour=Phase,geom=c("point","line"))->p
p+scale_colour_gradient2(limits=c(1,2),
midpoint=1.5,low="magenta",high="darkblue",breaks=c(1,2),labels=c("a","b"))

In the past, I have used "(print(p,pretty=F)->p)" to do away with the
legend. But it gives an error in the latest version of ggplot2. The
error I get is:

Error in ggplotGrob(x, ...) : unused argument(s) (pretty = FALSE)

Is it a bug or something has been changed. I shall appreciate any help.

Vikas

hadley wickham

unread,
Jan 22, 2009, 4:43:07 PM1/22/09
to Vikas Rawal, ggp...@googlegroups.com
Hi Vikas,

You are better off controlling the grouping directly. I think the
following should do what you want:

qplot(Year,CDR,data=b3,colour=Phase,geom=c("point","line"), group = 1)

By default, geoms are broken into groups by the combination of all
discrete variables used in the plot. Here you don't want that break
up to occur, so override the default grouping to have only one group.

If you do want to get rid the legend, you can use the following option:
+ opts(drop = "legend")

Regards,

Hadley
--
http://had.co.nz/

Vikas Rawal

unread,
Jan 23, 2009, 1:42:22 AM1/23/09
to ggplot2

> You are better off controlling the grouping directly. I think the
> following should do what you want:
>
> qplot(Year,CDR,data=b3,colour=Phase,geom=c("point","line"), group = 1)
>
> By default, geoms are broken into groups by the combination of all
> discrete variables used in the plot. Here you don't want that break
> up to occur, so override the default grouping to have only one group.
>

Thanks. This is perfect.

ggplot has come a long way over the last couple of years that I have
been using it. Congratulations and thanks for all the work that you have
put in.

Having said that, it is sometimes frustrating to find that a code which
used to work earlier has stopped working. Frustration is perhaps not the
right word because one can always ask on the mailing lists, and you are
always there to help. But then, I do hope that the syntax will become
more stable in future.

But on the whole, great show!!

Thanks,

Vikas

hadley wickham

unread,
Jan 23, 2009, 8:27:27 AM1/23/09
to Vikas Rawal, ggplot2
> Having said that, it is sometimes frustrating to find that a code which
> used to work earlier has stopped working. Frustration is perhaps not the
> right word because one can always ask on the mailing lists, and you are
> always there to help. But then, I do hope that the syntax will become
> more stable in future.

I know it's frustrating - the syntax is slowly stabilising, but there
will probably be a few more changes before it all settles down. I'm
still trying to optimise the syntax, rather than maintain backward
compatibility. That will change as I get closer and closer to ggplot2
1.0.

Hadley

--
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages