[R] for each element in matrix...

Petr Pikal petr.pikal at precheza.cz
Mon Feb 6 13:13:19 CET 2006


Hi

a bit of organisetion in colour sequence will do it.

graph1 <- function(a) {
    rownames(a) <- 1:nrow(a)
    colnames(a) <- 1:ncol(a)
    mean.diagonal <- mean(diag(a))

 bar<-c("red","green")[(a>mean(diag(a)))+1]
 bar<-matrix(bar[9:1],3,3)[3:1,]  # you need to set these according # 
# to actual dimensions of your matrices e.g. 
# bar<-matrix(bar[length(a):1],dim(a)[1],dim(a)[2])[dim(a)[1]:1,]

    par(bg = "gray95")
    dotchart(a, cex = 0.9, main = "MeSH Plot", xlab = "frequency",
        bg = bar,  # !!!
            pch = 21,labels = rownames(a))
    abline(v = mean.diagonal, col = "red", lty = 4)
}

HTH
Petr


On 5 Feb 2006 at 21:21, Andrej Kastrin wrote:

Date sent:      	Sun, 05 Feb 2006 21:21:20 +0100
From:           	Andrej Kastrin <andrej.kastrin at siol.net>
To:             	R-help <r-help at stat.math.ethz.ch>
Subject:        	[R] for each element in matrix...

> 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
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

Petr Pikal
petr.pikal at precheza.cz




More information about the R-help mailing list