## par(): adj, mgp, srt, ...
layout(matrix(c(1, 3, 4, 2), 2))
par(mar = rep(1, 4))
plot(1:5, type = "n", xlab = "", ylab = "", xaxt = "n",
    yaxt = "n", xlim = c(0.5, 5.5))
grid(col = "gray")
text(2, 2, "adj=c(1, 1)", adj = c(1, 1), cex = 1.3,
    col = "gray30")
points(2, 2, pch = 3, cex = 5, lwd = 2)
text(2, 4, "adj=c(1, 0)", adj = c(1, 0), cex = 1.3,
    col = "gray30")
points(2, 4, pch = 3, cex = 5, lwd = 2)
text(3, 3, "adj=c(0.5, 0.5)", adj = c(0.5, 0.5), cex = 1.3,
    col = "gray30")
points(3, 3, pch = 3, cex = 5, lwd = 2)
text(4, 2, "adj=c(0, 1)", adj = c(0, 1), cex = 1.3,
    col = "gray30")
points(4, 2, pch = 3, cex = 5, lwd = 2)
text(4, 4, "adj=c(0, 0)", adj = c(0, 0), cex = 1.3,
    col = "gray30")
points(4, 4, pch = 3, cex = 5, lwd = 2)
plot(1, type = "n", xlim = c(0.5, 4.5), ylim = c(0.5,
    3.5), xaxt = "n", yaxt = "n", ann = F)
box()
for (i in 0:2) {
    lines(c(1 + i, 2 + i, 1 + i), c(1, 2, 3), ljoin = i, lwd = 30,
        col = "darkgray")
    points(2 + i, 2, pch = 19, cex = 1.2)
}
text(2.5, 0.7, "ljoin = 0, 1, 2", cex = 1.2)
par(tcl = 0.5, mar = c(5, 5, 4, 1))
plot(1:10, xlab = "", ylab = "", main = "tcl = 1")
text(3, 8, "srt = 30", srt = 30, cex = 1.2)
text(8, 3, "srt = 120", srt = 120, cex = 1.2)
par(mgp = c(5, 3, 1), mar = c(6, 6, 4, 1), tcl = -0.5)
plot(1:10, xlab = "X title", ylab = "Y title", main = "mgp = c(5, 3, 1)")

## bty, family, font...
par(mfrow = c(2, 2), mar = c(3, 3, 3, 2))
btype = c("l", "7", "c", "u")
fml = c("serif", "sans", "mono", "symbol")
for (i in 0:3) {
    plot(1:10, font.main = i + 1, las = i, pch = NA, bty = btype[i +
        1], main = paste("font.main = ", i + 1, "; las = ", i,
        "; bty = \"", btype[i + 1], "\"", sep = ""))
    if (i < 3)
        text(5, 8, paste("family = \"", fml[i + 1], "\"", sep = ""),
            cex = 1.4, family = fml[i + 1], col = "gray40")
    text(1, 6, paste("pch = ", i + 21, sep = ""), adj = c(0,
        0.5), col = "gray40", cex = 1.3)
    points(5:10, rep(6, 6), pch = i + 21, cex = 2)
    if (i < 3) {
        text(1, 4, paste("lend = ", i, sep = ""), adj = c(0,
            0.5), col = "gray40", cex = 1.3)
        segments(5, 4, 10, 4, lwd = 20, lend = i, col = "darkgray")
        points(c(5, 10), c(4, 4), pch = 19, )
    }
    text(1, 2, paste("lty = ", i + 1, sep = ""), adj = c(0, 0.5),
        col = "gray40", cex = 1.3)
    segments(5, 2, 10, 2, lwd = 2, lty = i + 1, col = "darkgray")
}

## margin illustration: mar
par(mar = c(0, 0, 0, 0), mfrow = c(1, 2))
plot.new()
rect(0, 0, 1, 1, col = "lightgray", border = NA)
rect(0.1, 0.1, 0.9, 0.9, col = "white")
rect(0.1 + 0.08, 0.1 + 0.1, 0.9 - 0.04, 0.9 - 0.08,
    col = "lightgray", lty = 2)
text(0.5, 0.95, "Outer Margin")
text(0.5, 0.86, "Figure Region")
text(0.5, 0.5, "Plot Region")
par(cex = 0.7)
text(0.05, 0.55, "oma[2]")
text(0.95, 0.5, "oma[4]")
text(0.5, 0.05, "oma[1]")
text(0.14, 0.45, "mar[2]")
text(0.5, 0.15, "mar[1]")
par(cex = 1)
plot.new()
rect(0, 0, 1, 1, col = "lightgray", border = NA)
rect(0.1, 0.1, 0.5, 0.9, col = "white")
rect(0.5, 0.1, 0.9, 0.9, col = "white")
rect(0.1 + 0.08, 0.1 + 0.1, 0.5 - 0.04, 0.9 - 0.08,
    col = "lightgray", lty = 2)
text(0.5, 0.95, "Outer Margin")
text(0.31, 0.7, "Figure 1")
text(0.31, 0.5, "Current\n Plot Region", cex = 0.7)
text(0.72, 0.75, "Figure 2")
text(0.5, 0.05, "oma[1]", cex = 0.7)

## type in plot.default()
par(mfrow = c(3, 3), mar = c(2, 2.5, 3, 2))
for (i in c("p", "l", "b", "c", "o", "h", "s", "S",
    "n")) {
    plot(c(1:5, 5:1), type = i, main = paste("Plot type: \"",
        i, "\"", sep = ""), xlab = "")
}

## plot() as a generic function
library(MASS)
plot(longley)
fit = lm.ridge(GNP.deflator ~ ., longley, lambda = seq(0, 0.1, 0.001))
class(fit)
methods(plot)
plot(fit)

## points()
par(mar = c(0.5, 0.5, 0.5, 0.5))
ipch <- 0:33
np <- length(ipch)
k <- floor(sqrt(np))
dd <- c(-1, 1)/2
rx <- dd + range(ix <- ipch%/%k)
ry <- dd + range(iy <- 3 + (k - 1) - ipch%%k)
pch <- as.list(ipch)
pch[26 + 1:8] <- as.list(c("*", ".", "o", "O", "0",
    "+", "-", "|"))
plot(rx, ry, type = "n", axes = FALSE, ann = FALSE)
abline(v = ix, h = iy, col = "gray", lty = "dotted")
for (i in 1:np) {
    pc <- pch[[i]]
    points(ix[i], iy[i], pch = pc, bg = "yellow", cex = 3)
    text(ix[i] - 0.3, iy[i], pc, col = "brown", cex = 1.2)
}
box()

## control variable in regression
set.seed(123)
x = seq(0, 4, length = 100)
z = rep(0:4, each = 20)
y = -x + z +rnorm(100,0,.25)
coef(summary(lm(y ~ x)))
coef(summary(lm(y ~ x + z)))
library(rggobi)
ggobi(data.frame(x,y,factor(z)))
