help on adding legend for ggplot2

8 views
Skip to first unread message

JudyMou

unread,
Nov 4, 2010, 6:28:32 PM11/4/10
to ggplot2
I was wondering how to add legend for the fix colour/shape of
aesthetics with fixed value

> data3 = subset(mtcars, cyl != 4.000)
> p <- ggplot(data = subset(mtcars, cyl == 4.000), aes(wt, mpg)) + geom_point()
> p + geom_point(aes(data3$wt, data3$mpg), colour = "blue")

With above command, I've created a graph with two colors. black
indicates cyl!= 4.000, while blue indicates cyl == 4.000. Assume I
made two category, type 1 : cyl != 4.000, and type 2 : cyl == 4.000.
For the legend, I wish to label black is type 1 and red is type 2.

P.S it might not make sense I want to separate cyl to two type. I
wanted to do that because for the data I'm working with, the col cyl
will becomes continuous, and i wish to separate them to two category:
one = 0.25, one !=0.25.

I have been looking for the answer by searching online for the long
time. Please help.

Best,
Judy

Ben Bolker

unread,
Nov 4, 2010, 7:23:22 PM11/4/10
to JudyMou, ggplot2
Is there a reason


data4 <- mtcars
data4$fcyl <- factor(data4$cyl==4)
levels(data4$fcyl) = c("cyl!=4","cyl==4")
ggplot(data=data4,aes(wt,mpg,colour=fcyl))+
scale_colour_manual(value=c("blue","black"))+geom_point()

doesn't work for your purposes? (I'm a little confused about the
colours you want. You can certainly adjust the code above to change the
labels and colours as you like.)

Reply all
Reply to author
Forward
0 new messages