[R] Stacked barplot with two stacked bars besides each other

Daniel Brewer daniel.brewer at icr.ac.uk
Tue Jan 20 13:22:09 CET 2009


Thanks Henrique.  Unfortunately, that gets us a step closer but it fails
to stack the individual bars, so instead of having a total of 10 for
each bar you get 7, 8, 9 & 5 (i.e. the largest for each var sample
pair).  I tried adding the stack=T option, but that stacks all of each
sample and you are left with two bars.  Any more ideas?

Appreciate your help

Dan

Henrique Dallazuanna wrote:
> One option is to use lattice package:
> 
> library(lattice)
> newVar <- cbind(stack(rbind(var1, var2)), Var = rep(c("var1", "var2"),
> each = 2))
> barchart(values ~ ind, groups = Var, data = newVar)
> 
> 
> On Tue, Jan 20, 2009 at 9:03 AM, Daniel Brewer <daniel.brewer at icr.ac.uk
> <mailto:daniel.brewer at icr.ac.uk>> wrote:
> 
>     Thanks.
>     That is definitely in the right direction, but firstly I would like
>     yoda1:var1 next to yoda1:var2, not as currently yoda1:var1, yoda2:var1,
>     yoda1:var2, yoda2:var2.  Additionally, I would like the gap between
>     samples to be greater than the gap between variables.
> 
>     Many thanks
> 
>     Dan
> 
>     Henrique Dallazuanna wrote:
>     > Try this:
>     >
>     > barplot(cbind(as.matrix(var1), as.matrix(var2)), names.arg =
>     LETTERS[1:4])
>     >
>     > On Tue, Jan 20, 2009 at 8:28 AM, Daniel Brewer
>     <daniel.brewer at icr.ac.uk <mailto:daniel.brewer at icr.ac.uk>
>     > <mailto:daniel.brewer at icr.ac.uk <mailto:daniel.brewer at icr.ac.uk>>>
>     wrote:
>     >
>     >     Hi,
>     >
>     >     I have a particular barplot I would like to generate, but I am
>     having
>     >     trouble getting it to work.  What I would like is in effect
>     two barplots
>     >      with stacked bars merged into one.  For example,  I have two
>     samples
>     >     (yoda1,yoda2) on which I measure whether two variables
>     (var1,var2) are
>     >     present or absent for a number of measurements on that sample.
>     >
>     >     > var1 <- data.frame(yoda1=c(3,7), yoda2=c(1,9))
>     >     > var2 <- data.frame(yoda1=c(8,2), yoda2=c(5,5))
>     >
>     >     For each variable I can plot a barplot
>     >
>     >     > barplot(as.matrix(var1))
>     >     > barplot(as.matrix(var2))
>     >
>     >     I would like to join these together, so that for each sample
>     there are
>     >     two stacked bars next to each other, one for var1 and the
>     other for
>     >     var2.  I was thinking something like:
>     >
>     >     > barplot(list(as.matrix(var1),as.matrix(var2)))
>     >
>     >     would work, but it didn't.
>     >
>     >     Any suggestions you could make would be great.
>     >
>     >     Dan

-- 
**************************************************************

Daniel Brewer, Ph.D.

Institute of Cancer Research
Molecular Carcinogenesis
MUCRC
15 Cotswold Road
Sutton, Surrey SM2 5NG
United Kingdom

Tel: +44 (0) 20 8722 4109

Email: daniel.brewer at icr.ac.uk

**************************************************************

The Institute of Cancer Research: Royal Cancer Hospital, a charitable Company Limited by Guarantee, Registered in England under Company No. 534147 with its Registered Office at 123 Old Brompton Road, London SW7 3RP.

This e-mail message is confidential and for use by the a...{{dropped:2}}




More information about the R-help mailing list