[R] X Stacked bars of Y values, with bar segments colored according to Z

Jim Lemon drjimlemon at gmail.com
Wed Sep 30 10:44:51 CEST 2015


Hi Dale,
This apparently simple plot turned out to be fairly messy. "barplot"
doesn't seem to accept a matrix of colors, and "barp" doesn't yet do a
stacked plot, so I can only suggest a fairly labor intensive one-off plot:

barcol<-color.scale(x$mh,extremes=c("yellow","red"))
barheights<-rowSums(x[,2:4])
library(plotrix)
barp(barheights,xlab="x",main="Stacked bar plot")
rect(0.6,0,1.4,x[1,2],col=barcol[1])
rect(0.6,x[1,2]+x[1,3],1.4,barheights[1],col=barcol[1])
rect(1.6,0,2.4,x[2,2],col=barcol[2])
rect(1.6,x[2,2]+x[2,3],2.4,barheights[2],col=barcol[2])
rect(2.6,0,3.4,x[3,2],col=barcol[3])
rect(2.6,x[3,2]+x[3,3],3.4,barheights[3],col=barcol[3])
rect(3.6,0,4.4,x[4,2],col=barcol[4])
rect(3.6,x[4,2]+x[4,3],4.4,barheights[4],col=barcol[4])
rect(4.6,0,5.4,x[5,2],col=barcol[5])
rect(4.6,x[5,2]+x[5,3],5.4,barheights[5],col=barcol[5])

Can be streamlined a bit if it's worth the trouble

Jim


On Wed, Sep 30, 2015 at 4:54 AM, Daler <dale_ramsden at med.unc.edu> wrote:

> I'm trying to generate a bar plot from the following data frame
> seq     left    deleted right   mh
> 1       125     175     132     0
> 2       125     225     82      3
> 3       200     150     82      3
> 4       300     80      52      2
> 5       165     205     62      7
> where x="seq", y is a stack of "left", "deleted", and "right" values. then
> color left and right segments with color from palette (e.g. brewer
> yellow-orange-red) as determined by z="mh"
> Any help appreciated
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/X-Stacked-bars-of-Y-values-with-bar-segments-colored-according-to-Z-tp4712955.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list