How to change legend text for geom_point with scale_colour_manual?

3,068 views
Skip to first unread message

Marius Hofert

unread,
Aug 19, 2011, 2:43:00 PM8/19/11
to ggplot2
Dear all,

I managed to bring in line types and points in the legend (basically
through
two legends). For the linetype I can easily control both the legend
title and the
legend text. For the points, I can specify the legend title via "name"
in
scale_colour_manual, but I can't choose the legend texts in
scale_colour_manual.
Is this possible? For reasons such as greek letters, I can't/don't
want to name
the group variables in the data frame as they should appear in the
plot in the
first place.
I tried the argument "breaks" to scale_colour_manual, but it did not
work.

Cheers,

Marius


library(ggplot2)

df = data.frame(x=rnorm(100), y=rexp(100), group=rep(c("group1",
"group2"), each=50))

ggplot(df, aes(x=x, y=y)) + geom_line(aes(linetype="Linetype")) + #
legend text
scale_linetype_manual(name="Main Group", values=2) + # legend
title and line type
geom_point(aes(colour=group)) + # legend text: taken from df; how
to put in one's own labels?
scale_colour_manual(name="Sub-group", values=c("green", "blue")) #
point colors

## Trial 1:
ggplot(df, aes(x=x, y=y)) + geom_line(aes(linetype="Linetype")) + #
legend text
scale_linetype_manual(name="Main Group", values=2) + # legend
title and line type
geom_point(aes(colour=group)) + # legend text: taken from df
scale_colour_manual(name="Sub-group", breaks=c("Group label 1",
"Group label 2"), # trial with breaks
values=c("green", "blue")) # point colors

## Trial 2:
ggplot(df, aes(x=x, y=y)) + geom_line(aes(linetype="Linetype")) + #
legend text
scale_linetype_manual(name="Main Group", values=2) + # legend
title and line type
geom_point(aes(colour=group)) + # legend text: taken from df
scale_colour_manual(name="Sub-group", values=c("Group label
1"="green", "Group label 2"="blue")) # does not work either

Ista Zahn

unread,
Aug 19, 2011, 2:59:17 PM8/19/11
to Marius Hofert, ggplot2
Hi Marius,
This is explained in the documentation (see
http://had.co.nz/ggplot2/scale_manual.html 6th example).

Best,
Ista

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

Marius Hofert

unread,
Aug 19, 2011, 3:03:02 PM8/19/11
to ggplot2
Hi Ista,

thanks a ton, the "labels" argument was it:

library(ggplot2)

df = data.frame(x=rnorm(100), y=rexp(100), group=rep(c("group1",
"group2"), each=50))

ggplot(df, aes(x=x, y=y)) + geom_line(aes(linetype="Linetype")) + #
legend text
scale_linetype_manual(name="Main Group", values=2) + # legend
title and line type
geom_point(aes(colour=group)) + # legend text
scale_colour_manual(name="Sub-group", values=c("green", "blue"),
labels=c("Group 1", "Group 2")) # point colors

Cheers,

Marius

On Aug 19, 8:59 pm, Ista Zahn <iz...@psych.rochester.edu> wrote:
> Hi Marius,
> This is explained in the documentation (seehttp://had.co.nz/ggplot2/scale_manual.html6th example).
Reply all
Reply to author
Forward
0 new messages