[R] barplot ignoring col parameter

R. Michael Weylandt michael.weylandt at gmail.com
Mon Dec 5 17:49:34 CET 2011


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:
>
> On 5 Dec 2011, at 15:58, R. Michael Weylandt wrote:
>
>> x <- c(2L, 108L, 0L, 0L, 0L, 1L, 3L, 0L, 0L, 0L, 0L, 0L, 7L, 18L,
>> 3L, 4L, 8L, 20L, 26L, 20L, 19L, 7L, 1L, 1L)
>> mycol = c(rep('yellow', 2), rep('white', 3), rep('orange',2),
>> rep('white', 5), rep('orange',3), rep('red',9))
>> barplot(x, col = mycol)
>>
>> Produces a multi-colored barplot on my machine so I understand your
>> confusion. Does the above work for you? It may be something hidden in
>> your data.
>
> It does work. Might it be that my data is a matrix? I'm using it to give a name for each bar.
>
>
>>
>> What is your sessionInfo() and str(mydata)? Same problem in a fresh R
>> session (with --vanilla if necessary)?
>
>
> sessionInfo()
> R version 2.14.0 (2011-10-31)
> Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
>
> locale:
> [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
>
> attached base packages:
> [1] splines   stats     graphics  grDevices utils     datasets  methods   base
>
> other attached packages:
> [1] survival_2.36-10
>
> loaded via a namespace (and not attached):
> [1] tools_2.14.0
>
>
> str(mydata)
> num [1, 1:24] 2 108 0 0 0 1 3 0 0 0 ...
> - attr(*, "dimnames")=List of 2
>  ..$ : NULL
>  ..$ : chr [1:24] "A" "B" "C" "D" ...
>
>
> Basically, using the 'x' object as you created the code works, and I can have a workaround. I fail to see why it would not work using mydata.
>
> Cheers
>
> F
>
>
>
>>
>> Michael
>>
>> On Mon, Dec 5, 2011 at 10:44 AM, Federico Calboli
>> <f.calboli at imperial.ac.uk> wrote:
>>> Hi All,
>>>
>>> I'm having a problem with barplot:
>>>
>>> mydata
>>> [1,]  2 108  0  0  0  1  3  0  0  0  0  0  7 18  3  4  8 20 26 20 19  7  1  1
>>>
>>> mycol = c(rep('yellow', 2), rep('white', 3), rep('orange',2), rep('white', 5), rep('orange',3), rep('red',9))
>>>
>>> barplot(mydata, col = mycol)
>>>
>>> gives me an uniformly yellow barplot. How do I solve this?
>>>
>>> bw
>>>
>>> Federico
>>>
>>>
>>>
>>> --
>>> Federico C. F. Calboli
>>> Neuroepidemiology and Ageing Research
>>> Imperial College, St. Mary's Campus
>>> Norfolk Place, London W2 1PG
>>>
>>> Tel +44 (0)20 75941602   Fax +44 (0)20 75943193
>>>
>>> f.calboli [.a.t] imperial.ac.uk
>>> f.calboli [.a.t] gmail.com
>>>
>>> ______________________________________________
>>> 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.
>
> --
> Federico C. F. Calboli
> Neuroepidemiology and Ageing Research
> Imperial College, St. Mary's Campus
> Norfolk Place, London W2 1PG
>
> Tel +44 (0)20 75941602   Fax +44 (0)20 75943193
>
> f.calboli [.a.t] imperial.ac.uk
> f.calboli [.a.t] gmail.com
>



More information about the R-help mailing list