I have data of this format and want to make a contour plot. When I try to use the
"x must be numeric"
z <- c(
z <- matrix(z, ncol = 2, byrow = TRUE)
density() is used for univariate density estimation. Since you have two independent variables:
lat, you should use
kde2d() from R's default package
library(MASS) fit <- kde2d(z[,1], z[,2]) contour(fit$x, fit$y, fit$z) ## show original data locations points(z, pch = 19, col = 4)
If you look at
Usage: kde2d(x, y, h, n = 25, lims = c(range(x), range(y)))
The default number of cells along each of
n = 25, which gives you a 25 * 25 grid. Density estimation is done on this grids. Perhaps you are wondering why estimation is done on a regular grid. Because such grid is like pixels of a digital photo. Grid/raster like object is convenient for visualization. Actually, if you want computer to proceed 3D graph, you have to give it a raster like object.
In practice, you should choose
n according to how many data you have. Note that a 25 * 25 grid has 625 cells, this is quite fair when you have 1000 data points. You can also try
n = 50. Setting
n is very similar to setting number of bins when you produce a histogram. As
n increases, your resulting estimation is more jagged. Consider the histogram example if you are unclear:
x <- rnorm(200) hist(x, breaks = 10) hist(x, breaks = 20)
Precisely, density estimation is different to histogram; the former is a kernel smoother, while the latter is a primitive bin smoother. But the choice of
n (refinement) does has equal effect.