[R] Grouped AND stacked bar charts possible in R?
MSchwartz at MedAnalytics.com
Tue Jun 22 20:32:40 CEST 2004
On Tue, 2004-06-22 at 10:54, Patrick Lenon wrote:
> Good day all,
> My statisticians want an R procedure that will produce grouped stacked
> barplots. Barplot will
> stack or group, but not both. The ftable function can produce a table
> of the exact form they want, but the barplot doesn't show all the
> divisions we want.
> For an example, here's the sample from the help file for "ftable:"
> ftable(Titanic, row.vars = 1:3)
> ftable(Titanic, row.vars = 1:2, col.vars = "Survived")
> ftable(Titanic, row.vars = 2:1, col.vars = "Survived")
> Now take it a step further to try to add another dimension:
> b <- ftable(Titanic, row.vars=1:3)
> Survived No Yes
> Class Sex Age
> 1st Male Child 0 5
> Adult 118 57
> Female Child 0 1
> Adult 4 140
> 2nd Male Child 0 11
> Adult 154 14
> Female Child 0 13
> Adult 13 80
> 3rd Male Child 35 13
> Adult 387 75
> Female Child 17 14
> Adult 89 76
> Crew Male Child 0 0
> Adult 670 192
> Female Child 0 0
> Adult 3 20
> barplot(b, beside=T))
> Neither resulting barplot is satisfactory. The first stacks all the
> subdivisions of "Survived = Yes" and "Survived = No" together. The
> second is closer because it creates two groups, but it lists
> combinations side-by-side that we'd like stacked. In the above example
> "No" and "Yes" would be stacked on bars labeled "Male" or "Female"
> in groups by Class.
> I've taken a look through the R-Help archives and looked through the
> contributed packages, but haven't found anything yet.
> If you have any thoughts how we might produce groups of stacked bars
> from an ftable, we would appreciate it.
I think that you are trying to plot too much information in a single
graphic. The result of a multi-dimensional barplot is likely to be very
difficult to interpret visually.
You would likely be better served to determine, within the multiple
dimensions, what your conditioning and grouping dimensions need to be
and then consider a lattice based plot.
I would urge you to consider using either barchart() or perhaps
dotplot() in lattice, which are designed to handle multivariable charts
of this nature.
Then for general information
for more function specific information and examples of graphics with
For the Titanic data that you have above, you could do something like:
# Convert the multi-dimensional table to a
# data frame. Assumes you have already done
MyData <- as.data.frame(Titanic)
# Take a look at the structure
Class Sex Age Survived Freq
1 1st Male Child No 0
2 2nd Male Child No 0
3 3rd Male Child No 35
4 Crew Male Child No 0
5 1st Female Child No 0
6 2nd Female Child No 0
7 3rd Female Child No 17
8 Crew Female Child No 0
9 1st Male Adult No 118
10 2nd Male Adult No 154
11 3rd Male Adult No 387
12 Crew Male Adult No 670
13 1st Female Adult No 4
14 2nd Female Adult No 13
15 3rd Female Adult No 89
16 Crew Female Adult No 3
17 1st Male Child Yes 5
18 2nd Male Child Yes 11
19 3rd Male Child Yes 13
20 Crew Male Child Yes 0
21 1st Female Child Yes 1
22 2nd Female Child Yes 13
23 3rd Female Child Yes 14
24 Crew Female Child Yes 0
25 1st Male Adult Yes 57
26 2nd Male Adult Yes 14
27 3rd Male Adult Yes 75
28 Crew Male Adult Yes 192
29 1st Female Adult Yes 140
30 2nd Female Adult Yes 80
31 3rd Female Adult Yes 76
32 Crew Female Adult Yes 20
# Now do a plot. Use 'library(lattice)' here first
# if you had not already done so above for help.
barchart(Freq ~ Survived | Age * Sex, groups = Class, data = MyData,
auto.key = list(points = FALSE, rectangles = TRUE, space
= "right", title = "Class", border = TRUE), xlab = "Survived",
ylim = c(0, 800))
The above barchart will create a four panel plot, where the four main
panels will contain the combinations of Sex and Age. Within each panel
will be two groups of bars, one each for the Survived Yes/No status.
Within each group will be one bar for each Class.
That is one quick way of grouping things, but you can alter that and
other plot attributes easily.
More information about the R-help