Exercise-1

x <- seq(-pi*2, 2*pi, .05)
z <- dnorm(x)
y <- dt(x, df=3)
plot(x, z, type="l", bty="L", xlab="Standard unit", ylab="Density")
polygon(c(x, rev(x)), c(y, rev(z)), col='aliceblue')
lines(x, y, col='cadetblue')

x <- seq(-pi*2, 2*pi, .05)
z <- dnorm(x)
y <- dt(x, df=30)
plot(x, z, type="l", bty="L", xlab="Standard unit", ylab="Density")
polygon(c(x, rev(x)), c(y, rev(z)), col='aliceblue')
lines(x, y, col='cadetblue')

When degree of freedom grows to a large number, t distribution will be very close to z distribution.

Exercise-2

# read data
dta_cigarettes <- read.table("cigarettes.txt", header = T)

# generic plot
plot(dta_cigarettes$consumption, dta_cigarettes$death,
     xlab = "Consumption (per capita per year)",
     ylab = "Death rate (per million)",
     type = "n")

# add title
title("Lung Cancer and Cigarette Consumption")

# draw points
text(dta_cigarettes$death ~ dta_cigarettes$consumption, 
     labels = dta_cigarettes$Country)

# linear regression line
abline(lm(dta_cigarettes$death ~ dta_cigarettes$consumption), lty = 2)

# grid
grid()

Exercise-3

plot.new()
plot.window(xlim = c(0, 8.5), ylim = c(0, 7))

# 2 rectangles of the left coulmun, from bottom to top
rect(0, c(0, 4), 3, c(3, 7), col = "#C60C30", lty=0, border = "#C60C30")

# 2 rectangles of the right coulumn, from bottom to top
rect(4, c(0, 4), 8.5, c(3, 7), col = "#C60C30", lty = 0, border = "#C60C30")

# draw gray borders for the flag
rect(0, 0, 8.5, 7, border = "gray")

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)
}

Plot images every 0.05 second for 60 times. Each time change the position and the color of the point slightly.

Exercise-5

plot.new()
plot.window(xlim = c(0, 8.5), ylim = c(0, 7))
fifty_shades <- seq(from = 0, to = 1, by = 1/50)

fifty_shades_gray <- rev(gray(fifty_shades))

pie(rep(2, 50), col = fifty_shades_gray, clockwise = T, cex = 1.0,
    radius = 1)