Alpha issue with geom rect

3,396 views
Skip to first unread message

JiHO

unread,
Oct 12, 2009, 2:24:51 PM10/12/09
to ggplot2
Hi everyone,

I am trying to overlay rectangles on a previous plot and cannot make
them transparent. I think I have identified the core issue below

ggplot() + geom_rect(aes(xmin=0, xmax=1, ymin=0, ymax=1), alpha=0.5)
# works
df <- data.frame(a=runif(10), b=runif(10))
ggplot(df) + geom_rect(aes(xmin=0, xmax=1, ymin=0, ymax=1), alpha=0.5)
# does not work
ggplot(df) + geom_rect(data=data.frame(x1=0, x2=1, x3=0, x4=1),
aes(xmin=x1, xmax=x2, ymin=x3, ymax=x4), alpha=0.5)
# works, but is rather cumbersome

Alpha seems to be the only aesthetic causing trouble. Changing the
color, fill, etc. works

Is that a bug or is there a reason for this behaviour?

JiHO
---
http://maururu.net

Charlotte Wickham

unread,
Oct 12, 2009, 3:08:10 PM10/12/09
to JiHO, ggplot2
Try:
ggplot(df) + geom_rect(aes(xmin=0, xmax=1, ymin=0, ymax=1), alpha=0.1)

which highlights what's happening. df has 10 rows so 10 rectangles are
being drawn all on top of each other and appear opaque with alpha =
0.5. Your more cumbersome approach is defining a dataframe with a
single row.

Charlotte

JiHO

unread,
Oct 12, 2009, 3:28:27 PM10/12/09
to Charlotte Wickham, ggplot2
On Mon, Oct 12, 2009 at 21:08, Charlotte Wickham <cwic...@gmail.com> wrote:
> Try:
> ggplot(df) + geom_rect(aes(xmin=0, xmax=1, ymin=0, ymax=1), alpha=0.1)
>
> which highlights what's happening. df has 10 rows so 10 rectangles are
> being drawn all on top of each other and appear opaque with alpha =
> 0.5.  Your more cumbersome approach is defining a dataframe with a
> single row.

I see. I even remember someone having this issue before. Sorry for the
trouble. So I will do

ggplot() + geom_path(data=df, aes(...)) + geom_rect(xmin=0, xmax=1,
ymin=0, ymax=1, alpha=0.5)

and it will work. As in other occasions, specifying everything for
each layer is longer but more consistent. I guess I am just lazy...

PS: It seems the whole Wickham family is getting involved in ggplot!
Glad to see you here.

JiHO
---
http://maururu.net

baptiste auguie

unread,
Oct 12, 2009, 3:32:49 PM10/12/09
to JiHO, ggplot2
Alternatively,

ggplot(df) + annotate("rect", 0,0,0,1,0,1, alpha=0.5)

and you can use a default data.frame for the whole plot.

Cheers,

baptiste
Reply all
Reply to author
Forward
0 new messages