[R] for each element in matrix...

Andrej Kastrin andrej.kastrin at siol.net
Sun Feb 5 21:21:20 CET 2006


Dead R useRs,

I wrote function, which plot dotchart from given matrix, compute mean 
from diagonal elements and plot it with abline. In addition, if 
particular element of matrix is greater then mean value (i.e. 
mead.diagonal), it should be plot in red, otherwise in green color.

graph <- function(a) {
    rownames(a) <- 1:nrow(a)
    colnames(a) <- 1:ncol(a)
    mean.diagonal <- mean(a[row(a) == col(a)])
    par(bg = "gray95")
    dotchart(a, cex = 0.9, main = "MeSH Plot", xlab = "frequency",
        bg = ifelse((a) > mean.diagonal,"red", "green2"),  # !!!
            pch = 21,labels = rownames(a))
    abline(v = mean.diagonal, col = "red", lty = 4)
}

And now the main problem: I suppose that there is some mismatch in 
ifelse statement while I produce two totally different plots:
first input matrix:  A <-matrix(rep(c(1,3,4),3),3,3) # seem that works
second input matrix B <-matrix(rnorm(9),3,3) # total confusion between 
green and red points

Any advice would be appreciated,

Cheers, Andrej




More information about the R-help mailing list