Doll (1955) showed per capita consumption of cigarettes in 11 countries in 1930, and the death rates from lung cancer for men in 1950. Use R base graphics to generate the plot shown below. Source: Freedman, et al. (1997). Statistics. pp. 148-150.
Column 1: Country names Column 2: Cigarettes consumption (per million) Column 3: Death rate (per capita)
loading data and check data structure, display first 6 rows of data
## 'data.frame': 11 obs. of 3 variables:
## $ Country : Factor w/ 11 levels "Australia","Canada",..: 1 2 3 4 10 5 6 7 8 9 ...
## $ consumption: int 480 500 380 1100 1100 230 490 250 300 510 ...
## $ death : int 180 150 170 350 460 60 240 90 110 250 ...
## [[1]]
## Country consumption death
## 1 Australia 480 180
## 2 Canada 500 150
## 3 Denmark 380 170
## 4 Finland 1100 350
## 5 UK 1100 460
## 6 Iceland 230 60
## 7 Netherlands 490 240
## 8 Norway 250 90
## 9 Sweden 300 110
## 10 Switzerland 510 250
## 11 USA 1300 200
with(dta, plot(consumption, death, xlab="Cigarettes consumption (per million)", ylab="Death rate (per capita)", type='n', main='Ling Cancer and Cigarette Consumption'))
abline(lm(death ~ consumption, data=dta), lty=3)
with(dta, text(consumption, death, labels=dta$Country, cex=.5))In-class exercise 3.
Use R base graphics to create the national flag of Denmark.
op <- par(bg="aliceblue")
plot.new()
plot.window(xlim=c(1, 34),
ylim=c(1, 28))
abline(h=seq(1, 1, 1), lty=3,
col='white')
title(main='flag of Denmark')
rect(0, 0, 12, 12, col="red",
border="white")
rect(16, 0, 34, 12, col="red",
border="white")
rect(0, 16, 12, 28, col="red",
border="white")
rect(16, 16, 34, 28, col="red",
border="white")
segments(0, 0, 0, 28)
segments(0, 0, 34, 0)
segments(34, 0, 34, 28)
segments(0, 28, 34, 28) ## In-class exercise 4
#
n <- 60
t <- seq(0, 2*pi, length=n)
x <- sin(t)
y <- cos(t)
#
par(pty = "s")
#
for (i in 1:n) {
plot.new()
plot.window(c(-1, 1), c(-1, 1))
lines(x*y, -y, col="gray")
points(x[i]*y[i], -y[i], pch=16,
col=gray((i-1)/(n+1)))
Sys.sleep(.05)
}