[R] ggplot, qplot: alpha channel for colors corresponding to factor

Marianne Promberger mpromber at psych.upenn.edu
Wed Jun 10 13:37:08 CEST 2009


Hi,

I have a qplot like the one in the minimal example below, except I
also have faceting like this:

qplot(jitter(Goodall),jitter(Better.adapt),colour=Second.adapt,facets=~Pol,data=d1)

and with the real data I get quite a lot of overplotting, so I would
like to add an alpha channel.

In addition, I would like to be able to control which colors are used
for each value of Second.adapt (which takes 7 different values). With
my original data, qplot by default uses a range from blue to red,
which is quite suitable, but I probably need to change this to shades
of grey for publication. 

I tried
qplot(jitter(Goodall),jitter(Better.adapt),colour=I(alpha(Second.adapt,1/5)),facets=~Pol,data=d1)
but that does not give me what I want (There is no legend and I don't
think anything corresponds to the seven values of Second.adapt).

Minimal example:

Goodall <- c(rep(1:3,5))
Better.adapt <- c(rep(1,7),rep(2,8))
Second.adapt <- c(rep(1:5,3))
d1 <- data.frame(Goodall=c(Goodall),Better.adapt=c(Better.adapt),Second.adapt=c(Second.adapt))
library(ggplot2)
qplot(jitter(Goodall),jitter(Better.adapt),colour=Second.adapt,data=d1)
# follwoing doesn't work:
qplot(jitter(Goodall),jitter(Better.adapt),colour=I(alpha(Second.adapt,1/5)),data=d1)

Thanks for any pointers,

Marianne





-- 
Marianne Promberger PhD
http://www.psych.upenn.edu/~mpromber
PGP/GnuPG public key ID 80AD9916




More information about the R-help mailing list