Coloring data in ggplot using two factor columns

4,323 views
Skip to first unread message

Sabraham

unread,
Jul 29, 2013, 11:49:45 AM7/29/13
to ggp...@googlegroups.com
I have two columns that I want to use to color my data in ggplot. The first one just categorizes the data in two two groups and the second one tells if the data is an outlier or not. Is there anyway I can color my data in ggplot using both columns. Below is the part of the code I used
geom_point(aes(color=factor(column1)))+scale_Colour_manual(values=c("blue","green"))+
geom_point(aes(color=factor(column2)))+scale_colour_manual(values=c("red","yellow"))
 
but below is the error I get
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
 
Thanks,
S

Moritz Emanuel Beber

unread,
Jul 29, 2013, 1:41:54 PM7/29/13
to ggp...@googlegroups.com
Hi,
I think you would have to combine the two factors to one, so that you actually get four different possible groups and assign colours accordingly. Check out the 'interaction' function to do that.

 
Thanks,
S
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
 
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
Best,
Moritz

zelfortin

unread,
Jul 29, 2013, 2:07:06 PM7/29/13
to ggp...@googlegroups.com
Hi,

you could also change the shape of the points based on the first factor and change the color based on the other factor.

e.g.
geom_point(aes(color=factor(column1), shape=factor(column2)))+scale_Colour_manual(values=c("blue","green"))

and then you can use
+ scale_shape_manual(values=c(1,2))

It seems like a more efficient way of showing your two factors.

Hope this helps.

Cheers!

JMF
University of Ottawa
Reply all
Reply to author
Forward
0 new messages