Legend trouble - multiple data sets

1,406 views
Skip to first unread message

Sarah McIntyre

unread,
Jan 27, 2010, 7:18:10 PM1/27/10
to ggplot2
Hi,

I'm trying to plot two data sets on the same graph. The two data sets
are the results of two similar tasks - both have the same two
experimental conditions. I want one data set to be in black, and the
other in gray but I want the matching conditions to have matching
linetype and shape. I can do all of this, but my problem is that when
I add the second data set, its legend overrides the legend for the
first but I need them both to display.

Here is some sample code (just showing the shapes, not the lines, for
simplicity):

p1<- ggplot(data=result, aes(x=stimulus,y=proportion1))
p1<-p1 +geom_point(data=result, aes
(x=stimulus,y=proportion1,shape=condition),colour="black")
p1<-p1+ scale_shape(name="Task1",breaks = c("adapt", "control"),labels
= c("Adaptation", "Control"))
p1<- p1 +geom_point(data=result, aes
(x=stimulus,y=proportion2,shape=condition),colour="grey50")
p1<- p1+ scale_shape(name="Task2",breaks = c("adapt",
"control"),labels = c("Adaptation", "Control"))

The problem happens in the last line where Task2 legend overrides the
Task1 legend - I want them both to show that the black is for task 1
and the grey is for task 2.

Zack Weinberg

unread,
Jan 27, 2010, 8:13:58 PM1/27/10
to Sarah McIntyre, ggplot2
If you convert to long format with melt(), then the colour can be an
aes() parameter, and the legend should Just Work. Something like
this:

library(reshape)

result.l <- melt(result, measure.vars=c('proportion1', 'proportion2'))

ggplot(data=result.l) +
geom_point(aes(x=stimulus, y=value, shape=condition, colour=variable)) +
scale_shape(breaks=c("adapt", "control"), labels=c("Adaptation", "Control")) +
scale_colour_manual(breaks=c("proportion1", "proportion2"),
labels=c("Task 1", "Task 2"), values=c("black", "grey50"))

zw

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> To post to this group, send email to ggp...@googlegroups.com
> To unsubscribe from this group, send email to
> ggplot2+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/ggplot2

Sarah McIntyre

unread,
Jan 27, 2010, 10:36:02 PM1/27/10
to ggplot2
Thanks that worked very well once I juggled my data sets a bit.

On Jan 28, 12:13 pm, Zack Weinberg <za...@panix.com> wrote:
> If you convert to long format with melt(), then the colour can be an
> aes() parameter, and the legend should Just Work.  Something like
> this:
>
> library(reshape)
>
> result.l <- melt(result, measure.vars=c('proportion1', 'proportion2'))
>
> ggplot(data=result.l) +
>   geom_point(aes(x=stimulus, y=value, shape=condition, colour=variable)) +
>   scale_shape(breaks=c("adapt", "control"), labels=c("Adaptation", "Control")) +
>   scale_colour_manual(breaks=c("proportion1", "proportion2"),
>     labels=c("Task 1", "Task 2"), values=c("black", "grey50"))
>
> zw
>

Reply all
Reply to author
Forward
0 new messages