alpha and geom_rect

1,536 views
Skip to first unread message

Kasper Daniel Hansen

unread,
May 29, 2009, 1:02:11 PM5/29/09
to ggplot2
I am trying to enhance a plot area using geom_rect. I would like the
color and the alpha setting to be similar to what happens for
geom_smooth, shown on
http://had.co.nz/ggplot2/geom_smooth.html
(last picture). That is, I want a slightly darker grey colour,
transparent so I can see the grid lines through the rectangle.

Looking at the documentation for geom_smooth I see the (relevant)
default settings are
fill = "grey60", alpha = 0.4

I have made a reproducible example, showing my problem

testDF = data.frame(x1 = 1:10, y1 = 1:10)
ggplot(data = testDF, aes(x1, y1, colour = "BAD")) + geom_point() +
geom_rect(aes(NULL, NULL, xmin = 3, xmax = 7, ymin = 3, ymax =
7), fill = "grey60", alpha = 0.4)

In this case the rectangle is very dark and not transparent at all. If
I instead do something like

ggplot(data = testDF, aes(x1, y1, colour = "BAD")) + geom_point() +
geom_rect(aes(NULL, NULL, xmin = 3, xmax = 7, ymin = 3, ymax =
7), fill = "grey80", alpha = 0.1)

I get more transparency and lighter grey.

What am I missing here? It seems I need pretty low alpha settings and
high grey values to get anything resembling the geom_smooth settings.

I am using a quartz device on Mac, but instantiated from the Terminal
using the newish possibility of using quartz from terminal R. However,
if I run the example in geom_smooth, I get a picture similar to the
web page above, so it seems my device is fine.

Kasper


Mike Lawrence

unread,
May 29, 2009, 2:01:02 PM5/29/09
to Kasper Daniel Hansen, ggplot2
Alpha is expected to affect the "greyness". Consider a black square on
a white background; adjusting the opacity of the square is clearly
equivalent to simply changing the square to some degree of grey.

To simplify your search for what you want, I'd simply omit the fill
(which I believe is equivalent to setting fill='black') and play with
opacity until you get what you want.
--
Mike Lawrence
Graduate Student
Department of Psychology
Dalhousie University

Looking to arrange a meeting? Check my public calendar:
http://tr.im/mikes_public_calendar

~ Certainty is folly... I think. ~

Kasper Daniel Hansen

unread,
May 29, 2009, 2:07:45 PM5/29/09
to Mike Lawrence, ggplot2
I understand that there is some interaction between transparency and
greyness. Let me restate my problem.

geom_smooth produces exactly the effect I want. Using the default
settings of geom_smooth in my call to geom_rect, I don't get the same
grey/transparent scheme. I am baffled by this.

Kasper

Mike Lawrence

unread,
May 29, 2009, 2:29:46 PM5/29/09
to Kasper Daniel Hansen, ggplot2
Ah, my bad for not running your code. In your call to ggplot() you map
the colour aesthetic to a variable (which happens to have only 1
level), and that colour mapping is reused in geom_rect, hence the box
having a pink outline. To get rid of the outline, you should either
map color within the call to geom_point() only:

p = ggplot(data = testDF, aes(x1, y1))
p = p + geom_rect(aes(xmin = 3, xmax = 7, ymin = 3, ymax =7),alpha = 0.1)
p = p + geom_point(aes(colour='BAD'))

or set the color to "transparent" inside geom_rect():

p = ggplot(data = testDF, aes(x1, y1, colour = "BAD"))
p = p + geom_rect(aes(xmin = 3, xmax = 7, ymin = 3, ymax
=7),colour='transparent',alpha = 0.1)
p = p + geom_point()

Note that I've layered the points on top of the rect so that their
color isn't affected by the rect (which would diminish their luminance
and undermine your goal of calling attention to those points).


On Fri, May 29, 2009 at 3:07 PM, Kasper Daniel Hansen

JiHO

unread,
May 29, 2009, 2:30:24 PM5/29/09
to Kasper Daniel Hansen, Mike Lawrence, ggplot2
On 2009-May-29 , at 14:07 , Kasper Daniel Hansen wrote:

> I understand that there is some interaction between transparency and
> greyness. Let me restate my problem.
>
> geom_smooth produces exactly the effect I want. Using the default
> settings of geom_smooth in my call to geom_rect, I don't get the same
> grey/transparent scheme. I am baffled by this.

When I try this:

qplot(1:10, runif(10)*4) + geom_rect(aes(NULL, NULL, xmin = 3, xmax =
7, ymin = 3, ymax = 7), fill = "grey60", alpha = 0.4) + geom_smooth()

on a Quartz device, I get the exact same grey for the rectangle and
the smooth.
So it is something specific to how your example is written.

When I try that:

qplot(1:10, runif(10)*4) + geom_rect(aes(xmin = 3, xmax = 7, ymin = 3,
ymax = 7), fill = "grey60", alpha = 0.4) + geom_smooth()

the rectangle is opaque. What are the NULL, NULL for in your example?
Un-map x and y? But there are no x and y aes in geom_rect. There seem
to be an interaction between the definition of data in ggplot and the
fact that there is actually no data in geom_rect:

ggplot(data=data.frame(x=1:10, y=1:10)) + geom_point(aes(x=x, y=y)) +
geom_rect(aes(xmin=3, xmax=6, ymin=3, ymax=6), alpha=0.5)
# opaque

