Julian Saunders

 
 
Julian@Imfene.org
+27 (071) 414-8041  

R tips


R is a free software environment for statistical computing and graphics. OK, it has a wicked learning curve, but once one has a handle on the basics there is very little one cannot accomplish with R. The R community is very large and eager to help out.

Below are solutions to problems that stumped me at first. I hope that this might help others solve similar problems a bit more quickly.

Create plots with dynamic titles

The following example demonstrates how to create six plots on one page with dynamic titles above each.

First let's populate a few variables.

size <- 1
p <- 0.5
k <- 40
observations <- c(15, 25, 50, 100, 500, 1000)
n <- length(observations)

Set the parameters for the plot window.

op <- par(mfrow = c(2, 3), pty = "s")

Create the plots within the following loop.

for (j in 1:n) {
     results <- rep(NA, observations[j])
     for (i in 1:observations[j]) {
         results[i] <- sum(rbinom(k, size, p))
     }
     title <- paste(c("Num. Obs.", observations[j], sep = " "))
     hist(results, ylab = "Y-axis label", xlab = "X-axis label",
         main = title)
}

images/plot-with-dynamic-titles.png