Problem with use of geom_rect and alpha

260 views
Skip to first unread message

Gerry

unread,
May 5, 2010, 4:24:06 AM5/5/10
to ggplot2
Hi list, I have a problem with getting the syntax correct for
geom_rect with a semi-transparent fill which I am hoping someone may
be able to help me with.

I have multiple graphs to produce which need a semi-transparent red,
yellow and green rectangle drawn over the top of the grid (with the
lines showing through) prior to plotting boxplots ( minus the
whiskers) and data points. I have a number of variables to produce
these for, and I am setting it up to be a repeatable process as this
report is produced annually.

The problem is that the transparency of the rectangles varies
depending on how many data points are plotted using geom_point. I had
assumed that as this layer was plotted first after the grid, that
anything following would be plotted "on top", but my code doesn't seem
to work in this respect. It can be fixed using a Mark 1 Eyeball
device to vary the alpha, but I have many variables to plot separately
and would like a consistent and repeatable style through the report
which can be run automatically by a user. Please see the code below
as an example. Your assistance is appreciated!


df <- structure(list(z = structure(c(1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L,
1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L,
2L, 2L), .Label = c("f", "m"), class = "factor"), wst = c(125,
115, 132.6, 94, 98.5, 128, 105.2, 117, 133, 121, 100.5, 125,
94, 109.5, 104.3, 125, 104.6, 108.3, 102, 113.8, 110.6, 130,
NA, 111.3, 124.3, 125, 121, 132, 105.2, 126, NA, 102.5, 124.6,
105, NA, 120.5, 96.4, 103, 125, 84.6, 93.6, 116, 91, NA, 106,
124, NA, NA, 95.3, 98, 99, 93.2, 110, NA, 95, NA, 114.5, 93.6,
125, 125, 97, 97, 94, 74, 112, 120, 110, NA, 100.2, NA, 101.6,
114.6, 134.6, 98, 121.5, 122, 130.6, 105.6, 122, 113.5, 125.6,
85.3, NA, 89.9, 90.5, 116.5, 101, 81.5, 112, 99.6, 105.6, 72.5,
131, 87.3, 95, 110, 93.6, 117, NA, 111, 117.3, 101.6, 84.3, 111.3,
90.1, 98.6, 106.3, 93.1, 120.5, 123.6, 115.3, 132, 120.6, 109.2,
122.3, 103.3, 125.6, 111.3, 129.6, 69.3, 125, 102.5, 96, 101,
121.5, 135, 105.6, 106.3, 127.5, 91.5, 96, 112.5, 134.5, 114.3,
86.2, 112, NA)), .Names = c("z", "wst"), class = "data.frame",
row.names = c(NA,
137L))

library(ggplot2)
#################set up theme for graphs############################
base_size = 10
jtheme <- opts(axis.text.x = theme_text(colour = "black", size =
base_size * 0.8, vjust = 1, lineheight = 0.9),
axis.text.y = theme_text(colour = "black", size = base_size * 0.8,
hjust = 1, lineheight = 0.9),
plot.title= theme_text(face="bold", size = base_size * 1.2),
axis.title.x = theme_text(face = "bold", size = base_size, vjust =
0.5),
axis.title.y = theme_text(face = "bold", size = base_size, vjust =
0.5, angle = 90))


dev.new()
wsP <- subset(df, z == "f")
ymingr <- min(wsP$wst, na.rm = T) * .90
ymaxr <- max(wsP$wst, na.rm = T) * 1.05
fwxp <- ggplot(wsP, aes(y = wst, x = as.character(""))) +
geom_blank()

fwxp1 <- fwxp + geom_rect(mapping=NULL,
data=NULL,xmin=0,ymin=ymingr[1],xmax=2,
ymax=80,fill=alpha("green", 1/150)) + geom_rect(mapping=NULL,
data=NULL,xmin=0,ymin=80,xmax=2,
ymax=88,fill=alpha("yellow", 1/150)) + geom_rect(mapping=NULL,
data=NULL,xmin=0,ymin=88,xmax=2,
ymax=ymaxr[1],fill= alpha("red", 1/150))

fwxp2 <- fwxp1 + geom_boxplot( ymin = quantile(wsP$wst, probs =
c(0.25), na.rm = TRUE),
ymax = quantile(wsP$wst, probs = c(0.75),
na.rm = TRUE) ,width=.8, size =1) + scale_x_discrete("")

fwxp3 <- fwxp2 + geom_point(shape = 1,size = 3,
colour = "black") + ylab("Female wst")
print(fwxp3 + opts(axis.title.x = theme_blank(), axis.text.x =
theme_blank()) + jtheme)

dev.new()
wsP <- subset(df, z == "m")
ymingr <- min(wsP$wst, na.rm = T) * .90
ymaxr <- max(wsP$wst, na.rm = T) * 1.05
mwxp <- ggplot(wsP, aes(y = wst, x = as.character(""))) +
geom_blank()

mwxp1 <- mwxp + geom_rect(mapping=NULL,
data=NULL,xmin=0,ymin=ymingr[1],xmax=2,
ymax=94,fill=alpha("green", 1/150)) + geom_rect(mapping=NULL,
data=NULL,xmin=0,ymin=94,xmax=2,
ymax=102,fill=alpha("yellow", 1/150)) + geom_rect(mapping=NULL,
data=NULL,xmin=0,ymin=102,xmax=2,
ymax=ymaxr[1],fill= alpha("red", 1/150))

