[R] Barplot coloring question

Shravan Vasishth vasishth at coli.uni-sb.de
Fri Jul 26 14:18:03 CEST 2002


On Thu, 25 Jul 2002, Marc Schwartz wrote in response:

> The "list" of colors that you have specified for colorlist contains only
> 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 list
of colours). I wanted to keep beside = FALSE because I wanted to control
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: the
contiguous AA should be closer to each other than the contiguous AB. I
thought that a vector specification for space would do this, because this
works for the plot of a slightly different dataset that I have (below).
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 the
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 some
reason I then lose control over "space".

Any suggestions would be greatly appreciated.

Thanks,

> Date: Thu, 25 Jul 2002 13:29:06 -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
> 
> > Hi all,
> > 
> > I have the following dataset, call it test.data (30 columns, and one
> row
> > named "0"):
> > 
> >   ADVP  ADVP AP    AP   CONJ  CONJ CP    CP   DU    DU    INF   INF
> MWU
> > MWU  NP    NP    PP    PP    PPRT PPRT REL   REL  SMN SMN SSB  SSB SV1
> > SV1 TI    TI
> > 0 96.85 2.05 89.07 2.54 70.91 2.37 94.92 3.46 82.31 11.33 40.96 2.25
> 98.06
> > 3.43 90.77 17.63 86.60 10.78 60.27 1.32  93.27 0.97 77.60 12.46 64.98
> 3.32
> > 88.54 3.70 24.33 0.28
> > 
> > I'm trying to plot these data points so that any pair of identical
> columns
> > (e.g., ADVP) are plotted closer to each other than any neighbouring
> pairs.
> > 
> > This much is easy to do:
> > 
> > data<-as.matrix(read.table("test.data"))
> > 
> > colorlist <- c("red")
> > 
> > # build basic barplot:
> > 
> > barplot(data,
> >         beside=FALSE,     # don't put bars beside each other
> >         col=colorlist,    # list of colors
> >         xlim = c(1,40),   # x-axis limits
> >         width = .65,      # width of bar plots
> >         ylim=c(0, 140),   # y-axis limits
> >         space = c(1,.5),  # space between bars, and between bar-pairs
> >         axes = F          # don't plot y-axis yet
> >         )
> > 
> > This gives a graph with the same color for each bar.
> > 
> > The problem is that I want the first instance of a pair of identical
> > columns to have a certain colour, and the other one a different one.
> So,
> > the bars should really have an alternating pattern like red green red
> > green... and so on, not just one colour.
> > 
> > For the above dataset I could set beside=TRUE and transform the
> dataset to
> > two rows and 15 columns rather than the current 1x30, so something
> like:
> > 
> >    ADVP        (etc.)
> > 0a 96.85
> > 0b  2.05
> > 
> > but I will eventually need to plot a matrix like
> > 
> >   ADVP ADVP (etc.)
> > 1 30   40
> > 2 20   20
> > 3 10   5
> > 
> > and there I will want beside=FALSE because I want the 1, 2, 3 rows to
> > appear *within* a bar, but I still want pairs of bars for ADVP.
> > 
> > This is why I don't want to set beside=FALSE.
> > 
> > I'll be very grateful for any suggestions.
> 
> 
> HTH.
> 
> 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