[R] barplot with different color combination for each bar

Kim Milferstedt milferst at uiuc.edu
Fri Mar 2 16:48:51 CET 2007


Hi,

I'd like to construct a somewhat unusual barplot. In "barplot" I use 
beside=F as I'd like to have stacked bars. The height of each bar is 
always the same. Information in my plot is coded in the color of the 
bar. I therefore need to be able so assign a different combination 
(or order) of colors to each individual stacked bar.

In the example below, the combination of colors for my plot is 
generated by X, Q, color and cols. These colors are supposed to fill 
the stacked bars with the height of H. However, only the first column 
of cols is used for all columns of H as "barplot" only allows me to 
assign one vector for the color scheme of the entire barplot.

Does anybody know a way how I can assign each bar a potentially 
unique color combination?

Thanks for your help!

Kim

X <- seq(1:6)
Q    <- matrix(sample(X, 60, replace = T), nrow=6, byrow = T)
H   <-  matrix(rep(1,60), nrow=6, byrow=T)

color   <-  c("blue", "orange", "gold", "indianred", "skyblue4", "lightblue")
cols <-     ifelse(
                 (Q ==1) , color[1],
                     ifelse(
                         (Q ==2), color[2],
                             ifelse(
                                 (Q ==3) , color[3],
                                     ifelse(
                                         (Q ==4), color[4],
                                             ifelse(
                                                 (Q ==5) , color[5], color[6]
                                                     )
                                             )
                                     )
                             )
                     )

barplot(
         H,
         col=cols,
         width = c(0.1),
         xlim = c(0,3),
         beside=F
         )

__________________________________________

Kim Milferstedt
University of Illinois at Urbana-Champaign
Department of Civil and Environmental Engineering
4125 Newmark Civil Engineering Laboratory
205 North Mathews Avenue MC-250
Urbana, IL 61801
USA
phone: (001) 217 333-9663
fax: (001) 217 333-6968
email: milferst at uiuc.edu
http://cee.uiuc.edu/research/morgenroth



More information about the R-help mailing list