[R] barplot with different color combination for each bar

Jonne Zutt j.zutt at tudelft.nl
Mon Mar 5 11:43:32 CET 2007


Hi, I'd suggest you use ?rect for this.
Here's an example (I did not check whether it's correct...)
I also improved (but not checked :) your definition of cols.

Jonne.

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 <- matrix(data=color[Q], ncol=10)

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

# New:
x11()
plot(0, 0, type="n", ylim=c(0,nrow(Q)), xlim=c(0,ncol(Q)), 
     xlab="xlabel", ylab="")
xleft <- rep(1:ncol(Q), each=nrow(Q))
ybottom <- rep(1:nrow(Q), times=ncol(Q))
rect(xleft-1, ybottom-1, xleft, ybottom, col=cols)

On Fri, 2007-03-02 at 09:48 -0600, Kim Milferstedt wrote:
> 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
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list