################################################################################ # CODE FOR PRODUCING GRAPHICS IN PRESENTATION # January 27, 2010 # RYAN R. ROSARIO # UCLA Statistical Consulting Center # Department of Statistics # University of California, Los Angeles # # Please do not redistribute without this header. ################################################################################ #PAGE 12 #Bivariate plot of textbook prices. texts <- read.csv("http://www.stat.ucla.edu/~rosario/boot08/data/book-shopping.csv",header=TRUE) plot(texts[,5],texts[,7],main="Bivariate Analysis of\n Textbook Prices",xlab="Amazon Sales Price",ylab="Borders Sales Price",pch='*',xaxt="n",yaxt="n",cex.lab=0.75,bg="grey") mtext("Fall 2008",3,font=3) axis(1,at=seq(0,200,25),paste("$",seq(0,200,25)),cex.axis=0.75,col="grey") axis(2,at=seq(0,200,25),paste("$",seq(0,200,25)),cex.axis=0.75,col="grey") axis(c(3:4),col="grey",tick=FALSE,labels=FALSE) box("plot",col="grey") #PAGE 28 #Plot types par(mfrow=c(2,3)) plot(y~x,type="p",xlab="Time",ylab="Effort",xaxt="n",yaxt="n",main="type=\"p\" (points)") axis(1,at=seq(0,600,200),labels=c(0,"T/3","2T/3","T")) plot(y~x,type="b",xlab="Time",ylab="Effort",xaxt="n",yaxt="n",main="type=\"b\" (both lines and points)") axis(1,at=seq(0,600,200),labels=c(0,"T/3","2T/3","T")) mtext("type=o is similar",3) plot(y~x,type="h",xlab="Time",ylab="Effort",xaxt="n",yaxt="n",main="type=\"h\" (histogram-like)") axis(1,at=seq(0,600,200),labels=c(0,"T/3","2T/3","T")) plot(y~x,type="s",xlab="Time",ylab="Effort",xaxt="n",yaxt="n",main="type=\"s\" (stair steps)") axis(1,at=seq(0,600,200),labels=c(0,"T/3","2T/3","T")) plot(y~x,type="S",xlab="Time",ylab="Effort",xaxt="n",yaxt="n",main="type=\"S\" (other steps)") axis(1,at=seq(0,600,200),labels=c(0,"T/3","2T/3","T")) plot(y~x,type="n",xlab="Time",ylab="Effort",xaxt="n",yaxt="n",main="type=\"n\" (none!)") axis(1,at=seq(0,600,200),labels=c(0,"T/3","2T/3","T")) #PAGE 30 #Color index source("http://research.stowers-institute.org /efg/R/Color/Chart/ColorChart.R") #PAGE 33 #Final Learning Curve graphic. my.gamma <- rgamma(10000000,7,2) y <- hist(my.gamma,br=100,plot=FALSE)$counts x <- seq(0,max(my.gamma),length=length(y)) plot(y~x,xlab="Time",ylab="Effort",type="l",lwd=4,col="red",xaxt="n",yaxt="n", main="The R Graphics Learning Curve") axis(1,at=seq(0,max(x),length=4),labels=c(0,"T/3","2T/3","T")) #Cyan grid. abline(v=seq(0,max(x),length=15),lty=3,col="cyan") abline(h=seq(0,max(y),length=15),lty=3,col="cyan") #PAGE 36 #EXERCISE 1 #Solution my.norm <- rnorm(10000000,0,1) #easiest to use standard normal! #large number makes the curve smooth. y <- hist(my.norm,br=100,plot=FALSE)$counts #dummy x axis. x <- seq(-3,3,length=length(y)) x.ticks <- seq(-3,3,1) #Can specify the color in many different ways: #With color name string plot(y~x,type="l",lwd=4,col="forestgreen",yaxt="n",xaxt="n",xlab="z",ylab="",main="My Normal Distribution") #With palette code plot(y~x,type="l",lwd=4,col=139,yaxt="n",xaxt="n",xlab="z",ylab="",main="My Normal Distribution") #Or with RGB color content plot(y~x,type="l",lwd=4,col="#228B22",yaxt="n",xaxt="n",xlab="z",ylab="",main="My Normal Distribution") axis(1,at=x.ticks,labels=x.ticks) #PAGE 37 #Better solution. x <- seq(-3,3,by=0.01) plot(dnorm(x)~x,type="l",lwd=4,col=139,yaxt="n",xaxt="n",xlab="z",ylab="",main="My Normal Distribution") axis(1,at=x.ticks,labels=x.ticks) #Even better solution, curve(dnorm(x),type="l",lwd=4,col=139,yaxt="n",xaxt="n",xlab="z",ylab="",main="My Normal Distribution") axis(1,at=x.ticks,labels=x.ticks) #PAGE 39 #Facebook Data group.1 <- read.csv("http://www.stat.ucla.edu/~rosario/scc/facebook-2007.csv",header=TRUE) group.2 <- read.csv("http://www.stat.ucla.edu/~rosario/scc/facebook-2009.csv",header=TRUE) #PAGE 41 #Two plots, separate windows, width=4, height=4 dev.new(height=4,width=4) plot(group.1$Wall.Posts~group.1$Friends,pch='.',main="Facebook 2007",xlab="Friends",ylab="Wall Posts") dev.new(height=4,width=4) plot(group.2$Wall.Posts~group.2$Friends,pch='.',main="Facebook 2009",xlab="Friends",ylab="Wall Posts") #PAGE 42 #Two plots, same window par(mfrow=c(1,2)) plot(group.1$Wall.Posts~group.1$Friends,pch='.',main="Facebook 2007",xlab="Friends",ylab="Wall Posts") plot(group.2$Wall.Posts~group.2$Friends,pch='.',main="Facebook 2009",xlab="Friends",ylab="Wall Posts") #PAGE 48 #Same window, same plot plot(group.1$Wall.Posts~group.1$Friends,pch='.',main="Facebook 2007 vs. 2009",xlab="Friends",ylab="Wall Posts",xlim=c(0,max(group.2$Friends)+400),ylim=c(0,max(group.2$Wall.Posts)+500)) points(group.2$Wall.Posts~group.2$Friends,pch='.',col="red") #some stuff is cutoff! legend(1300,4000,c(2007,2009),col=c("black","red"),pch=c('.','.'),inset=1,title="Legend",pt.cex=5) #PAGE 52 #EXERCISE 2 #Requirements: data <- read.csv("http://www.stat.ucla.edu/~rosario/scc/textbooks.csv",header=TRUE) attach(data) #Solution plot(Amazon.Sale.Price~Amazon.List.Price,pch='*',main="Comparison of UCLA Textbook Prices: Amazon vs. BN",xlab="List Price",ylab="Sales Price") points(Barnes.Noble.Price~Amazon.List.Price,pch='o') abline(a=0,b=1,lty=2,col="grey") good.deals <- which(abs((Amazon.List.Price-Amazon.Sale.Price)/Amazon.List.Price) >= 0.25) points(Amazon.Sale.Price[good.deals]~Amazon.List.Price[good.deals],pch='o',col="red") detach(data) #PAGES 64-72 #Mathematical typesetting. f <- function(x) { return(cos(x)^3) } my.title <- expression(paste("Computing the Integral ",integral(plain(cos)^3*symbol(theta) *d*symbol(theta), -symbol(pi)/2, symbol(pi)/2)) ) x.label <- expression(symbol(theta)) y.label <- expression(plain(cos)^3*symbol(theta)) curve(f,from=-pi,to=pi,n=10000,lwd=4,col="green", xaxt="n",yaxt="n",main=my.title ,xlab=x.label , ylab=y.label) x.tick.locations <- seq(-pi,pi,pi/2) x.tick.labels <- c(expression(-pi),expression(-frac(symbol(pi),2)),0,expression(frac(symbol(pi),2)),expression(symbol(pi))) #y axis is even easier. y.tick.locations <- c(-1,0,1) y.tick.labels <- y.tick.locations axis(1,at=x.tick.locations ,labels=x.tick.labels , cex.axis=0.5) axis(2,at=y.tick.locations ,labels=y.tick.labels , cex.axis=0.5) segments(-pi/2,-1,-pi/2,0,lty=2,col="grey") segments(pi/2,-1,pi/2,0,lty=2,col="grey") #PAGES 73-76 #Integration movie. #The background of the movie: f <- function(x) { return(cos(x)^3) } my.plot <- function(i) { curve(f,from=-pi,to=pi,n=10000,lwd=4,col="green",xaxt="n",yaxt="n",main=my.title,xlab=x.label,ylab=y.label) axis(1,at=x.tick.locations,labels=x.tick.labels, cex.axis=0.5) axis(2,at=y.tick.locations,labels=y.tick.labels, cex.axis=0.5) segments(-pi/2,-1,-pi/2,0,lty=2,col="grey") segments(pi/2,-1,pi/2,0,lty=2,col="grey") e <- (pi/4)*(1/(2**i)) rect(-pi/2+e*seq(0,(2**(i+2))-1),f(-pi),-pi/2+e*seq(1,(2**(i+2))),f(-pi/2+e*seq(1,(2**(i+2)))),col="black") } #Produce graphics frames for movie. setwd(tempdir()) for (i in 1:5) { filename <- paste("plot",i,".jpg",sep="") jpeg(file=filename) my.plot(i) dev.off() } #Make the movie! make.mov <- function(){ unlink("plot.mpg") system("convert -delay 10 plot*.jpg plot.mpg") } make.mov() #PAGE 75 #Example PDF pdf(file="mypdf.pdf",height=7,width=7,onefile=TRUE) #execute your plotting commands here... plot(1:10,1:10) #turn the plotting "device" off. dev.off() #like closing a file in a programming language. ####### # FIN #######