[R] contour plot axis correspondence

ivo welch ivo.welch at anderson.ucla.edu
Sat Nov 16 20:39:36 CET 2013

I am struggling with a contour plot.  I want to place a cross over the
minimum.  alas, I don't seem to be able to map axes appropriately.
here is what I mean:

N <- 1000

rm <- rnorm(N, mean=0.0, sd=0.4)
rx <- rnorm(N, mean=0.0, sd=0.4)
rt <- rnorm(N, mean=0.0, sd=0.4)

exploss <- function(hdgM,hdgX) {
    ## this could be any function that is not vectorized
    losscosts <- function(FirmV) { D <- 50; ifelse( FirmV<D, 0.2*(D-FirmV), 0) }
    FirmV <- 100*(1+rt)*(1+rm)*(1+rx) + 100*(hdgM*(1+rm)-hdgM) +
    mean( losscosts( FirmV ) )

ss <- seq(-2,0.5,0.1)
MX <- expand.grid( hdgM= ss, hdgX= ss )
MX$z <- unlist(lapply( 1:nrow(MX), function(i) with(MX,
exploss(hdgM[i],hdgX[i])) ))

M <- matrix(MX$z, nrow=length(ss), ncol=length(ss))
rownames(M) <- colnames(M) <- ss

filled.contour( x=ss, y=ss, M )

vline <- function(x, y=c(-99,99), ...) lines(c(x,x), y, ...)
vline(-0.5, col="blue", lwd=3 )

how do I map the -0.5 in the vline() to the true -0.5?  it is drawn .

as a sidenote, it was not easy to figure out how I could plot an z
function for an x-axis and y-axis.  plot(x,y) is very intuitive.  it
would have been nice to have analogous plot3d(x,y,z) and
contour(x,y,z) functions.  as with everything in R, it probably
exists, but I did not find it.  my above code had to map MX (with
x,y,z columns) into a matrix first.

advice appreciated.



Ivo Welch (ivo.welch at gmail.com)

More information about the R-help mailing list