Thanks for the example. You can put an annotation of this sort on
fairly easily, but it is actually easiest to do if you make a
secondary annotation data set (either "by hand" or derived from your
original data set in some way):
data_annot <- data.frame(left=c(1,5), right=c(4,8),
label=c("Left","Right"))
ggplot(data) +
geom_rect(data=data_annot, aes(xmin=left, xmax=right, fill=label,
ymin=-Inf, ymax=Inf), alpha=0.2) +
geom_point(aes(x,y), colour=2) +
facet_wrap(~z, ncol=1, scales="free_y") +
theme_bw()
A couple of things of note. The aes() call in ggplot() has been
moved. That is because if it is there, every layer looks for both "x"
and "y", and the data set for the annotation layer does not have it
and so you get an error. The aes() is now in the geom_point() call.
Also, I put the geom_rect() layer before the geom_point() layer so
that it is drawn below it. To make it somewhat transparent, I made
the alpha 0.2. The y limits are set to -Inf and Inf so that the bars
cover the whole vertical range, no matter what it is (the scales don't
expand to try and take -Inf and Inf in). Fill colors can be set with
one of the scale_fill()'s
You can even go a step further and put the specific filling colors in
the annotation set:
data_annot <- data.frame(left=c(1,5), right=c(4,8),
fill=c("gray80","gray90"))
ggplot(data) +
geom_rect(data=data_annot, aes(xmin=left, xmax=right, fill=fill,
ymin=-Inf, ymax=Inf)) +
geom_point(aes(x,y), colour=2) +
facet_wrap(~z, ncol=1, scales="free_y") +
scale_fill_identity(legend=FALSE) +
theme_bw()
Here I used scale_fill_identity() to indicate that the value of the
variable actually has the color names to be used. Also, I got rid of
the transparency; since it is behind the points, you may not need it
(although if not transparent, the grid lines can't be seen). If you
really want to use transparency, set the fill to black and use alpha
as an aesthetic set to an appropriate variable in the annotation data
set:
data_annot <- data.frame(left=c(1,5), right=c(4,8), alpha=c(0.2, 0.1))
ggplot(data) +
geom_rect(data=data_annot, aes(xmin=left, xmax=right, alpha=alpha),
ymin=-Inf, ymax=Inf, fill="black") +
geom_point(aes(x,y), colour=2) +
facet_wrap(~z, ncol=1, scales="free_y") +
scale_alpha_identity(legend=FALSE) +
theme_bw()
Where you have to get scale_alpha_identity() from
http://groups.google.com/group/ggplot2/browse_thread/thread/f8576a044fd09944
(code from Hadley reproduced below):
scale_alpha_identity <- function (name = NULL, breaks = NULL, labels =
NULL, formatter = NULL,
legend = TRUE, ...) ScaleIdentity$new(name = name, breaks =
breaks, labels = labels,
formatter = formatter, legend = legend, variable = "alpha", ...)
> I hope I have explained the problem clear enough.
>
> Thanks in advance!
>
> Sam
--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University
> I hope I have explained the problem clear enough.
>
> Thanks in advance!
>
> Sam
--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University