hadley/ggplot2 — pull request #544 by wch (2012-05-07 16:18:39+00:00)
Title: stat_density2d: 'lims' gets passed to kde2d

This patch allows the `lims` to properly get passed to `kde2d`. Previously it was getting confused because `lims` needs to be a vector, but each element of `lims` vector was getting turned into an element in the *list*.

This example now works -- density contours are drawn beyond the range of the data:

```R
# Set up the data
sigm1 <- diag(c(30,50))
sigm2 <- diag(c(5,3))
mu1 <- c(10,15)
mu2 <- c(80,60)
library(mvtnorm)
sample <- round(rbind(rmvnorm(100,mu1,sigm1),rmvnorm(100,mu2,sigm2)))
sample <- sample[! (sample[,1] <1 | sample[,2] <1 | sample[,1]> 100 |
sample[,2]>100),]
d <- as.data.frame(sample)
names(d)<-c("x","y")

ggplot(d, aes(x,y)) + stat_density2d(bins=4, lims=c(-20, 120, 0, 100)) +
geom_point() + xlim(-20,120)

```

Note that the limits need to be specified for ggplot, with `xlim`, and to `kde2d` (via `stat_density2d`), with `lims=c(xmin, xmax, ymin, ymax)`.