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
> 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/
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
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
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