Nathan S. Watson-Haigh
2009-Feb-03 01:50 UTC
[R] pairs() help - colour histograms on diagonal
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I'd like to be able to colour histograms along the diagonal using the colours stored in colnames(d):> dblack blue brown cyan 1 0.96405751 -0.02964390 -0.060147424 -0.06460070 2 -0.03614607 0.95475444 -0.152382053 -0.07767974 3 -0.07095613 -0.05884884 -0.061289399 -0.06445973 4 -0.03708223 -0.05997624 -0.054044275 -0.08291373 5 -0.08877190 -0.07193658 -0.078598617 -0.08892916 6 -0.09294377 -0.05563854 -0.051405213 -0.08442332 7 -0.08431200 -0.01657793 -0.119773022 -0.07364633 8 -0.06105875 -0.05311773 -0.062928495 -0.06982507 9 -0.05757523 -0.02589045 -0.102312333 -0.05616588 10 -0.05092343 -0.03935830 -0.062069716 -0.05402492 11 -0.08057353 -0.12690058 -0.004248301 -0.06850326 12 -0.08052613 -0.04962747 -0.098955086 -0.06496541 13 -0.07901151 -0.07587651 -0.077401999 0.96525294 14 -0.07187448 -0.15431262 0.952982852 -0.06471004 15 -0.07230232 -0.13704876 0.032573081 -0.05040565 panel.hist <- function(x, ...) { # get some graphical parameter settings, and reset them on exit usr <- par("usr") on.exit(par(usr)) par(usr = c(usr[1:2], 0, 1.5) ) # get a histogram of the data, but don't plot it - we just need to get some info from the histogram h <- hist(x, plot = FALSE) breaks <- h$breaks nB <- length(breaks) y <- h$counts; y <- y/max(y) rect(breaks[-nB], 0, breaks[-1], y, ...) } pairs(d, upper.panel=panel.smooth, diag.panel=panel.hist) So I'd like the top-left histogram on the diagonal to be coloured black, then the next one on the diagonal to be coloured blue etc. Is this possible? Cheers, Nathan - -- - -------------------------------------------------------- Dr. Nathan S. Watson-Haigh OCE Post Doctoral Fellow CSIRO Livestock Industries Queensland Bioscience Precinct St Lucia, QLD 4067 Australia Tel: +61 (0)7 3214 2922 Fax: +61 (0)7 3214 2900 Web: http://www.csiro.au/people/Nathan.Watson-Haigh.html - -------------------------------------------------------- -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkmHovYACgkQ9gTv6QYzVL5bUgCgw3EHQKS9WjO2AmtEks6x0Bh9 FLgAoIFpikJ903quFBaxQe5UVXAAbrnq =XRan -----END PGP SIGNATURE-----
Nathan S. Watson-Haigh wrote:> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > I'd like to be able to colour histograms along the diagonal using the colours > stored in colnames(d): > > >> d >> > black blue brown cyan > 1 0.96405751 -0.02964390 -0.060147424 -0.06460070 > 2 -0.03614607 0.95475444 -0.152382053 -0.07767974 > 3 -0.07095613 -0.05884884 -0.061289399 -0.06445973 > 4 -0.03708223 -0.05997624 -0.054044275 -0.08291373 > 5 -0.08877190 -0.07193658 -0.078598617 -0.08892916 > 6 -0.09294377 -0.05563854 -0.051405213 -0.08442332 > 7 -0.08431200 -0.01657793 -0.119773022 -0.07364633 > 8 -0.06105875 -0.05311773 -0.062928495 -0.06982507 > 9 -0.05757523 -0.02589045 -0.102312333 -0.05616588 > 10 -0.05092343 -0.03935830 -0.062069716 -0.05402492 > 11 -0.08057353 -0.12690058 -0.004248301 -0.06850326 > 12 -0.08052613 -0.04962747 -0.098955086 -0.06496541 > 13 -0.07901151 -0.07587651 -0.077401999 0.96525294 > 14 -0.07187448 -0.15431262 0.952982852 -0.06471004 > 15 -0.07230232 -0.13704876 0.032573081 -0.05040565 > > > > So I'd like the top-left histogram on the diagonal to be coloured black, then > the next one on the diagonal to be coloured blue etc. Is this possible? > >Normally information about what is being plotted isn't passed to the panel function, and attributes of the columns are stripped off before passing, so this isn't easy. If you want to do some ugly programming, you can look up the variable i in the sys.frame(2) environment; that will be the column number. While you're at it, you might as well get the data too: it's called x there. For example, d <- data.frame(black=rnorm(100), blue=rnorm(100), brown=rnorm(100), cyan=rnorm(100)) panel.hist <- function(x, ...) { # get some graphical parameter settings, and reset them on exit usr <- par("usr") on.exit(par(usr)) par(usr = c(usr[1:2], 0, 1.5) ) # get a histogram of the data, but don't plot it - we just need to get some info from the histogram h <- hist(x, plot = FALSE) breaks <- h$breaks nB <- length(breaks) y <- h$counts; y <- y/max(y) colnum <- parent.frame(2)$i x <- parent.frame(2)$x colour <- colnames(x)[colnum] rect(breaks[-nB], 0, breaks[-1], y, col=colour, ...) } pairs(d, upper.panel=panel.smooth, diag.panel=panel.hist) Duncan Murdoch> Cheers, > Nathan > > - -- > - -------------------------------------------------------- > Dr. Nathan S. Watson-Haigh > OCE Post Doctoral Fellow > CSIRO Livestock Industries > Queensland Bioscience Precinct > St Lucia, QLD 4067 > Australia > > Tel: +61 (0)7 3214 2922 > Fax: +61 (0)7 3214 2900 > Web: http://www.csiro.au/people/Nathan.Watson-Haigh.html > - -------------------------------------------------------- > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.9 (MingW32) > Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org > > iEYEARECAAYFAkmHovYACgkQ9gTv6QYzVL5bUgCgw3EHQKS9WjO2AmtEks6x0Bh9 > FLgAoIFpikJ903quFBaxQe5UVXAAbrnq > =XRan > -----END PGP SIGNATURE----- > > ______________________________________________ > 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. >