[R] Help Creating a Stacked Bar Chart with Color Coding

Wu Gong wg2f at mtmail.mtsu.edu
Thu Jul 29 15:37:46 CEST 2010


It is a little complicate for me to transform the table. Hope it works for
you.

x <- read.table(textConnection("Johnson     4    
Smith        4
Smith        2
Smith        3
Garcia       1
Garcia       4
Rodriguez   2
Adams       2
Adams       3
Adams       4
Turner       4
Turner       3 "),header=F)
## sort data
x<-x[order(x[,1],-x[,2]),]
## add another column to be the index used for reshape
x$times<-sequence(rle(as.character(x[,1]))$lengths)

y <- t(reshape(x, timevar="times", idvar="V1", direction="wide"))
colnames(y)<-y[1,]

barplot(y[-1,],col=27:29)

-----
A R learner.
-- 
View this message in context: http://r.789695.n4.nabble.com/Help-Creating-a-Stacked-Bar-Chart-with-Color-Coding-tp2305487p2306464.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list