[R] Matrix-like plot

Henrik Bengtsson hb at maths.lth.se
Fri May 31 19:53:37 CEST 2002


On Fri, 31 May 2002, Gavin Simpson wrote:

> Dear List,
> 
> I have a 47 species * 83 samples matrix containing percentage abundance
> data.  I have two cluster analyses one of the samples and one of the
> species, and have ordered the rows and columns of the species by samples
> matrix according to these two cluster analyses.  So far so good!
> 
> Now what I want to do is create a plot with the species dendrogram at
> the top of the plot, the samples dendrogram to the left of the plot and
> then in the main plotting area plot the abundances held in the cells of
> the matrix.  I know how to split the plotting region up so that it looks
> somewhat like this:
> 
>          ----------------------
>         |                      |
>         |     dendro 1         |
>         |                      |
>          ----------------------
>  ------  ----------------------
> |      ||                      |
> |  d   ||                      |
> |  e   ||  Matrix-like plot    |
> |  n   ||                      |
> |  d   ||                      |
> |  r   ||                      |
> |  o   ||                      |
> |  2   ||                      |
> |      ||                      |
>  ------  ----------------------
> 
> What I do not know who to do is to plot the matrix-like plot.  I want to
> have a point for every value greater than zero in the matrix.  So that
> if in sample/row 1, species 1 and 2 were present at 25% and 75%
> respectively but species 3 was absent, I would want 2 points, one for
> each species present scaled relative to their abundance.  I presume what
> I would actually do is plot an expand.grid() object and vary the cex by
> the values in the matrix so that zero values had zero cex, but I am not
> sure how to go about doing this.  An alternative could be to use image()
> to plot coloured boxes for each cell based on the abundances.  I tried
> this initially like so:
> 
> > test <- expand.grid(x=1:47, y=1:83)
> > names(test)
> [1] "x" "y"
> > image(test, z=cladocera) #cladocera being the matrix containing the
> abundances
> Error in image.default(test, z = cladocera) : 
>         increasing x and y values expected
> 
> I am now stuck as to how generate the x and y points for image.
> 
> If anyone has any pointers I would be most grateful,
> 
> All the best
> 
> Gavin Simpson
> 
> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> Gavin Simpson                     [T] +44 (0)20 7679 5402
> ENSIS Research Fellow             [F] +44 (0)20 7679 7565
> ENSIS Ltd. & ECRC                 [E] gavin.simpson at ucl.ac.uk
> UCL Department of Geography       [W] http://www.ucl.ac.uk/~ucfagls/cv/
> 26 Bedford Way                    [W] http://www.ucl.ac.uk/~ucfagls/
> London.  WC1H 0AP.
> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

Below there is a runnable code (R --vanilla) that might shine some light
on the often confusing image() function.

Cheers

Henrik Bengtsson

Dept. of Mathematical Statistics @ Centre for Mathematical Sciences 
Lund Institute of Technology/Lund University, Sweden (+2h UTC)
Office: P316, +46 46 222 9611 (phone), +46 46 222 4623 (fax)
h b @ m a t h s . l t h . s e, http://www.maths.lth.se/bioinformatics/


############################################################################
# Matrix manipulation methods
############################################################################
# Converts an integer into a hexadecimal value
as.character.hexmode <- function(x) {
  hexDigit <- c(0:9, "A", "B", "C", "D", "E", "F")
  isna <- is.na(x)
  y <- x[!isna]
  ans0 <- character(length(y))
  while (any(y > 0)) {
    z <- y%%16
    y <- floor(y/16)
    ans0 <- paste(hexDigit[z + 1], ans0, sep = "")
  }
  ans <- rep("NA", length(x))
  ans[!isna] <- ans0
  ans
}

############################################################################
# Matrix manipulation methods
############################################################################
# Flip matrix (upside-down)
flip.matrix <- function(x) {
  mirror.matrix(rotate180.matrix(x))
}

# Mirror matrix (left-right)
mirror.matrix <- function(x) {
  xx <- as.data.frame(x);
  xx <- rev(xx);
  xx <- as.matrix(xx);
  xx;
}

# Rotate matrix 90 clockworks
rotate90.matrix <- function(x) {
  t(mirror.matrix(x))
}

# Rotate matrix 180 clockworks
rotate180.matrix <- function(x) { 
  xx <- rev(x);
  dim(xx) <- dim(x);
  xx;
}

# Rotate matrix 270 clockworks
rotate270.matrix <- function(x) {
  mirror.matrix(t(x))
}

############################################################################
# Color methods
############################################################################
# The inverse function to col2rgb()
rgb2col <- function(rgb) {
  rgb <- as.integer(rgb)
  class(rgb) <- "hexmode"
  rgb <- as.character(rgb)
  rgb <- matrix(rgb, nrow=3)
  paste("#", apply(rgb, MARGIN=2, FUN=paste, collapse=""), sep="")
}

getColorTable <- function(col) {
  # Convert all colors into format "#rrggbb"
  rgb <- col2rgb(col);
  col <- rgb2col(rgb);
  sort(unique(col))
}

############################################################################
# Draw methods
############################################################################
# 'colorTable' instead of 'col' to be more explicit.
image.matrix <- function(x, colorTable=NULL, xlab="x", ylab="y", ...) {
  image(x=1:ncol(x),y=1:nrow(x),z=rotate270.matrix(x), col=colorTable, xlab=xlab, ylab=ylab, ...)
}


############################################################################
# Example
############################################################################
opar <- par(ask=TRUE)

x <- y <- seq(-4 * pi, 4 * pi, len = 27)
r <- sqrt(outer(x^2, y^2, "+"))
z <- cos(r^2) * exp(-r/6)
z[2,] <- min(z) # To show that z[1,1] is in the
z[,2] <- max(z) # upper left corner.

colorTable <- gray((0:32)/32)
image.matrix(z, colorTable=colorTable)
image.matrix(t(z), colorTable=colorTable)
image.matrix(mirror.matrix(z), colorTable=colorTable)
image.matrix(flip.matrix(z), colorTable=colorTable)


img <- matrix("white", nrow=12, ncol=9)
img[2:3,c(2:3,7:8)] <- "blue"
img[5:6,5] <- "green"
img[9,c(3,7)] <- "red"
img[10,4:6] <- "red"

colorTable <- getColorTable(img)
z <- rgb2col(col2rgb(img)) # Assert format "#rrggbb"
z <- match(z, colorTable)
dim(z) <- dim(img)

image.matrix(z, colorTable=colorTable)
image.matrix(rotate90.matrix(z), colorTable=colorTable)

par(opar)

# End of File

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list