##-------------------------------## ## Script for Part 1: Graphics ## ## John Fox ## ## Statistical Computing in R ## ## McMaster University ## ## Fall 2005 ## ##-------------------------------## # Graphics Basics # points, lines, axes, frames plot(c(0,1), c(0,1), type='n', xlab='', ylab='') # coordinate system ?plot par('col') # graphical parameters par() ?par plot(1:25, pch=1:25, xlab='Symbol Number', ylab='') # symbols lines(1:25, type='h', lty='dashed') plot(26:1, xlab='letters', ylab='', pch=letters, axes=FALSE, frame=TRUE) plot(c(1,7), c(0,1), type='n', axes=FALSE, # lines xlab='Line Type (lty)', ylab='') box() axis(1, at=1:6) for (lty in 1:6) lines(c(lty, lty, lty + 1), c(0, 0.5, 1), lty=lty) # text par(mfrow=c(1,2)) # array of plots plot(c(0,1), c(0,1), axes=FALSE, type='n', xlab='', ylab='') box() text(x=c(.2, .5), y=c(.2, .7), c('example text', 'another string')) title('(a)') plot(c(0,1), c(0,1), axes=FALSE, type='n', xlab='', ylab='') box() text(locator(3), c('one','two','three')) # position by mouse title('(b)') locator() # returns mouse coordinates # arrows and line segments plot(c(1,5), c(0,1), axes=FALSE, type='n', xlab='', ylab='') arrows(x0=1:5, y0=rep(0.1, 5), x1=1:5, y1=seq(0.3, 0.9, len=5), code=3) title('(a) arrows') plot(c(1,5), c(0,1), axes=FALSE, type='n', xlab='', ylab='') segments(x0=1:5, y0=rep(0.1, 5), x1=1:5, y1=seq(0.3, 0.9, len=5)) title('(b) segments') # polygons par(mfrow=c(1,1)) # restore single panel plot(c(0,1), c(0,1), type='n', xlab='', ylab='') polygon(c(.2,.8,.8), c(.2,.2,.8), col="red") polygon(c(.2,.2,.8), c(.2,.8,.8)) # legend plot(c(1,5), c(0,1), axes=FALSE, type='n', xlab='', ylab='', frame.plot=TRUE) legend(locator(1), legend=c('group A', 'group B', 'group C'), lty=1:3, pch=1:3, col=c("blue", "green", "red")) # colors pie(rep(1, length(palette())), col=palette()) palette() rainbow(10) gray(0:8/8) pie(rep(1,100), col=rainbow(100), labels=rep('',100)) pie(rep(1,100), col=gray(0:100/100), labels=rep('',100))