[R] Barplot coloring question

Marc Schwartz mschwartz at medanalytics.com
Fri Jul 26 16:41:56 CEST 2002

> On Thu, 25 Jul 2002, Marc Schwartz wrote in response:
> > The "list" of colors that you have specified for colorlist contains
> > one entry.
> >
> > If you specify:
> >
> > colorlist <- c("red", "blue", ...)
> >
> > You bars or their stacked segments will cycle through the colors,
> > depending upon whether beside is TRUE or FALSE, respectively.
> >
> > Just be sure to specify one color for each group in your data.
> Right. I had tried that earlier but that alone didn't work; sorry, I
> should have mentioned that.  But having beside as TRUE works (with a
> of colours). I wanted to keep beside = FALSE because I wanted to
> the space between bars, as I decribe below with toy examples.
> In the first kind of barplot I want, I have data like this:
>    A A  B  B  C  C
> 0  5 10 15 20 70 90
> I can plot it using code like this:
> data2 <- as.matrix(read.table("test2.data"))
> colorlist2 <- c("orange","limegreen")
> barplot(data2,
>         beside=TRUE,
>         col=colorlist2,
>         width = c(1,.55),
>         space = c(2.75,.25),
>         axes = F
>         )
> I want to be able to manipulate the spaces between the bars pairwise:
> contiguous AA should be closer to each other than the contiguous AB. I
> thought that a vector specification for space would do this, because
> works for the plot of a slightly different dataset that I have
> But it doesn't in the above case.
> Here's an example where "space" DOES work as (I) expected:
> The data set:
>    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
> The code:
> data3 <- as.matrix(read.table("test3.data"))
> colorlist3 <- c("orange","lightblue","darkblue")
> barplot(data3,
>         beside=FALSE,
>         col=colorlist3,
>         xlim = c(1,6),
>         width = c(.5,.35),
>         space = c(2.75,.25),
>         axes = F
>         )
> As expected, "colorlist" in both examples cycles through the list of
> colors, but when beside = TRUE it uses each color for each column in
> data, and when beside = FALSE the list of colors is cycled through for
> each bar's components. In the *first* case, I'm trying to get the two
> colors to alternate (which works only when beside = TRUE), but for
> reason I then lose control over "space".
> Any suggestions would be greatly appreciated.

If I read the information correctly, the problem with your first example
is with the structure of the data2 matrix (and with your original set of
data that was 30 columns by 1 row.)

Your data2 set is a one dimensional vector of values not a matrix, even
though you are trying to coerce it in the read.table() step.  If you do
dim(data2), you will find that it is a 1 x 6 vector.  

Take a look at R-FAQ 7.7.

With data3, you have created a matrix that is 3 x 6.  Do dim(data3).

Some of the output that you are seeing from data3 is the result of the
way in which barplot() is handling the sorting of the data labels and
the space arguments that you have specified (which are backwards), not
because the data is being grouped.

In fact, if you reverse your space arguments in your call to barplot()
for data3 (or just leave them out) and set beside = TRUE, you will note
that the common lettered groups are not being grouped at all. They are
being treated as unique entries, each of which has three values.

What you really need to do is to restructure your data so that the
values for the bar heights in common groups are arranged in single

For example, reconfigure data2 so that it looks like:

0 5 15 70 
1 10 20 90

Then call barplot() as follows:

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

Take note (as I reference above) that I also reversed the sequence of
the values for space from your original.  The first value is the within
group space, the second value is the between group space.

You should do the same restructuring with your first set of data so that
it becomes a 2 row by 15 column matrix. Right now it is a 1 x 30 vector,
which barplot() will not handle properly when grouping bars, thus the
space argument is not functioning as you wish.

Sorry that I missed this the first time around.  I must not have had my
afternoon coffee yet.  ;-)



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