pbreheny / visreg

Visualization of regression functions
http://pbreheny.github.io/visreg/
62 stars 18 forks source link

Custom axes in visreg2d image plots #2

Closed pbreheny closed 9 years ago

pbreheny commented 9 years ago

Really good question from Jared Laufenberg (U of Georgia): how do you modify axes in visreg2d image plots? The basic issue:

n <- 2000
x1 <- runif(n,-1,1)
x2 <- runif(n,-3,3)
x3 <- runif(n,0,10)
alpha <- 0
beta1 <- 3
beta2 <- -0.5
beta3 <- 0.01
beta4 <- 0.75
beta5 <- -.4
beta6 <- 0.002
lin.eq <- alpha + beta1*x1 + beta2*x2 + beta3*x3 + beta4*x1*x2 + beta5*x1*x3 + beta6*x2*x3
probs <- exp(lin.eq)/(1+exp(lin.eq))
resp <- rbinom(n,1,probs)
dat <- data.frame(resp,x1,x2,x3)
fit <- glm(resp ~ (x1 + x2 + x3)^2, family=binomial,data=dat)

## Default axes
visreg2d(fit, "x1", "x2", plot.type = "image", scale = "response", cond = list(x3=2),
         main="Example", xlab="X1", ylab="X2", col=gray.colors(20))

## Works in filled.contour, but not visreg2d (axes are blank):
visreg2d(global, "x1", "x2", plot.type = "image", scale = "response", cond = list(x3=2),
         main="Example", xlab="X1", ylab="X2", col=gray.colors(20), 
         plot.axes = {axis(1, at=seq(-1, 1, 0.2)); axis(2, seq(-3, 3, 0.5))})

To pass an argument like plot.axes, where it is important not to evaluate it until the proper moment, you need to wrap it in a quote() call. The fix:

## Works!
visreg2d(global, "x1", "x2", plot.type = "image", scale = "response", cond = list(x3=2),
         main="Example", xlab="X1", ylab="X2", col=gray.colors(20), 
         plot.axes = quote({axis(1, at=seq(-1, 1, 0.2)); axis(2, seq(-3, 3, 0.5))}))