[R] barchart with stacked and beside bars

RICHARD M. HEIBERGER rmh at temple.edu
Thu Jan 21 17:13:23 CET 2010


## There are at least two options, one using mosaic and the other
using barchart.
## Here are both.

## this is your data
ld<-matrix(c(25,25,50,10,30,60,15,35,50,30,30,40),nrow=4,byrow=T)
ntreat2<-c("n0","n96","n0","n96")
rownames(ld)<-ntreat2
typ<-c("gr","fo","le")
colnames(ld)<-typ

## now some rearrangements

dim(ld) <- c(2,2,3)
dimnames(ld) <- list(ntreat2=c("n0","n96"),
                     rep=c(1,2),
                     typ=c("gr","fo","le"))

ld.df <- as.data.frame.table(ld)

require(vcd)
mosaic(ld, direction=c("v","v","h"), highlighting="typ")

require(lattice)
barchart(Freq ~ rep|ntreat2, group=typ, data=ld.df,
         origin=0, stack=TRUE)


## both mosaic and barchart have many arguments that you can use for
## finer control of the appearance.



More information about the R-help mailing list