Skip to content
Prev 279379 / 398506 Next

barplot ignoring col parameter

barplot() defaults to stacked bars for matrices and uses the colors
fresh for each bar: e.g.,

layout(1:2)
barplot(matrix(1:6, 2), col = c("red", "blue","green"))
barplot(matrix(1:6, 3), col = c("red", "blue","green"))

To get what you are looking for, try something like this:

x = matrix(1:6,1); colnames(x) = LETTERS[1:6]
barplot(as.vector(x), names.arg = colnames(x))

More generally, just turn the matrix into a vector

mat2vec <- function(x){
    stopifnot(any(dim(x) == 1L))
    if(nrow(x) != 1L) x <- t(x)
    cn <- colnames(x)
    x <- c(x)
    names(x) <- cn
    return(x)
}

and barplot that.

Hope this helps,

Michael

On Mon, Dec 5, 2011 at 11:41 AM, Federico Calboli
<f.calboli at imperial.ac.uk> wrote: