[R] Stacked Bar Plot With Two Dependent Variables

Achim Zeileis Achim.Zeileis at uibk.ac.at
Wed Sep 18 09:42:41 CEST 2013


On Tue, 17 Sep 2013, Carabiniero wrote:

> Hi All,
>
> I need to construct a stacked bar plot with two independent (x) variables,
> where the stacking is x1 and the x-axis label is x2. Can someone help out
> with the code for this or provide a reference/example?

I'm not completely sure what exactly you are looking for but you might 
want to explore mosaic displays that generalize stacked barplot. For 
example you can do the following using the UCBAdmissions data. I treat 
"Admit" as the dependent and "Gender" and "Dept" as the explanatory 
variables:

## data and colors
ucb <- aperm(UCBAdmissions, 3:1)
gr <- gray.colors(2)[2:1]

## mosaic with alternating split direction
mosaicplot(ucb, col = gr, off = c(5, 3, 0))

## doubledecker style
mosaicplot(ucb, col = gr, dir = c("v", "v", "h"), off = c(6, 4, 0))

## or using vcd package
library("vcd")
mosaic(~ Dept + Gender + Admit, data = UCBAdmissions,
   gp = gpar(fill = gr), spacing = spacing_highlighting)
doubledecker(Admit ~ Dept + Gender, data = UCBAdmissions)

> Thank you,
> J
>
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Stacked-Bar-Plot-With-Two-Dependent-Variables-tp4676402.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org 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