[R] Plotting a cloud/fog of variable density in rgl

JiHO jo.lists at gmail.com
Mon Nov 22 18:51:04 CET 2010

Hi everyone,

I want to plot a 3D interpolation of the concentration of aquatic
organisms. My goal would be to have the result represented as clouds
with a density proportional to the abundance of organisms, so that I
could fly (well, swim actually ;) ) through the scene and see the
patches here and there. Basically, I want to do something like this:
but simpler and with only clouds.

I though about doing it this way:
1- interpolate to a fine grid
2- plot points at each grid intersection of transparency inversely
proportional to abundance
3- blur/fog a bit each point to create the general impression of a cloud

So far I am stuck on 3 but maybe there is a better overall solution.
Here is some code that reads the result of the interpolation on a
coarse grid and plots it:

	# read a set of gridded data points in 3D
	d = read.table("http://dl.dropbox.com/u/1047321/R/test3Ddata.txt", header=T)

	# plot
	spheres3d(d$x, d$y, d$z, alpha=alpha, radius=0.05)

And here is a version that actually performs the interpolation a
random set of points in 3D through kriging in case you want to try
with increase precision.

	# create a set of random data points in 3D
	n = 50
	data3D = data.frame(x = runif(n), y = runif(n), z = runif(n), v = rnorm(n))

	# do 3d interpolation via kriging
	coordinates(data3D) = ~x+y+z
	range1D = seq(from = 0, to = 1, length = 10)
	grid3D = expand.grid(x = range1D, y = range1D, z = range1D)
	gridded(grid3D) = ~x+y+z
	res3D = krige(formula = v ~ 1, data3D, grid3D, model = vgm(1, "Exp", .2))

	# convert the result to a data.frame
	d = as.data.frame(res3D)

	# compute transparency (proportional to the interpolated value)
	maxD = max(d$var1.pred)
	minD = min(d$var1.pred)
	alpha = (d$var1.pred - minD)/(maxD - minD)
	# reduce maximum alpha (all points are semi-transparent)
	alpha = alpha/5

	# plot
	spheres3d(d$x, d$y, d$z, alpha=alpha, radius=0.05)

I saw the fog effect but it seems to add a fog in the scene to
increase depth. What I want is my scene to actually look like a fog.

Thanks in advance for any help. Sincerely,


More information about the R-help mailing list