Assign different colour to subset of points in 2D scatter

3,308 views
Skip to first unread message

Tim

unread,
Dec 10, 2010, 4:56:48 PM12/10/10
to ggplot2
I am new to ggplot. Ordered the book but haven't received it yet, so I
am posting a basic question here. Apologies.

I have a 2D scatter plot and want to colour points according to the
value of third variable: if (value of third variable < 1) then colour
point red

How do you do that?

Any help much appreciated.

Tim.

James McCreight

unread,
Dec 10, 2010, 10:39:06 PM12/10/10
to Tim, ggplot2
Hi Tim-

best practice is to supply your attempt (a reproducible example) so that folks have a clear idea of what your trying to do and what might be going wrong. 

i took a guess at what you might be thinking.

library(ggplot2)
x=1:100
y=x*12+4+(20*rnorm(100))
z=(y-mean(y))/sd(y)
df=data.frame(x=x,y=y,z=z)

p0 <- ggplot(df, aes(x=x,y=y)) + geom_point(data=df[which(df$z <= 1),],colour='black') + geom_point(data=df[which(df$z>1),],colour='red')
p0

this is honestly the quick and dirty. you have a layer for each color and no legend describing what the colour means. this is the next step in elegance.

df$c=cut(z,breaks=c(min(z),1,max(z)),include.lowest=T)
p <- ggplot(df, aes(x=x,y=y, colour=c)) + geom_point()

as far as getting red, 

p + scale_colour_manual(values = c("black","red")) 

Hope that gets ya started. 

James

--
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                                  mccreigh at colorado.edu
NASA Postdoctoral Fellow
cell: (831) 261-5149

Hadley Wickham

unread,
Dec 13, 2010, 1:47:27 PM12/13/10
to James McCreight, Tim, ggplot2
On Fri, Dec 10, 2010 at 9:39 PM, James McCreight <mccr...@colorado.edu> wrote:
> Hi Tim-
> best practice is to supply your attempt (a reproducible example) so that
> folks have a clear idea of what your trying to do and what might be going
> wrong.
> i took a guess at what you might be thinking.
> library(ggplot2)
> x=1:100
> y=x*12+4+(20*rnorm(100))
> z=(y-mean(y))/sd(y)
> df=data.frame(x=x,y=y,z=z)
> p0 <- ggplot(df, aes(x=x,y=y)) + geom_point(data=df[which(df$z <=
> 1),],colour='black') + geom_point(data=df[which(df$z>1),],colour='red')
> p0
> this is honestly the quick and dirty. you have a layer for each color and no
> legend describing what the colour means. this is the next step in elegance.
> df$c=cut(z,breaks=c(min(z),1,max(z)),include.lowest=T)
> p <- ggplot(df, aes(x=x,y=y, colour=c)) + geom_point()

Or even more simply:

p <- ggplot(df, aes(x=x,y=y, colour=factor(z < 1)) + geom_point()

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Timothy Hughes

unread,
Dec 14, 2010, 7:15:41 AM12/14/10
to Hadley Wickham, James McCreight, ggplot2
Thanks. That worked for me.
--
Tim Hughes PhD (http://digitised.info)
Medical Genetics Department
Oslo University Hospital Ullevål
Kirkeveien 166
0407 Oslo
Norway


Reply all
Reply to author
Forward
0 new messages