[R] Plot 3d density

David L Carlson dcarlson at tamu.edu
Wed Nov 28 20:59:48 CET 2012


Is this what you are trying to do? 

x <- rnorm(25)
y <- rnorm(25)
xd <- density(x, n=25, from=-3.5, to=3.5)
yd <- density(x, n=25, from=-3.5, to=3.5)
z <- outer(xd$y, yd$y)
persp(xd$x, yd$x, z)

The density function gives x values at which the density is estimated, so
that gives you the axes for the plot, but the z value must be computed from
the y values that density returns. This example multiplies the two y values
to get z.
----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352



> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Maximilian Lklweryc
> Sent: Wednesday, November 28, 2012 10:07 AM
> To: r-help at r-project.org
> Subject: [R] Plot 3d density
> 
> I want to create a 3d plot with densities.
> 
> I use the function density to first create a 2d dimensional plot for
> specific x values, the function then creates the density and puts them
> into a y variable. Now I have a second set of x values and put it
> again into the density function and I get a second set of y variables
> and so on.... I want to put those sets into a 3d plot, I hope you know
> what I mean. So I have a surface of densities....
> 
> E.g. I have:
> 
> x1<-c(1:10)
> x2<-c(2:11)
> y1<-c(1,1,2,1,3,4,2,3,2,2)
> y2<-c(1,2,3,1,3,6,2,8,2,2)
> .
> .
> .
> .
> 
> Now I want to put on the x axis for the value 1 the first set that
> means for x=1 on the y axis of the 3d plot the x values from the first
> set and on the z axis the densities. So I have a "disk" for x=1, for
> x=2 I have the second "disk" and so on, so I get a density "mountain".
> 
> I hope I am understandable, if you have a better idea to realize it
> then you are welcome! I want to do it with the persp function, would
> be nice if you make an example with that function,
> 
> The function kde2d does not help.
> 
> Thanks a lot for your help.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list