3.1 Working with graphs


In [ ]:
# win.metafile() png() jpeg() bmp() tiff() xfig() postscript()
# pdf("mygraph.pdf")
attach(mtcars)
plot(wt, mpg)
abline(lm(mpg ~ wt))
title("Regression of MPG on Weight")
detach(mtcars)
# dev.off()

3.2 A simple example


In [ ]:
dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
plot(dose, drugA, type = "b")

par to display and set the variables.

3.3 Graphical parameters


In [ ]:
par(no.readonly = TRUE)

In [ ]:
opar <- par(no.readonly = TRUE)
par(lty = 2, pch = 17)
plot(dose, drugA, type = "b")
par(opar)

3.3.1 symbol and lines


In [ ]:
plot(dose, drugA, type = "b", lty = 3, lwd = 3, pch = 15, cex = 2)

3.3.2 colors


In [ ]:
# col for default printing color, lines and pie accepts a vector of colors
# col = c("red", "black") will plot 1st red, 2nd black and 3rd red and so on.
# col.axis, col.lab, col.main (titles), col.sub (subtitles), fg, bg

# col = 1, col = "white", col = "#FFFFFF", col = rgb(1, 1, 1), col = hsv(0, 0, 1)
colors() # returns all available color names

In [ ]:
# These functions produce vector of colors
rainbow(10); heat.colors(10); terrain.colors(10); topo.colors(10); cm.colors(10)
# RColorBrewer also create palettes
# mycolor <- brew.pal(10, "Set1")

In [ ]:
n <- 10
mycolors <- rainbow(10)
pie(rep(1, n), labels = mycolors, col = mycolors)
mygray <- gray(0:n/n)
pie(rep(1, n), labels = mygray, col = mygray)

3.3.3 Text characteristics


In [ ]:
# cex for the main plot, I think it means the mark size, cex = 1.5 means 50% bigger and 0.5 means 50% smaller
# We also has cex.axis, cex.lab, cex.main, cex.sub

In [ ]:
# font to specify the style: 1 plain, 2 bold, 3 italic, 4 bold italic
# We also have *.axis, lab, main, sub

In [ ]:
# family to sepecify font family
# Report the fonts available to plot
names(pdfFonts())
names(quartzFonts()) # Mac
names(postscriptFonts())

In [ ]:
plot(dose, drugA, type = "b", font = 4, family = "mono", font.lab = 3, main = "lala", font.main = 3,
    cex = 3, cex.lab= 2, cex.main = 0.5, sub = "hehe")

3.3.4 margin


In [ ]:
dose  <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
opar <- par(no.readonly=TRUE)
par(pin=c(2, 3))
par(lwd=2, cex=1.5)
par(cex.axis=.75, font.axis=3)
plot(dose, drugA, type="b", pch=19, lty=2, col="red")
plot(dose, drugB, type="b", pch=23, lty=6, col="blue", bg="green")
par(opar)

3.4 Adding Text


In [ ]:
# main, sub, xlab, ylab, xlim, ylim
# Use

In [ ]:
plot(dose, drugA, type="b",
     col="red", lty=2, pch=2, lwd=2,
     main="Clinical Trials for Drug A",
     sub="This is hypothetical data",
     xlab="Dosage", ylab="Drug Response",
     xlim=c(0, 60), ylim=c(0, 70))
title(main="My Title", col.main="red",
              sub="My Subtitle", col.sub="blue",
              xlab="My X label", ylab="My Y label",
              col.lab="green", cex.lab=0.75)

3.4.2 Axes


In [ ]:
# Suppress axes annotation
# axes=False, xaxt="n", yaxt="n", ann="FALSE" (The axes name)
x <- c(1:10)
y <- x
z <- 10/x
opar <- par(no.readonly = TRUE)
par(mar = c(5, 4, 4, 8) + 0.1)
plot(x, y, type = "b", lty = 3, col = "red", yaxt = "n", ann = FALSE)
lines(x, z, type = "b", lty = 2, col = "blue", pch = 22)
axis(side = 2, at = y, col.axis = "red", las = 2)
axis(side = 4, at = z, labels = round(z, 2), col.axis = "blue", las = 2, cex.axis = 0.5)
mtext("y=1/x", side = 4, las = 2, cex.axis = 1.0, line = 3, col = "blue")
par(opar)

In [ ]:
plot(dose, drugA, type="b")
abline(h = c(30, 40, 50))

In [ ]: