[R] Back-to-back graph with lattice package

Erik Svensson erik.b.svensson at gmail.com
Fri Oct 30 14:11:58 CET 2009

I am an [R] and lattice beginner.
I an doing an epidemiological study and want to present my data in a
condensed and clear manner as possible.

The example below is fake, but representative for the the data I want to show.

I have a data frame with different categorical data and one column of
numerical data:

Country, Hair, Eyes, Sex, Age
sweden, blond, blue, male, 25
sweden, dark, brown, female, 30
denmark, dark, blue, male, 22
denmark, blond, blue, female 33
norway, dark, blue, female, 21
norway, blond, blue, male, 31
finland, dark, blond, male, 43
island, blond, blue, female, 40
...and so on for 300 rows...

Country <- as.factor(rep(c("Sweden", "Denmark", "Norway", "Finland",
"Iceland","Sweden", "Denmark", "Norway", "Finland","Sweden" ),50))
Hair <- as.factor(rep(c("blond",  "dark", "blond", "dark", "blond"),100))
Eyes <- as.factor(rep(c("blue",  "blue", "blue", "brown"),125))
Sex <- as.factor(rep(c("male",  "female", "female", "male"),125))
Age <- rep(c(1:20, 1:100, 76:80),4)

Country has 5 levels, Age has many.
Hair, Eyes, and Sex have two levels each

I want to do one lattice graph with 5 columns of panels with these
descriptive data.
I would prefer to use the lattice barchart or the corresponding
dotplot with lines (except for the Age panel)

The  Age panel and probably the Country panel I think I can solve myself:
	barchart(Country)  # This only gives one panel, though...
	densityplot(~ Age|Country,  layout=c(1,5), xlim=c(0,100),scales =
list(y=list (relation="free")))

But, for the dichotomous variables Hair, Eyes, and Sex columns I would
like to have a bihistogram aka back-to-back aka age-sex-pyramid
I want all bars to be horizontal, the names of the countries to the
left on the y-axis.
I would also like to sort the country names according to a list of their names.

For the back-to-back graph  I have only managed to get two panels per
column instead of one:
	barchart(table(Country, Hair),groups=FALSE)

How can I do all this? Is it possible at all?

Erik Svensson

More information about the R-help mailing list