change colours in ggplot2?

538 views
Skip to first unread message

Zhenjiang Lan

unread,
Mar 3, 2012, 3:16:01 PM3/3/12
to ggplot2
Hi,

Can anybody help me to change colours in ggplot2?

Here is my example:

df0<-data.frame(x=rnorm(6),y=rnorm(6),grp=factor(rep(c("blue","green","purple"),each=2)))
ggplot(df0,aes(x=x,y=y,colour=grp)+geom_point+scale_colour_brewer(palette="Ste1")

But, the points colour are not like their name: group 'blue' is red;
group 'green' is blue; group 'purple' is green.

Because the colors in 'Set1' is : red, blue, green, purple, ......
so I decided to insert a new empty level into 'grp', to let them match
like this:

"empt" => red
"blue" => blue
"green"=> green
"purple"=>purple

I do like this:

df0$grp2<-factor(df0$grp,levels=c("empt",levels(df0$grp)))
ggplot(df0,aes(x=x,y=y,colour=grp2)+geom_point+scale_colour_brewer(palette="Ste1")

I thought that ggplot will assign red to level 'empt', assign blue to
level 'blue', etc.
but it doesn't work... The colour didn't change.

How can I do this?

Thanks!

Lan

James McCreight

unread,
Mar 3, 2012, 3:33:23 PM3/3/12
to Zhenjiang Lan, ggplot2
Hello!

this works fine, if you just fix your syntax and spelling errors
ggplot(df0,aes(x=x,y=y,colour=grp2))+geom_point()+scale_colour_brewer(palette="Set1")

you can also avoid the empty level by using scale_colour_manual
ggplot(df0,aes(x=x,y=y,colour=grp))+geom_point()+scale_colour_manual(values = c("blue", "green","purple"))





Lan

--
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



--

******************************************************************************
James McCreight                               
cell: (831) 261-5149
VoIP (to cell): (720) 897-7546

Zhenjiang Lan

unread,
Mar 3, 2012, 3:44:31 PM3/3/12
to James McCreight, ggplot2
Thanks for reply.

I try again, with typo fixed, but still doesn't work.
The results from these two lines are the same in my screen:

ggplot(df0,aes(x=x,y=y,colour=grp2))+geom_point()+scale_colour_brewer(palette="Set1")
ggplot(df0,aes(x=x,y=y,colour=grp))+geom_point()+scale_colour_brewer(palette="Set1")

and I'm sure the two factors are different:

df0$grp
[1] blue blue green green purple purple
Levels: blue green purple


df0$grp2
[1] blue blue green green purple purple
Levels: empt blue green purple

And about the alternative solution, the problem is, I like some
colours in brewer, but I don't know their name.

Zhenjiang Lan

unread,
Mar 3, 2012, 3:59:20 PM3/3/12
to James McCreight, ggplot2
'scale_color_manual' solved my problem. Thank you!
Reply all
Reply to author
Forward
0 new messages