Using multiple overlayed geom_tile layers

1,620 views
Skip to first unread message

b

unread,
Jan 26, 2009, 5:14:40 PM1/26/09
to ggplot2, b...@ekran.org
Hello all,

I'm trying to create a plot from multiple layers of geom_tile.

The reason for multiple layers is to give special treatment to certain
obsevations.

A obvervation that is missing looks like this:

unitX unitY unit umatrix missing
4 3 0 3 0 TRUE

A observation that is not looks like this:

unitX unitY unit umatrix missing
1 0 0 0 0.4933065 FALSE

I'm currently plotting an "X" over missing obsevations:

ggplot(data=umatrix,aes(x=unitX,y=unitY)) +
geom_tile(aes(fill=umatrix)) +
scale_fill_gradient(low="white",high="black") +
geom_point(aes(colour=missing),shape=4,size=3) +
scale_colour_manual(values=c(NA,"red")) +
opts(aspect.ratio=3/4))

But what I want now is to have missing data shown as tiles, just in a
different colour.

Something like:

ggplot(data=umatrix,aes(x=unitX,y=unitY)) +
geom_tile(aes(fill=umatrix)) +
scale_fill_gradient(low="white",high="black") +
geom_tile(aes(fill=missing)) +
scale_fill_manual(values=c(NA,"red")) +
opts(aspect.ratio=3/4)

but I get:

Error: Continuous variable (umatrix) supplied to the discrete
scale_manual.

I was scale_gradient to be applied to umatrix, but I want scale_manual
to be applied to missing.

How can I accomplish this?

Thanks,
.b.

hadley wickham

unread,
Jan 29, 2009, 11:37:24 AM1/29/09
to b, ggplot2, b...@ekran.org
Could you please supply a small reproducible example? (i.e. include
some subset of the data in the email). I think I see what you need to
do, but it's hard to check without your data.

Hadley
--
http://had.co.nz/

B. Bogart

unread,
Jan 29, 2009, 1:10:26 PM1/29/09
to hadley wickham, ggplot2
Here is some sample data that will work with the code already posted.

Please change data=umatrix to data=subset.

Thanks,
.b.
sample.R

hadley wickham

unread,
Jan 29, 2009, 2:42:57 PM1/29/09
to B. Bogart, ggplot2
Ah ok. One possibility is:

ggplot(subset, aes(x=unitX,y=unitY)) +
geom_tile(aes(fill=umatrix)) +
geom_tile(aes(width = 1, height = 1), data = subset(subset,
missing), fill = "red") +
scale_fill_gradient(low="white",high="black") +
opts(aspect.ratio=3/4)

However there's currently no way to get that red colour into the
legend. You want a scale which is a combination of a continuous
colour gradient and a discrete colour (for missings) and there's no
built in scale that supports that.

Hadley
--
http://had.co.nz/

B. Bogart

unread,
Jan 29, 2009, 5:26:25 PM1/29/09
to hadley wickham, ggplot2
That does the trick!

Thanks Hadley.

..b.
Reply all
Reply to author
Forward
0 new messages