[R] Barplot coloring question

Shravan Vasishth vasishth at coli.uni-sb.de
Tue Jul 30 11:33:08 CEST 2002


Hi all,

I summarize below the eventual solution I used following the discussion in
this thread, in case others are interested. This also answers Marc's
question below (why the inner loop?).

The problem:

I need to build two kinds of plots automatically; I need to keep things as
automated as possible, since a lot of plots will be generated in the
future and we just want to see the output quickly without having to fiddle
with the plots individually.

The first kind's initial data looks like this:  

> data2
  A  A  B  B  C  C
0 5 10 15 20 70 90

The second kind's data looks like this:

> data3
   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

For uniformity, we want to keep the format of the data identical in both
cases.

In both cases, I want the distance between the bars A-A, B-B, C-C, to be
closer to each other than the A-B, B-C distance, and I want to alternate
colors, so that the first A has color x and the second A has a different
color y. The x, y colors must alternate after that: the first B has x,
then the second B has y, etc.

However, in data3, I also want the three rows for a given column to appear
*within* a single bar, in three colors, say, c1, c2, c3, and the column
next to this column should have colors c4, c5, c6. So, now we want the
triplets <c1, c2, c3>, <c4,c5,c6> alternating for each successive bar. The
reason for this is that we want to display two related but different
percentages in the same graph, and we want to do this because then we can
quickly eyeball the graph and get maximum information from a single graph,
rather than having to pore over dozens of them and to compare bars
pairwise across different graphs (a sure way to lose your mind).

Solution: 

(for the reasons why this should be so, see Marc's previous responses)

For data2, barplot is rewritten as mybarplot1 (thanks to Marc) as 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[i])
            }

 ...


For data3, barplot is rewritten as mybarplot2 (again, thanks to Marc) as
follows:

   ...

   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) {
                       mycol = col[,i]
                       xyrect(height[1:NR, i], w.l[i], height[-1, i],
                            w.r[i], horizontal = horiz, col = mycol)
                     }
            }

   ...


Now, one nice thing is that we can just use the above modification, i.e.,
mybarplot2, for both data2 and data3, we don't need mybarplot1 any more.
Not a real big deal, but now we can use the same R script for all our
data, irrespective of whether it comes in in data2 or data3 format. Just
might make things easier for other people who will one day have to
read and/or modify this and related code at our end here.

On Mon, 29 Jul 2002, Marc Schwartz wrote:

> The question this whole process now raises is why the inner loop?

Does the above explanation answer that question?

> I am still a little confused as to how you want the colors structured.
> That is do you want each bar to contain the three colors, one for each
> segment, or do you want each of the three pairs of bars to be colored
> differently?

I want them to alternate, for data3, as follows:

c3 c6    c3 c6
c2 c5    c2 c5
c1 c4    c1 c4   ...
A  A     B  B 

Marc, I want to thank you again for the enormous amount of time you put
into this, and for your extraordinary patience as you educated me on
these revisions to the code. It would have taken much, much longer to
figure all this out without your help.

-- 
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