[R] Barplot coloring question

Shravan Vasishth vasishth at coli.uni-sb.de
Mon Jul 29 17:01:05 CEST 2002

Hi Marc,

Thanks for that very helpful explanation about barplot's innards.  Your
suggestion works perfectly for NR = 1, so that problem is resolved. Now on
to NR > 1. I am trying the following. I am trying to define a new
function, mybarplot, in which the assumption is that the colors are
presented as a matrix, so:

v <- c("red","yellow","green")

colormatrix <- cbind(v,v,v,v,v,v)

which gives:

> colormatrix
     v        v        v        v        v        v
[1,] "red"    "red"    "red"    "red"    "red"    "red"
[2,] "yellow" "yellow" "yellow" "yellow" "yellow" "yellow"
[3,] "green"  "green"  "green"  "green"  "green"  "green"

Now, the crucial change to the barplot code is that I add another

  if (beside)
            xyrect(0, w.l, c(height), w.r, horizontal = horiz,
                col = col)
        else {                                 
            for (i in 1:NC) {
                     for (j in 1:NR) { # new loop added
                       col = col[,i]
                       xyrect(height[1:NR, i], w.l[i], height[-1, i],
                            w.r[i], horizontal = horiz, col = col[j])

The reasoning here is that if beside = FALSE, and I have a data
structure data3 like this:

   A  A  B  B  C  C
1  5 10 15 20 70 90
2 20 15 24 56 34 70
3 40 54 23 67 12 78

then, for each column of data3, cycle through the colors "red", "yellow",
and "green". This means that for each column in data3, I successively do
the following for the j (= 3) rows of data3: get the color-vector
corresponding to the current column from data3 (I want to enforce this
correspondence for now so that I can have alternating triplets of colors
if I want). That is, I get the three colors in a vector (col = col[,i]),
and then go through these colors in turn, i.e., get the jth color each
time (this is the intended purpose of the inner for loop).

So, the call to mybarplot would be:

        col= colormatrix,
        width = c(1,.55),
        space = c(2.75,.25),  
        axes = F)        

But this doesn't work; I get the message:

"Error in col[, i] : incorrect number of dimensions" 

If you can easily see what's wrong here in my code, please do tell.
Meanwhile I will keep trying to figure out what I'm missing. 

A propos, how do I find out how exactly "xyrect" is defined?

Many thanks again for spending so much time on this. I'm learning a lot...

On Fri, 26 Jul 2002, Marc Schwartz wrote:

> Date: Fri, 26 Jul 2002 18:36:45 -0500
> From: Marc Schwartz <mschwartz at medanalytics.com>
> To: 'Shravan Vasishth' <vasishth at CoLi.Uni-SB.DE>, r-help at stat.math.ethz.ch
> Subject: RE: [R] Barplot coloring question 
> > I have snipped the original message text to conserve space, since my
> reply is long.
> The source of the problem that you are having in trying to get the
> colors to cycle in the case where you only have one row in your matrix
> (data2), is in the way that the rectangles are drawn in barplot(). The
> way it is coded, it precludes cycling the colors when beside = FALSE and
> there is only one vector of height values.


> The code snippet from barplot.default() calling (not defining) xyrect()
> follows:
> if (beside)
>    xyrect(0, w.l, c(height), w.r, horizontal=horiz, col = col)
> else {
>     for (i in 1:NC) {
>         xyrect(height[1:NR, i], w.l[i], height[-1, i], w.r[i],
> horizontal=horiz, col = col)
>     }
> }
> When beside = TRUE, each of the arguments is passed as a vector in the
> first call to xyrect(), thus normal R cycling occurs, including the
> colors.
> However, when beside = FALSE, note that the for loop passes arguments by
> explicit indexing in the second call to xyrect().  The loop repeats for
> each value in NC, which is the number of columns in the matrix.
> Note that "col" is not indexed (ie. col[i] ) in the looped call, thus
> only the first value for color is passed when NR = 1 (one vector of
> height values) and no cycling through the color vector occurs.  
> When NR > 1, cycling within the xyrect() call takes place and the color
> vector is cycled to allow for the multi-color segments in a stacked bar
> plot.
> Hence, you are stuck in this particular case, unless you want to modify
> barplot().
> By the way, if "col" was indexed as col[i] in the code, it would address
> your problem, but it would now preclude color cycling in stacked bars as
> "i" would increment with each matrix column and not within each bar
> segment.
> My guess is that the author of barplot allowed for color cycling in
> stacked and grouped bars, but not in the case of a single set of
> non-stacked bars.
> You could feasibly modify the code for the special case when NR = 1,
> such as you have in data2 to enable each bar to be a different color or
> pairs of colors as specified by the color vector. You would have to set
> up the code to properly configure the color vector or pass it explicitly
> in barplot.  For data2, the explicit argument could be something like:
>  "col = rep(c("orange", "yellow"), 3)" 
> which would create a vector of:
> [1] "orange" "yellow" "orange" "yellow" "orange" "yellow" 
> Not sure if that is what you wanted to hear, but I think that may be
> your only choice, unless others reading this have other ideas about how
> to approach the issue.
> The other alternative, would be as I had suggested earlier, which is to
> use beside = TRUE for the special case where your number of rows = 1,
> with the space arguments in a standard configuration and the data set up
> with multiple rows per group and only one column per group. In the cases
> where the number of rows > 1, then take the "beside = FALSE" approach.
> I think you have it right for when the number of rows in your data is >1
> using the reversed space argument values.  It seems to yield what you
> are trying to achieve, which is paired groupings of grouped bars.
> Hope that provides some direction. If I have missed anything else, let
> me know.
> Marc

Dr. Shravan Vasishth                           Phone: +49 (681) 302 4504    
Computational Linguistics, Universität des Saarlandes, Postfach 15 11 50
D-66041 Saarbrücken, Germany         http://www.coli.uni-sb.de/~vasishth 

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