### /Users/kjhealy/data/dsquared/surface.r ### Thu Sep 9, 2004 2:03 PM ### Surface plot from CT discussion on DEM04 contract. ### Uses data calculated by Daniel Davies. # The lattice library contains the function to draw surface plots. # Could also have used persp() in the default package. library(lattice) # Read in the data. data <- read.csv("surface.csv",header=TRUE,row.names=1,check.names=FALSE) # Look at the top few rows. head(data) # Dimensions of the dataframe. dim(data) # I know there's a simpler way of doing the axis labeling # but I can't remember what it is. # Manually generate a scale for the y-axis labels. y.scale <- seq(0.02,1,by=0.02) y.ind.ticks <- seq(50, 1,by=-5) # we don't want to display all the tick marks # Manually generate a scale for the x-axis labels that skips in increments of 10. x.ind.ticks <- seq(5,nrow(data),by=10) # Flip around the column-ordering of the matrix, so the plot # replicates DD's output. (R's default is opposite direction.) # There's almost certainly a more elegant way to do this, too. ind <- seq(50,1,by=-1) tmp <- data[,ind] # This is the working dataframe we'll use below head(tmp) # Create the PDF pdf("surface.pdf", width=8, height=8) # wireframe() does all the work. # see help(wireframe) for details on the various options. # leave out the scales = list( ... ) argument to see the default # scale (basic labels, with arrows). wireframe(as.matrix(tmp),drape = TRUE, colorkey=TRUE, scales = list(arrows=FALSE, x=list(labels=rownames(tmp)[x.ind.ticks],at=x.ind.ticks), # This is a bit messy. y=list(labels=y.scale[y.ind.ticks],at=ind[y.ind.ticks])), # zlab="Deviation\nfrom\nMarket\nPrice", xlab="Time", ylab="Black-Scholes Volatility", zoom=0.75, # zoom out a bit col.regions=topo.colors(124), # use topographical colors rather than spectrum screen = list(z=60,x=-70)) # set the angle of view title(main="Overvaluation of the DEM04 Contract on the Iowa Electronic Markets\n", sub="See http://www.crookedtimber.org/archives/002460.html for discussion.") dev.off()