[R] overlaid transparent histograms

Austin Frank austin.frank at gmail.com
Sat Jun 14 01:06:05 CEST 2008

Hello all--

I'm attempting to produce overlaid histograms with partially transparent
columns.  Whether this display will end up being useful, I can't say.
But I do want to get it right.

I've already got one solution (shown below), but I tried some other
versions and had questions about my results.  (Note:  I'm using a quartz
device, so transparency shows up correctly.  You might need to print to
a pdf device to get transparency, according to the docs I've read)

--8<---------------cut here---------------start------------->8---
## Working version:
data(lexdec, package="languageR")

x <- log(c(BNCw, Frequency))
label <-  c(rep("BNCw", length(BNCw)),
            rep("CELEX", length(Frequency)))
h <- data.frame(x, label)

g <- ggplot(h, aes(x=x, fill=label))
g +
  geom_bar(position="identity") +
  scale_fill_manual(values = c(
                      alpha("red", 0.5),
                      alpha("blue", 0.5)))
--8<---------------cut here---------------end--------------->8---

Three questions:
1a)  Why does the following code not produce transparent bars?
1b)  How can I manually specify the elements of the legend for this
     version of the plot?

--8<---------------cut here---------------start------------->8---
## Non-working version
data(lexdec, package="languageR")

g <- ggplot(lexdec)
g +
  geom_histogram(aes(x=log(BNCw), fill = alpha("red", .5))) +
  geom_histogram(aes(x=log(BNCc), fill = alpha("blue", .5)))
--8<---------------cut here---------------end--------------->8---

2) Does anyone have a way to accomplish the same thing in lattice?  I
   saw the post at
   but couldn't figure out how to extend these suggestions to overlaid
   transparent histograms.

Thanks in advance for any help,

> sessionInfo()
R version 2.7.0 (2008-04-22) 


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

other attached packages:
 [1] ggplot2_0.6        colorspace_0.95    RColorBrewer_1.0-2 MASS_7.2-42       
 [5] proto_0.3-8        reshape_0.8.0      languageR_0.92     coda_0.13-2       
 [9] lme4_0.999375-15   Matrix_0.999375-10 zipfR_0.6-0        lattice_0.17-8    
[13] Design_2.1-1       survival_2.34-1    Hmisc_3.4-3

Austin Frank
GPG Public Key (D7398C2F): http://aufrank.net/personal.asc
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 193 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20080613/0ac91d0e/attachment.bin>

More information about the R-help mailing list