** par(mfrow=())** can be used to split the graphics window into the specified number of panels

```
> par(mfrow=c(2,2))
# Split the graphics display into 4 (two rows, two columns)
> boxplot(bio$tb ~ bio$Treat, main= "Total Biomass")
> boxplot(bio$repro_bio ~ bio$Treat, main= "Reproductive Biomass")
> boxplot(bio$num_scapes ~ bio$Treat, main= "Number of Scapes")
> boxplot(bio$perc_repr ~ bio$Treat, main= "Reproductive Allocation")
```

** abline()** can be used to add straight lines on the current plot

```
> x <- runif(n=20, min = 0, max = 10)
> y <- rnorm(n=20, mean = 1.2*x, sd = 2)
> plot(y~x)
> abline(v = mean(x), col = "red") # add a vertical line (red) at the mean of the x-values
> abline(h = mean(y), col = "blue") # add a horizontal line (blue) at the mean of the y-values
> abline(a = 0, b = 1, lwd = 2) # add the one-to-one line (thick black)
> abline(lm(y~x), lty = 2) # add a best-fit line (dashed) by feeding it the linear regression
```

** curve()** is good for plotting functions like pdfs

```
> curve(dgamma(x, shape = 5, rate = 2), from = 0.1, to = 10)
> curve(dgamma(x, shape = 5, rate = 1/2), from = 0.1, to = 10, add = T, lty = 2) # dashed line
```

… or your own functions

```
> curve(exp(-1/x), from = 0, to = 20)
> curve(x/(x+5), add = T, lty = 3) #dotted line
```

** identify()** identifies the points you click on on a graph. It is useful when you are working with medium-sized datasets or when you see "outlying" points…

```
> heights <- rnorm(6, mean = 160, sd = 16)
> weights <- rnorm(6, mean = 75, sd = 10)
> names <- c("alice", "ben", "charles", "donna", "eric", "frank")
> plot(weights ~ heights)
> identify(x = heights, y = weights, labels = names)
[1] 1 3 4
```

** coplot()** can be used for dividing up a scatterplot according to one or more factors or grouping variables.

```
> set.seed(1)
> Time <- c(runif(20, min = 0, max = 15), runif(20, min = 10, max = 25), runif(20, min = 20, max = 25) )
> SVL <- rnorm(60, mean = 10 * Time/2, sd = 15)
> InitSize <- rep( c("Large", "Medium", "Small"), each = 20)
> coplot(SVL ~ Time | InitSize, rows = 1)
```

```
> Food <- rep( rep( c("High", "low"), each = 10), times = 3)
> coplot(SVL ~ Time | InitSize + Food, rows = 2)
```

layout() can be used to divide up the plotting area into multiple subplots. You can find an example provided by Duncan Temple Long

** ifelse()** can be useful for making step functions in a curve. Form is: ifelse (if, then, else). You can have multiple ifelse's building off of each other.

`>curve(ifelse(x<25,0,ifelse(x>75,0,ifelse(x<50,(1/25)*x-1,(-1/25)*x+3)))`

** stripchart()** can be useful for making boxplots with individual values overlaying the boxplots. This may be appropriate if the number of individual observations in each group is very unbalanced.

```
>boxplot(Animal~Sex, data = SalData, boxfill="light green", xlab = "Gender", ylab = "Number on Individuals")
with(SalData, stripchart(Animal~Sex, method="jitter", vertical=TRUE, add=TRUE, col="dark green"))
```

** xyplot() {lattice}** is another option for dividing a plot area into subplots. It seems like a nice easy shortcut, but I haven't been able to add a legend using the locator. See example from HW#2.

```
xyplot(SVL ~ DayToMeta | Food.Treatment, groups = InitialSize, pch = c(0,1,2),
+ xlab = "Days to Metamorphosis", ylab = "SVL (mm)")
```

** ask = T** is a graphical parameter. If you have multiple graphs specified in a chunk of code you will find that only the last graph created is available in the graphics device. Setting par(ask = T) will stop on the first graph with each successive graph displayed only after clicking on the graphical device.

This can be problematic when using the locator(1) argument as your graph will dissapear when locating the legend, but is otherwise useful.

`> par(ask = T)`

I struggled a long time to put a superscript on an axis in a plot…here's the solution.

`> xlab=expression(paste("Patch size (",km^2,")",sep=""))`

[[/code]]

Plotting in 3D requires that your data be stored in a matrix, such as that acquired from a nested loop that varies two parameters (say slope and sample size simultaneously). Once you have that matrix, you might use either a contour() or persp() function to plot your data.

```
a <- 2 # Specify a
bvec <- seq(from = 0, to = 1.9, by = 0.1) # Specify values of b to loop over
nvec <- seq(from = 10, to = 105, by = 5) # Specify sample size values to loop over
nsim <- 200 # Simulations to run
pval <- numeric(nsim) # Create vector to hold the significance level for your estimated b value
power <- matrix(nrow = length(bvec), ncol = length(nvec)) # matrix to record the power for a given b and sample size
for (k in 1:length(nvec)) { # Step through each sample size in turn
n <- nvec[k]
x <- seq(from = 1, to = 20, length = n)
for (j in 1:length(bvec)) { # For the specified sample sample, step through each b value in turn
b <- bvec[j]
for (i in 1:nsim) { # For the specified sample size and b value, step through each of nsim
y_det <- a + b * x
y <- rnorm(n, mean = y_det, sd = 8)
m <- lm(y ~ x)
pval[i] <- coef(summary(m)) ["x", "Pr(>|t|)"] # For each simulation record the pvalue
}
power[j,k] <- sum(pval < 0.05) / nsim # Summarize power across the nsim simulations
# Store output in row j (set by b), and column k (set by sample size)
}
}
# Contour Plot
contour(x=seq(from = 0, to = 1.9, by = 0.1), y = seq(from = 10, to = 105, by = 5), z = power, levels = c(0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0), xlab = "Slope", ylab = "Sample Size")
```

```
# 3D shaded perspective plot
persp(nvec, bvec, power, theta = 30, phi = 20, col = "lightgray", shade = 0.5, ticktype = "detailed", xlab = "Sample Size", ylab = "Slope (b)", zlab = "Power")
```