[R] Barplot coloring question

Marc Schwartz mschwartz at medanalytics.com
Thu Jul 25 20:29:06 CEST 2002


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

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.

HTH.

Marc


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