mwxp2 <- mwxp1 + geom_boxplot( ymin = quantile(wsP$wst, probs =
c(0.25), na.rm = TRUE),
ymax = quantile(wsP$wst, probs = c(0.75),
na.rm = TRUE) ,width=.8, size =1) + scale_x_discrete("")

mwxp3 <- mwxp2 + geom_point(shape = 1,size = 3,
colour = "black") + ylab("Male wst")
print(mwxp3 + opts(axis.title.x = theme_blank(), axis.text.x =
theme_blank()) + jtheme)


# sessionInfo()
#R version 2.10.1 (2009-12-14)
#i386-pc-mingw32

#locale:
#[1] LC_COLLATE=English_Australia.1252 LC_CTYPE=English_Australia.
1252
#[3] LC_MONETARY=English_Australia.1252
LC_NUMERIC=C
#[5] LC_TIME=English_Australia.1252

#attached base packages:
# [1] grDevices datasets grid splines graphics stats
tcltk
# [8] utils methods base

#other attached packages:
# [1] ggplot2_0.8.7 digest_0.4.2 reshape_0.8.3 plyr_0.1.9
# [5] proto_0.3-8 svSocket_0.9-48 TinnR_1.0.3 R2HTML_2.0.0
# [9] Hmisc_3.7-0 survival_2.35-8 rcom_2.2-1 rscproxy_1.3-1

#loaded via a namespace (and not attached):
#[1] cluster_1.12.3 lattice_0.18-3 svMisc_0.9-56 tools_2.10.1

--
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 ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

hadley wickham

unread,
May 5, 2010, 3:48:09 PM5/5/10
to Gerry, ggplot2
Hi Gerry,

Would you mind making your example a little more minimal? That is,
extract out just the pieces that are causing you problems, and remove
everything that currently works (e.g. all the themes). That will make
it much easier to figure out what the problem is.

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

Gerry

unread,
May 5, 2010, 5:37:47 PM5/5/10
to ggplot2
Hadley, with pleasure! Here we go.

In a nutshell: how can I plot the semi-transparent rectangles
underneath the boxplot and the data and maintain a consistent
transparency via code?
Thanks - Gerry
##2 plots to demonstrate difference in transparency levels
# plot1
dev.new()
wsP <- subset(df, z == "f")
ymingr <- min(wsP$wst, na.rm = T) * .90
ymaxr <- max(wsP$wst, na.rm = T) * 1.05

fwxp <- ggplot(wsP, aes(y = wst, x = as.character(""))) +
geom_blank()
fwxp1 <- fwxp + geom_rect(mapping=NULL,
data=NULL,xmin=0,ymin=ymingr[1],xmax=2,
ymax=80,fill=alpha("green", 1/150)) + geom_rect(mapping=NULL,
data=NULL,xmin=0,ymin=80,xmax=2,
ymax=88,fill=alpha("yellow", 1/150)) + geom_rect(mapping=NULL,
data=NULL,xmin=0,ymin=88,xmax=2,
ymax=ymaxr[1],fill= alpha("red", 1/150))
fwxp2 <- fwxp1 + geom_boxplot() + geom_point() + xlab("")
fwxp2
# plot 2
dev.new()
wsP <- subset(df, z == "m")
ymingr <- min(wsP$wst, na.rm = T) * .90
ymaxr <- max(wsP$wst, na.rm = T) * 1.05
mwxp <- ggplot(wsP, aes(y = wst, x = as.character(""))) +
geom_blank()
mwxp1 <- mwxp + geom_rect(mapping=NULL,
data=NULL,xmin=0,ymin=ymingr[1],xmax=2,
ymax=94,fill=alpha("green", 1/150)) + geom_rect(mapping=NULL,
data=NULL,xmin=0,ymin=94,xmax=2,
ymax=102,fill=alpha("yellow", 1/150)) + geom_rect(mapping=NULL,
data=NULL,xmin=0,ymin=102,xmax=2,
ymax=ymaxr[1],fill= alpha("red", 1/150))
mwxp2 <- mwxp1 + geom_boxplot() + geom_point() + xlab("")
mwxp2

On May 6, 5:48 am, hadley wickham <h.wick...@gmail.com> wrote:
> Hi Gerry,
>
> Would you mind making your example a little more minimal?  That is,
> extract out just the pieces that are causing you problems, and remove
> everything that currently works (e.g. all the themes).  That will make
> it much easier to figure out what the problem is.
>
> Hadley

hadley wickham

unread,
May 5, 2010, 6:16:11 PM5/5/10
to Gerry, ggplot2
Hi Gerry,

Ok, there are two problems with your code:

* There's a bug so that when you set data = NULL, the layer still
uses the default dataset
* When you set mapping = NULL that means (by design), just use the
defaults - you want (outside of aes) inherit.aes = FALSE

The easiest way to resolve both of these problems is to create another
data frame that specifies the different ranges and their colours. Then
use geom_rect with that dataset. Also note that you can use -Inf and
Inf to refer to the left and right extents of the plotting area
respectively.

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

Gerry Deakin

unread,
May 5, 2010, 6:21:28 PM5/5/10
to hadley wickham, ggplot2
Thanks Hadley - much appreciated!
Cheers
Gerry
No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 9.0.814 / Virus Database: 271.1.1/2854 - Release Date: 05/05/10
04:27:00
Reply all
Reply to author
Forward
0 new messages