[R] Help with color coded bar graph

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Fri Sep 7 22:17:41 CEST 2007


On Fri, 7 Sep 2007, Luis Naver wrote:

> I have a list of observations that are -1, 1 or 0.  I would like to
> represent them in a horizontal bar color coded based on value like a
> stacked bar graph. I can achieve this in the form of a png with the
> following code:
>
> A = floor(runif(10)*3) - 1
>
> png(width=100, height=10)
> par(mar=c(0,0,0,0))
> image(matrix(A), col=grey(c(0.1, 0.5, 0.9)))
> dev.off()

If I understand you correctly, you want a sequence of bars with equal 
height and colors coded by A (treated like a factor). So Maybe something 
like
   cA <- grey.colors(3)[factor(A)]
   barplot(rep(1, length(A)), col = cA, border = cA)
or
   barplot(rep(1, length(A)), col = cA, border = cA, space = 0,
     xaxs = "i", axes = FALSE)
?

hth,
Z


> However I would like to do this with one of the standard plotting
> tools (i.e. barplot) to take advantage of labels and multiple
> series.  Any help would be appreciated.
>
> - Luis Naver
>
> ______________________________________________
> 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