[Return to tutorial page]
Statistics 200: Lab 3 (Friday 19 and 26 September 97)
Today's tasks:
Fancier graphics: beyond the defaults. Multiple plots per page,
split screens, "graphics frames".
We have saved some Census data in the Stat200 library (in the section
called nhcensus. You should attach that section of the
library to get access to the data set nhages, which is a list. ( See
HELP if you have forgotten about
libraries.)
The data give counts for New Haven population, broken down by race,
sex, and agegroups. (Here is the html
version, as it came from the Census lookup page. Don't try to copy
from the html; use the Stat200 library.)
This week I want you to write a series of functions that generate
population pyramids (similar to this example for Hartford County) for the New
Haven data.
Problem 1
Write a function to construct a population pyramid for white population of the
town. The plot should represent
percentages of the population for the various age groups, with females
on one side of the central vertical axis and males on the other.
Hint: ?barplot (Look at the various optional arguments to barplot.
You should find ways to tip a barplot on its side, to make bars stick
out in two directions, and other exciting variations.)
Problem 2
Improve your function from Problem 1:
- make sure the ages increase from the bottom of the pyramid.
- make the axis show percentages of population increasing in both
directions away from the central vertical axis.
- add a title
- fill in any gaps between the horizontal bars
- make sure any labels fit on the page
Hint: ?par, with its many arguments (see also remarks on the dreaded par())
; ?axis; ?pretty; more hints.
Problem 3
Improve your function from Problem 2:
group the population into 5 year age ranges, 0-4 years, 5-9 years, and
so on. Redraw the pyramid. Hint: ?paste, ?rep, ?tapply. Also, try this
little experiment, to see one way of collapsing categories:
junk <- rep(1:6,c(3,4,2,6,2,2))
garbage <- 1:length(junk)
squish <- tapply(garbage,junk,sum)
# Now use paste() to make suitable labels for the squish values.
Problem 4
Write a function to draw three little population pyramids in a row:
white, black, hispanic. Have a title on each pyramid, and a main title
for the plot. Hint: ?par, for mfrow, ...; ?split.screen