15 Overlaying plots

data("swiss")

str(swiss)

fertility = swiss$Fertility

Draw Histogram

data("swiss")

str(swiss)

fertility = swiss$Fertility

h = hist(fertility,

probability = TRUE,

ylim = c(0, 0.04),

xlim = c(20, 110),

breaks = 11,

col = "#EFEFEF",

border = 0,

xlab = "Fertility",

main = "Swiss Fertility Data\n 1888")

#Draw normal distribution curve

curve(dnorm(x, mean = mean(fertility), sd = sd(fertility)), col = "darkred", lwd = 3, add = TRUE)

#Kernel Density Plot ... follows the average

lines(density(fertility), col = "blue")

lines(density(fertility, adjust = 3), col = "green")

#A rug representation (1-d plot) of the data to the plot.

rug(fertility, col = "red")

x = 1:10

y1 = runif(10)

y2 = runif(10)

plot(x, y1, col = "steelblue", pch = 19, xlab = "X", ylab = "", ylim = c(0, 1))

points(x, y2, col = "red", pch = 19)

lines(x, y1, col = "steelblue")

lines(x, y2, col = "red")

legend(8, 1.0, c("Y1", "Y2"), col = c("steelblue", "red"), lty = c(1, 1))

x = 1:10

y1 = runif(10)

y2 = runif(10)

par(mfrow = c(1, 2))

plot(x, y1, col = "steelblue", pch = 19)

lines(x, y1, col = "steelblue")

plot(x, y2, col = "red", pch = 19)

lines(x, y2, col = "red")

For practice: