Discussion:
Alpha and facet_wrap()
Pierre Roudier
2011-08-04 23:50:30 UTC
Permalink
Hi,

I've been bumping into the following problem lately> Am wondering if
I'm missing something obvious, or if this is a bug:

d <- cars
d$foo <- letters[1:2]

# Transparency works
ggplot(d) + geom_rect(aes(xmin=0, xmax=8, ymin=0, ymax=100), fill =
"lightblue", alpha = 0.2) + geom_point(aes(x=speed, y=dist))

# Transparency fails
ggplot(d) + geom_rect(aes(xmin=0, xmax=8, ymin=0, ymax=100), fill =
"lightblue", alpha = 0.2) + geom_point(aes(x=speed, y=dist)) +
facet_wrap(~foo)

Cheers,

Pierre
--
Scientist
Landcare Research, New Zealand
Joran
2011-08-05 03:13:08 UTC
Permalink
I've struggled with geom_rect myself doing stuff like this (which is
intended to be a comment on me, not on geom_rect). The follow does
what you expect in the faceting case:

rect <- rbind(data.frame(xmin=0, xmax=8, ymin=0, ymax=100,foo = "a"),
data.frame(xmin=0, xmax=8, ymin=0, ymax=100,foo = "b"))

ggplot(d) +
geom_rect(data = rect,aes(xmin=xmin, xmax=xmax, ymin=ymin,
ymax=ymax),
fill = "lightblue", alpha = 0.2) +
geom_point(aes(x=speed, y=dist)) +
facet_wrap(~foo)

When doing things with geom_rect and faceting I nearly always resort
to building a separate data frame that explicitly contains the
faceting variable in it. I do this precisely because I haven't figured
out a way to avoid it, even when the rect is actually going to be the
same in each facet. My wild-ass guess is that this behavior has to do
with how ggplot makes guesses about how to draw the rect based on the
information you give it, specifically when you don't feed geom_rect a
distinct data frame so it's trying to fit that info into what it knows
about the data frame d.

Perhaps someone will chime in with a way to avoid this and we'll both
learn something...
Post by Pierre Roudier
Hi,
I've been bumping into the following problem lately> Am wondering if
d <- cars
d$foo <- letters[1:2]
# Transparency works
ggplot(d) + geom_rect(aes(xmin=0, xmax=8, ymin=0, ymax=100), fill =
"lightblue", alpha = 0.2) + geom_point(aes(x=speed, y=dist))
# Transparency fails
ggplot(d) + geom_rect(aes(xmin=0, xmax=8, ymin=0, ymax=100), fill =
"lightblue", alpha = 0.2) + geom_point(aes(x=speed, y=dist)) +
facet_wrap(~foo)
Cheers,
Pierre
--
Scientist
Landcare Research, New Zealand
--
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 ggplot2-/***@public.gmane.org
To unsubscribe: email ggplot2+unsubscribe-/***@public.gmane.org
More options: http://groups.google.com/group/ggplot2
Hadley Wickham
2011-08-15 11:42:43 UTC
Permalink
Post by Joran
When doing things with geom_rect and faceting I nearly always resort
to building a separate data frame that explicitly contains the
faceting variable in it.
This is very good advice!
Post by Joran
I do this precisely because I haven't figured
out a way to avoid it, even when the rect is actually going to be the
same in each facet. My wild-ass guess is that this behavior has to do
with how ggplot makes guesses about how to draw the rect based on the
information you give it, specifically when you don't feed geom_rect a
distinct data frame so it's trying to fit that info into what it knows
about the data frame d.
Exactly.

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