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

Marianne Promberger mpromber at psych.upenn.edu
Wed Jun 10 15:09:22 CEST 2009


Many thanks for the quick reply!

> d1 <- data.frame(Goodall=c(rep(1:3,5)), Better.adapt =
> c(rep(1,7),rep(2,8)),Second.adapt=c(rep(1:5,3)))
> library(ggplot2) 
> ggplot(d1, aes(x= Goodall, y = Better.adapt, colour=Second.adapt)) +
> geom_jitter(alpha = 0.2) + scale_colour_gradient(low = "white", high =
> "black")

Great, this is exactly what I was looking for.

In case someone else needs this in the future, for my original data, I
was able to add the faceting using:

ggplot(d1, aes(x= Goodall, y = Better.adapt, colour=Second.adapt)) +
geom_jitter(alpha = 0.6) + scale_colour_gradient(low = "white", high =
"black") + facet_wrap(~ Pol, ncol = 4)

(Pol has length 16, so it is nice to have it wrapped to four columns
using facet_wrap instead of facet_grid)

Thanks again,

Marianne


> Another option would be to convert Second.adapt to a factor and use
> scale_colour_manual
> 
> d1$Second <- factor(d1$Second.adapt)
> ggplot(d1, aes(x= Goodall, y = Better.adapt, colour=Second)) +
> geom_jitter(alpha = 0.9) + scale_colour_manual(values = c("white",
> "grey", "black", "green", "red"))
> 
> HTH,
> 
> Thierry
> 
> 
> ------------------------------------------------------------------------
> ----
> ir. Thierry Onkelinx
> Instituut voor natuur- en bosonderzoek / Research Institute for Nature
> and Forest
> Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
> methodology and quality assurance
> Gaverstraat 4
> 9500 Geraardsbergen
> Belgium
> tel. + 32 54/436 185
> Thierry.Onkelinx at inbo.be
> www.inbo.be
> 
> To call in the statistician after the experiment is done may be no more
> than asking him to perform a post-mortem examination: he may be able to
> say what the experiment died of.
> ~ Sir Ronald Aylmer Fisher
> 
> The plural of anecdote is not data.
> ~ Roger Brinner
> 
> The combination of some data and an aching desire for an answer does not
> ensure that a reasonable answer can be extracted from a given body of
> data.
> ~ John Tukey
>  
> -----Oorspronkelijk bericht-----
> Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> Namens Marianne Promberger
> Verzonden: woensdag 10 juni 2009 13:37
> Aan: R-help
> Onderwerp: [R] ggplot, qplot: alpha channel for colors corresponding to
> factor
> 
> 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=~P
> ol,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
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
> en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
> door een geldig ondertekend document. The views expressed in  this message 
> and any annex are purely those of the writer and may not be regarded as stating 
> an official position of INBO, as long as the message is not confirmed by a duly 
> signed document.
> 

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




More information about the R-help mailing list