basegraphics_EXTRAS.R

etienne — Jun 6, 2013, 10:59 PM

# base graphics tutorial
# EXTRA MATERIAL
# Etienne Laliberte
# June 4, 2013


# tweaking plots
plot(pressure, type = "l", lwd = 3)

plot of chunk unnamed-chunk-1

plot(pressure, type = "l", col= "red")

plot of chunk unnamed-chunk-1

plot(pressure, type = "l", lty = "dashed")

plot of chunk unnamed-chunk-1

plot(pressure, type = "l", ylim = c(0, 1200))

plot of chunk unnamed-chunk-1



# customising graphics
# Type par() to list all values of parameters
# Some settings describe placement of plot
# Some settings describe aspects of device
# Some setting describe visual appearance
# par() has persistent effect
# calls within graphics function are temporary
# for all options, see
?par
starting httpd help server ...
done


# example of temporary effect within functions
# vs persistent effect of using par...
plot(pressure, col = "red")

plot of chunk unnamed-chunk-1

par(col = "red")
plot(pressure) # points AND box are red because of par() effect!
plot(pressure, col = "blue") # points are blue!

plot of chunk unnamed-chunk-1


# return to original colour
par(col = "black")


# adding text to a plot
plot(pressure, main = "Temperature-pressure relationship", las = 1,
     xlab = expression(Temperature~(degree*C)),
     ylab = "Pressure (mm Hg)")
eq <- expression(P == C~italic(e)^(-Delta*H[v] / RT ))
text(120, 700, eq)

plot of chunk unnamed-chunk-1

# for info on math expressions, see
?plotmath


# arranging multiple plots per file
par(mfrow = c(2, 1) )
plot(pressure, type = "p")
mtext("a", adj = 0, line = 1, font = 2, cex = 1.5)
plot(pressure, type = "l")
mtext("b", adj = 0, line = 1, font = 2, cex = 1.5)


# arranging multiple plots per file: layout
# allows you to have plots of different sizes
layout(matrix(1:6, byrow = T, ncol = 2),
   heights = c(2, 1, 1),
   widths = c(1, 2))

plot of chunk unnamed-chunk-1

layout.show(6)

plot of chunk unnamed-chunk-1

for (i in 1:6) plot(pressure)


# get normal layout
layout(matrix(1) )

plot of chunk unnamed-chunk-1



# 3D plots
head(volcano)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
[1,]  100  100  101  101  101  101  101  100  100   100   101   101   102
[2,]  101  101  102  102  102  102  102  101  101   101   102   102   103
[3,]  102  102  103  103  103  103  103  102  102   102   103   103   104
[4,]  103  103  104  104  104  104  104  103  103   103   103   104   104
[5,]  104  104  105  105  105  105  105  104  104   103   104   104   105
[6,]  105  105  105  106  106  106  106  105  105   104   104   105   105
     [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24]
[1,]   102   102   102   103   104   103   102   101   101   102   103
[2,]   103   103   103   104   105   104   103   102   102   103   105
[3,]   104   104   104   105   106   105   104   104   105   106   107
[4,]   104   105   105   106   107   106   106   106   107   108   110
[5,]   105   105   106   107   108   108   108   109   110   112   114
[6,]   106   106   107   109   110   110   112   113   115   116   118
     [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35]
[1,]   104   104   105   107   107   107   108   108   110   110   110
[2,]   106   106   107   109   110   110   110   110   111   112   113
[3,]   108   110   111   113   114   115   114   115   116   118   119
[4,]   111   114   117   118   117   119   120   121   122   124   125
[5,]   115   118   121   122   121   123   128   131   129   130   131
[6,]   119   121   124   126   126   129   134   137   137   136   136
     [,36] [,37] [,38] [,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46]
[1,]   110   110   110   110   110   108   108   108   107   107   108
[2,]   114   116   115   114   112   110   110   110   109   108   109
[3,]   119   121   121   120   118   116   114   112   111   110   110
[4,]   126   127   127   126   124   122   120   117   116   113   111
[5,]   131   132   132   131   130   128   126   122   119   115   114
[6,]   135   136   136   136   135   133   129   126   122   118   116
     [,47] [,48] [,49] [,50] [,51] [,52] [,53] [,54] [,55] [,56] [,57]
[1,]   108   108   108   108   107   107   107   107   106   106   105
[2,]   109   109   109   108   108   108   108   107   107   106   106
[3,]   110   110   109   109   109   109   108   108   107   107   106
[4,]   110   110   110   109   109   109   109   108   108   107   107
[5,]   112   110   110   110   110   110   109   109   108   107   107
[6,]   115   113   111   110   110   110   110   109   108   108   108
     [,58] [,59] [,60] [,61]
[1,]   105   104   104   103
[2,]   105   105   104   104
[3,]   106   105   105   104
[4,]   106   106   105   105
[5,]   107   106   106   105
[6,]   107   107   106   106
x <- 1:nrow(volcano)
y <- 1:ncol(volcano)
persp(x, y, volcano, theta = 135, phi = 30, shade = 0.75, col = "light blue")

plot of chunk unnamed-chunk-1

contour(x, y, volcano)

plot of chunk unnamed-chunk-1

image(x, y, volcano)

plot of chunk unnamed-chunk-1



# scatterplot matrix
pairs(iris[, 1:4])

plot of chunk unnamed-chunk-1



# a cooler version
#  copy and paste these three functions
panel.hist <- function(x, ...)
{
    usr <- par("usr"); on.exit(par(usr))
    par(usr = c(usr[1:2], 0, 1.5) )
    h <- hist(x, plot = FALSE)
    breaks <- h$breaks; nB <- length(breaks)
    y <- h$counts; y <- y/max(y)
    rect(breaks[-nB], 0, breaks[-1], y, col="grey", ...)
}
panel.cor <- function(x, y, digits=2, prefix="", cex.cor, ...)
{
    usr <- par("usr"); on.exit(par(usr))
    par(usr = c(0, 1, 0, 1))
    r <- cor(x, y)
    p.value <- cor.test(x, y)$p.value
    is.sign <- p.value <= 0.05
    txt <- format(c(r, 0.123456789), digits=digits)[1]
    txt <- paste(prefix, txt, sep="")
    if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt)
    if (is.sign) text(0.5, 0.5, txt, cex = 1.2 * cex.cor, font = 2)
    else text(0.5, 0.5, txt, cex = cex.cor, col = "grey50")
}
panel.smooth <- function (x, y, col = par("col"), bg = NA, pch = par("pch"), 
    cex = 1, col.smooth = "black", span = 2/3, iter = 3, ...) 
{
    p.value <- cor.test(x, y)$p.value
    is.sign <- p.value <= 0.05
    if (is.sign) points(x, y, pch = pch, col = "black", bg = bg, cex = cex)
    else points(x, y, pch = pch, col = "grey50", bg = bg, cex = cex)
    ok <- is.finite(x) & is.finite(y)
    if (any(ok)) {
        if (is.sign) lines(stats::lowess(x[ok], y[ok], f = span, iter = iter), 
            col = col.smooth, ...)
        else lines(stats::lowess(x[ok], y[ok], f = span, iter = iter), 
            col = "grey50", ...)
  }
}

# and now reate a new scatterplot matrix, way cooler!
pairs(iris[, 1:4], panel=panel.smooth, upper.panel = panel.cor, diag.panel = panel.hist)

plot of chunk unnamed-chunk-1