by dhewitt » Mon Sep 29, 2008 12:52 pm
Apologies in advance, but this comes up enough with RMark that I think it's worth giving people a simple start.
If you have estimates for a series of years and your data frame looks like this:
Year Estimate Lower Upper
2002 0.4743 0.2821 0.8462
2003 0.7767 0.641 0.9487
2004 0.7293 0.4872 0.9487
2005 0.5947 0.2821 0.9231
2006 0.5093 0.2051 0.8974
The plotCI() function in the plotrix package works well for this. Something like:
# Read in the data
ests <- read.table("estimates.txt", header=T)
# Eliminate white space (depends on output type)
oldpar <- par(mar=c(4.5, 5.5, 1, 1))
# Plot the estimates and CIs, but suppress axes
plotCI(ests$Year, ests$Estimate, yaxt="n", xaxt="n",
ui=ests$Upper, li=ests$Lower, gap=0.02, sfrac=0.01,
lwd=2, cex=1.6, pch=21, pt.bg="blue",
ylab="", xlab="", ylim=c(0, 1), cex.axis=1.5)
# Add the x-axis
axis(1, at=ests$Year, las=2, lwd=1.5, cex.axis=1.5)
# Add the y-axis
axis(2, las=1, lwd=1.5, cex.axis=1.5)
# Make the outline of the box a little thicker
box(lty=1, lwd=2)
# Add the y-axis label
mtext(side=2, line=4, "Probability of occupancy", cex=1.8)
# Reset the graphical parameters
par(oldpar)
If your x-axis categories are not years (integer variables), you'll have to do some more tweaking. One easy option is to coerce your factor (e.g., habitat types A, B, C, and D) into integers with as.integer() wrapped around the variable in the plotCI() call. Then the x-axis call above could be modified coarsely to make it what you want... e.g.,
axis(1, at=c(1, 2, 3, 4), lwd=1.5, cex.axis=1.5,
labels=c("A", "B", "C", "D"))