Marius Hofert
unread,Aug 19, 2011, 2:43:00 PM8/19/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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