# Annotating a plot # R Graphics seminar # 11 February 2013 #NJG #################################################### # Standard calls for preserving defaults, and reading a csv opar <- par(no.readonly=TRUE) Ant.Data <- read.csv("antcountydata.csv",header=TRUE) attach(Ant.Data) plot(lat.centroid,n.species,log="y") # log transforms; use x,y, or xy plot(lat.centroid,n.species) abline(h=20) # drawing a horizontal line abline(v=46,lwd=2,lty="twodash",col="grey") # drawing a vertical line abline(a=-1600,b=40) # intercept and slope abline(lm(n.species~lat.centroid)) # standard regression lines(lowess(n.species~lat.centroid),col="blue",lty="dotdash",lwd=1.5) rug(lat.centroid) # show the density of data points rug(n.species,side=2) # can be done on any axis for any variable grid() box(which="outer") # box options: outer, inner, figure, plot box(col="gray",lwd=5) text(44,60,"my text here at 44,60") # cleaned up regression par(mar=c(4,6,2,2)+0.1) plot(lat.centroid,n.species, xlab="Latitude",ylab="Number of Species", las=1,cex.axis=1.2,cex.lab=1.5) reg.model <- lm(n.species~lat.centroid) abline(reg.model) rug(n.species,side=2) grid() # text annotations x <- 1:5 y <- seq(1,5,1) plot(x,y) text(1,1,"right",pos=4) # use pos to offset the text # use vectors for multiple plotting plot(x,y,ann=FALSE, axes= FALSE, col="blue") box(col="gray") text(x[-3],y[-3],c("right","top","bottom","left"),pos=c(4,3,1,2)) text(3,3,"overlay") text(4,2,"angle = 10 degrees",srt=10) # illustration of plot math plot(x,y,ann=FALSE, axes= FALSE, type="n") box(col="gray") text(3,4, expression(sqrt(x[i]^2+y[i]^2)-hat(beta))) text(3,3, expression(bar(x) == sum(frac(x[i], n), i == 1,n))) # text(3,3, # expression(bar(x) == sum(frac(x[i], n), # i == 1,n)), # cex=4,col="red",font=1,srt=-10) text(3,2, expression(paste("Temperature (", degree, "C) in 2013"))) detach(Ant.Data) # detach ant data par(opar) # restore original parameters ls() # list objects in memory rm(list=ls()) # remove all objects from memory - optional!