[R] 2-dim density plot
Duncan Murdoch
murdoch.duncan at gmail.com
Thu Aug 11 21:23:39 CEST 2011
On 11/08/2011 3:11 PM, annie Zhang wrote:
> Hi All,
>
> I have a 2-dim density defined on 0<x<1, 0<y<1, x<y. I know the exact
> formula of the density. How can I visualize it? What plot functions can I
> use?
persp() or contour() are in the graphics package. There are similar
ones in lattice (and I think in ggplot2), but I forget their names.
There's also persp3d() in the rgl package for a rotatable version of
persp().
Supposing f(x,y) gives the density at (x,y) (or NA if x > y), you plot
it as follows:
x <- seq(0, 1, len=30)
y <- seq(0, 1, len=30)
z <- outer(x, y, f)
persp(x,y,z)
contour(x,y,z)
persp3d(x,y,z)
This relies on f() being vectorized; if it's not, use
z <- outer(x,y, Vectorize(f))
instead.
Duncan Murdoch
More information about the R-help
mailing list