ggplot() + geom_rect(aes(xmin=3, xmax=6, ymin=3, ymax=6), alpha=0.5)
# transparent

I would stick to a rule that works well for me: unless something is
common to *all* later geoms, do not supply it to ggplot(), only give
it to the appropriate geom. Here indeed:

ggplot() + geom_point(aes(x=x, y=y), data=data.frame(x=1:10, y=1:10))
+ geom_rect(aes(xmin=3, xmax=6, ymin=3, ymax=6), alpha=0.5)

works.

I hope that helps.

NB: an exception to the above rule is when faceting is involved: a
dataset containing all the facetting variables must be supplied to
ggplot.

JiHO
---
http://jo.irisson.free.fr/

hadley wickham

unread,
May 29, 2009, 3:42:18 PM5/29/09
to Kasper Daniel Hansen, ggplot2
On Sat, May 30, 2009 at 5:02 AM, Kasper Daniel Hansen
<kha...@stat.berkeley.edu> wrote:
>
> I am trying to enhance a plot area using geom_rect. I would like the
> color and the alpha setting to be similar to what happens for
> geom_smooth, shown on
>   http://had.co.nz/ggplot2/geom_smooth.html
> (last picture). That is, I want a slightly darker grey colour,
> transparent so I can see the grid lines through the rectangle.
>
> Looking at the documentation for geom_smooth I see the (relevant)
> default settings are
>   fill = "grey60", alpha = 0.4
>
> I have made a reproducible example, showing my problem
>
> testDF = data.frame(x1 = 1:10, y1 = 1:10)
> ggplot(data = testDF, aes(x1, y1, colour = "BAD")) + geom_point() +
>     geom_rect(aes(NULL, NULL, xmin = 3, xmax = 7, ymin = 3, ymax =
> 7), fill = "grey60", alpha = 0.4)

This one took me a while to figure out - the problem is that the
rectangle layer is inheriting the _data_, and even though you have not
mapped any variables to the data you still get one rectangle for each
row of the original dataset, i.e. ten rectangles plotted on top of one
another.

ggplot(data = testDF, aes(x1, y1, colour = "BAD")) + geom_point() +

geom_rect(aes(xmin = 3, xmax = 7, ymin = 3, ymax =
7), fill = "grey60", alpha = 0.4, data = data.frame(x1 = 1, y1 = 1))

or

ggplot(data = testDF, aes(x1, y1, colour = "BAD")) + geom_point() +

annotate("rect", xmin = 3, xmax = 7, ymin = 3, ymax = 7, fill =


"grey60", alpha = 0.4)

# but this is broken for this example in the current version, fixed in devel

Hadley


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

Mike Lawrence

unread,
May 29, 2009, 4:57:46 PM5/29/09
to hadley wickham, Kasper Daniel Hansen, ggplot2
On Fri, May 29, 2009 at 4:42 PM, hadley wickham <h.wi...@gmail.com> wrote:
> This one took me a while to figure out - the problem is that the
> rectangle layer is inheriting the _data_, and even though you have not
> mapped any variables to the data you still get one rectangle for each
> row of the original dataset, i.e. ten rectangles plotted on top of one
> another.

Ah, I clearly didn't notice that.

Maybe this is a good example of why specifying data & mapping in
ggplot() can be dangerous. In teaching students ggplot2, I've come to
prefer telling them to explicitly specify their data & mapping within
each layer; indeed, I've been telling them to use
p=ggplot()
p=p+layer(
geom=...
,geom_params=list(...)
,stat=...
,stat_params=list(...)
,data=...
,mapping=...
,position=...
)
just to be extra explicit. I find it also reinforces the grammar.

Mike

hadley wickham

unread,
May 29, 2009, 5:43:00 PM5/29/09
to Mike Lawrence, Kasper Daniel Hansen, ggplot2
On Sat, May 30, 2009 at 8:57 AM, Mike Lawrence <Mike.L...@dal.ca> wrote:
> On Fri, May 29, 2009 at 4:42 PM, hadley wickham <h.wi...@gmail.com> wrote:
>> This one took me a while to figure out - the problem is that the
>> rectangle layer is inheriting the _data_, and even though you have not
>> mapped any variables to the data you still get one rectangle for each
>> row of the original dataset, i.e. ten rectangles plotted on top of one
>> another.
>
> Ah, I clearly didn't notice that.
>
> Maybe this is a good example of why specifying data & mapping in
> ggplot() can be dangerous. In teaching students ggplot2, I've come to
> prefer telling them to explicitly specify their data & mapping within
> each layer; indeed, I've been telling them to use
> p=ggplot()
> p=p+layer(
>     geom=...
>     ,geom_params=list(...)
>     ,stat=...
>     ,stat_params=list(...)
>     ,data=...
>     ,mapping=...
>     ,position=...
> )
> just to be extra explicit. I find it also reinforces the grammar.

Sometimes it's good to be explicit. But you don't want to let it get
in the way of actually getting work done - I've found students often
have pretty poor touch typing skills and making them be too verbose
makes them so slow a creating a single graph that they never manage
the iterative sequence necessary to uncover something interesting in
the data.

Hadley

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

Reply all
Reply to author
Forward
0 new messages