###################################################################### ### Mon Oct 13 12:57:05 PDT 2008 ### ### /home/davis/hdir1/carlm/213/Week5/demonstration.r ### Demog 213 Week 5 ### This file contains a few demonstrations of the awesome power of ### the R's graphics system. ### you may read this file into your current emacs buffer by typing ### C-x i ~carlm/213/Week5/demonstration.r ### If you would like to see your ancestors' work: ### C-x i ~carlm/213/Graphics/Cohort23.r ### C-x i ~carlm/213/Graphics/Cohort24.r ### C-x i ~carlm/213/Graphics/Cohort25.r ### C-x i ~carlm/213/Graphics/Cohort26.r ### C-x i ~carlm/213/Graphics/Cohort27.r ### C-x i ~carlm/213/Graphics/Cohort28.r ### C-x i ~carlm/213/Graphics/Cohort29.r ### BUT NOTE -- while your ancestors are now accomplished demographers, ### they may not have been during week 5 of their first semester, so ### don't expect elegance or even correctness when you look at their code. ###################################################################### ### ----------------------------------------------------------------- ### creating the graphics window using the X11() driver and drawing a ### simple graph using the plot() command ### ----------------------------------------------------------------- ### you don't have to explicity create x11 device x11() ### plot is the most basic and most useful graphics command. plot(x= 1:100,y =sin(1:100/10)) ## Some of the nifty options of the plot command can change the way ## the graph looks. Note how executing the plot command blows away the ## current contents of the graphics window and replaces it with new ## stuff ### the 'type' argument to the plot() function has profound effects. plot(x=1:100,y=sin(1:100/10),type='s') plot(1:100,sin(1:100/10),type='l') plot(1:100,sin(1:100/10),type='b') plot(1:100,sin(1:100/10),type='p') plot(1:100,sin(1:100/10),type='o') plot(1:100,sin(1:100/10),type='h') ### ### xlab and ylab arguments change the x and y axis labels ### if you like fancy greek letters in your titles and labels ### check out plotmath. ### xlb<- expression(pi*r^2) ylb<- expression(paste("Transflux Reception in ", beta^3, "glutrons/sec")) plot(1:100,sin(1:100/10),type='n', xlab=xlb, ylab=ylb) #### #### adding points to an existing plot #### plot(1:100,sin(1:100/10),type='h') points(1:100,cos(1:100/10),col="blue", cex=3) points(1:100,cos(1:100/10),col="red", cex=.5) lines(1:100,tan(1:100/10),lwd=3) abline(h=.5, lty=2,col='magenta') abline(h=.6, lty=2,col='#FF4523') title(main="A pretty but useless graph of some common trig functions", sub="useless subtitle") mtext(side=1,adj=1,cex=.75,line=3, text="carlm@demog.berkeley.edu\nUCB Demography") mtext(side=3,adj=0,line=0,cex=.75,text=date()) ### several of the above parameters e.g. cex (character expansion - ### size); col (color); lwd (line weight or thickness); lty (line ### type) can be set as defaults using the par() command. These same ### options apply to lots and lots of graphic functions. ### An example from weeks ago illustrating high level, low level, ### and "interactive" graphing commands; nesting functions within ### graphing commands; adding text to existing plots. data(faithful) waiting<-faithful$waiting eruptions<-faithful$eruptions ### simple version plot(waiting,eruptions) points(supsmu(waiting,eruptions),col='purple') lines(supsmu(waiting,eruptions),col='purple',lwd=2) ### embellished version plot(log(waiting),log(eruptions), main="Waiting vs Eruption", xlab="Waiting Time in Minutes Between Eruptions", ylab="Duration of Eruption") lines(supsmu(waiting,eruptions),col='purple',lwd=2) text(60,3.5,"middling eruption",cex=3) ### text() is surprisingly useful you can place text according to specific ### data points such as the max eruption text(x=waiting[eruptions==max(eruptions)], y=max(eruptions), label=expression(theta),cex=3,col="goldenrod") ## You can also use the locator() function to place text interactively. ## uncomment this to put "long eruption onto the graph ## interactively left button to select location(s), right button to ## drop in the text. ##text(locator(), "Long eruption",pos=4) ### to print the graph that is currently displayed on the dev.cur() ### use the command dev.print(). You can set a whole mess of options ### with dev.print(). see help on ps.options for a list. The most ### useful one is command as show below to specify the printer. ### BUT rembmer this is only for quick and dirty output. ### use the postscript() device driver for quality output. ##dev.print(command="lpr -P age") ##dev.copy2eps(file="Useless.ps") ##postscript(file='graph.ps',paper='letter',horiz=F) #### using dnorm() and abline() xvec<-seq(1,50,length=150) plot(xvec,dnorm(mean=25,sd=10,x=xvec)) plot(xvec,dnorm(mean=10,sd=10,x=xvec)) plot(xvec,dnorm(mean=10,sd=10,x=xvec),cex=xvec) plot(xvec,dnorm(mean=10,sd=10,x=xvec),cex=xvec, col=rainbow(10)) plot(xvec,dnorm(mean=10,sd=10,x=xvec),cex=xvec, col=rainbow(length(xvec))) plot(xvec,dnorm(mean=10,sd=10,x=xvec),cex=xvec, col=heat.colors(length(xvec))) points(xvec , dnorm(mean=10,sd=10,x=xvec)) points(xvec + sin(xvec)*2, dnorm(mean=10,sd=10,x=xvec)) points(xvec + sin(xvec)*2, dnorm(mean=10,sd=10,x=xvec), pch=6, cex=xvec/3, col='blue') abline(a=.015,b=0) abline(a=.015,b=0,lty=3,lwd=10,col='papayawhip') abline(a=.015,b=0,lty=5,lwd=8,col='turquoise3') abline(a=.015,b=.001,lty=2,lwd=8,col='brown') #dev.off() ######################################################################## ### Using par(mfrow) to split the screen into several parts ### ### Here are some nice graphs produced by members of Cohort 23 ### used here to demonstrate par(mfrow=) ######################################################################## par(mfrow=c(2,2)) ## Bernardo Lanza's graph goes in the first quadrant plot(1:100, 1:100,col='blue') lines(1:100,tan(1:100/10), lwd=25) lines(1:100, tan(1:100/10), lwd=3, col='yellow') title(main='Some sort of fluctuation') ## Bernardo Lanza graph also goes in the second quadrant x<-rnorm(100) y<-log(x) plot (x,y, col='red', cex=10) title(main='The tube....') ## Maggie and Ayesha's masterpeice goes in the 3rd quad x<-runif(100) y<-sin(runif(100)) plot(x,y,col="sienna3", type='b') title(main="maggie and ayesha's game of pictionary", sub="what is this picture of?") ## Emi and CK's goes in the forth plot(1:100,tan(1:100/10),type='h') ## Insert the colors on the graph points(1:100,tan(1:100/10),col="green", cex=3) points(1:100,cos(1:100/10),col="red", cex=.5) ## Insert the tile for the graph title(main="Bio-like Art\n by CK and Emi", cex=2) mtext(side=1,adj=1,cex=.75,line=3, text="Department of Demography\n UC Berkeley") par(mfrow=c(1,1)) ## return to default behavior ### ----------------------------------------------------------------- ### using split.screen() to split the graphics device into several pannels ### and using a couple of more obscure plotting commands ### The only reason to use this new split.screen() capability is if ### you need to divide the plotting surface into irregular sized ### panels. par(mfrow=c()) is much simpler if all you need are N equal ### sized plot panels. ### ----------------------------------------------------------------- x<-rnorm(1000) z<-exp(rnorm(1000)) split.screen(c(2,2)) screen(1) hist(x,xlab='Value',main='Normal') screen(3) hist(z,xlab='Value',main='Lognormal',breaks=30) screen(2) boxplot(x,z,main="Boxplot",names=c("Normal","LogNormal"),ylim=c(-3,5)) screen(4) split.screen(c(1,2)) screen(5) qqplot(x,z,type='l',lty=1,main="Q-Q Plot",xlab="Normal", ylab="LogNormal") screen(6) qqplot(x,z,type='l',lty=1,main="Q-Q Plot",xlab="Normal", ylab="LogNormal") mtext(outer=T,line=-1,side=3,text="Normal and LogNormal: a tedius comparison") ## undo all the screen commands close.screen(all.screens=T) ######################################################################## ## What do you think this will do? ######################################################################## par(mfrow=c(2,2)) for(j in 1:4){ plot(c(-2,2),c(-2,2),type='n',xaxt='n',yaxt='n',xlab='',ylab='',bty='n') for(i in 1:25){ polygon(x=rnorm(i+2),y=rnorm(i+2),col=rainbow(25)[runif(1)*25]) } }