Question 1: The time series below gives the
annual totals of car drivers killed or seriously injured in Great
Britain between 1969 and 1984.
| year | DriverDeaths |
|---|---|
| 1969 | 19492 |
| 1970 | 19521 |
| 1971 | 20503 |
| 1972 | 21253 |
| 1973 | 20086 |
| 1974 | 19796 |
| 1975 | 21152 |
| 1976 | 20999 |
| 1977 | 20029 |
| 1978 | 18738 |
| 1979 | 20636 |
| 1980 | 19725 |
| 1981 | 19285 |
| 1982 | 18975 |
| 1983 | 19951 |
| 1984 | 20558 |
Display these data using an appropriate graph. Your graph should include:
Possible improvements you already know about:
bty="l", pch=16, las=1, cex=1.5. Find out what each of
these arguments do by adding/removing them one at a time.
Finally, run and try to understand the following R code:
plot(dat$year, dat$DriverDeaths, bty="l", type="o", col="red", xlab="year", ylab="number of deaths", main="UK driver death data", xaxt="n", lwd=1.5)
axis(side=1, at=1969:1984, las=2)
### or, better:
plot(dat$year, dat$DriverDeaths, bty="l", type="o", col="red", xlab="year", ylab="number of deaths", main="UK driver death data", xaxt="n", lwd=1.5)
axis(side=1, at=1969:1984, labels=FALSE)
text(1969:1984, par("usr")[3] - 300, labels = 1969:1984, srt = 45, pos = 1, xpd = TRUE)
Question 2: The dataset below gives death rates in Virginia in 1940. Rates are measured per 1000 population per year. They are cross-classified by age group (rows) and population group (columns). The age groups (in years) are: 50–54, 55–59, 60–64, 65–69, 70–74 and the population groups are Rural/Male, Rural/Female, Urban/Male and Urban/Female.
| Age.group | Rural.Male | Rural.Female | Urban.Male | Urban.Female |
|---|---|---|---|---|
| 50-54 | 11.7 | 8.7 | 15.4 | 8.4 |
| 55-59 | 18.1 | 11.7 | 24.3 | 13.6 |
| 60-64 | 26.9 | 20.3 | 37.0 | 19.3 |
| 65-69 | 41.0 | 30.9 | 54.6 | 35.1 |
| 70-74 | 66.0 | 54.3 | 71.1 | 50.0 |
Display these data on an appropriate graph (or on a 2-pannel graph if you prefer). Your graph should include:
Finally, run and try to understand the following R code:
barplot(height = dat$Rural.Male, names.arg = dat$Age.group, xlab="Age group", ylab="Death rate (per 1000 pop)", main="Virginia death data", col=colors()[133])
### or, better:
barplot(height = t(as.matrix(dat[,2:5])), xlab="Age group", ylab="Death rate (per 1000 pop)", main="Virginia death data", col=colors()[c(87, 133, 123, 52 )], names.arg = dat$Age.group, beside=T)
legend(x="topleft", legend=names(dat)[2:5], fill=colors()[c(87, 133, 123, 52 )], bty="n", cex=.5)
#legend(x=2, y=70, legend=names(dat)[2:5], fill=colors()[c(87, 133, 123, 52 )], bty="n", cex=.5)
Question 3: The “airquality” dataset in R gives air
quality measurements in New York in 1973. Type: ?airquality
to obtain information on the different variables. Use an appropriate
graph to show the relationship between Temperature and Ozone pollution.
The maximum allowable ozone concentration recommended by the American
Society of Heating, Refrigerating and Air-Conditioning Engineers
(ASHRAE) in an air conditioned and ventilated space is 50ppb. Use the
segments() function in R to add a dashed line (argument
lty=2) to your graph that represents this threshold. Since
you’ve never heard of the segments(), type:
?segments
to obtain information about how to use it. Here is an example:
plot(0:1, 0:1, type="n")
segments(0.2, 0.1, 0.7, 0.8, lty=3)
Save your graph as a vector image in pdf format.
Finally, run and try to understand the following R code:
?airquality
transp <- function(col, alpha=.5){###this creates a custom function
res <- apply(col2rgb(col),2, function(c) rgb(c[1]/255, c[2]/255, c[3]/255, alpha))
return(res)
}
plot(airquality$Temp, airquality$Ozone, bty="l", xlab="Temperature (°F)", ylab="Ozone concentration (ppb)", las=1, pch=16, col=transp("blue", 0.5))
segments(min(airquality$Temp), 50, max(airquality$Temp), 50, lty=2, col="red")
Question 4: Run this mysterious command in R:
myVector <- rnorm(1000, 1, 2)
The myVector you just created is a series of 1000
numbers. Use an appropriate type of graph to show the distribution of
this variable. Your graph should include: * at least 2 colors * properly
labelled axes * a title Save your graph as a vector
image in pdf format.
Finally, run and try to understand the following R code:
myVector <- rnorm(1000, 1, 2)
hist(myVector, las=1, xlab="values", nclass=20, main="distribution of my values", col=transp("red", .6))