[R] how to plot three dimension data to filled contour plot or surface plot in R Ask Question

Ismail SEZEN sezenismail at gmail.com
Thu Apr 13 04:05:20 CEST 2017


> On 12 Apr 2017, at 09:08, dncdd <dncdd at aliyun.com> wrote:
> 
> Sorry, I might make the question complicated. 
> 
> I can use filled.contour() to plot mini data 1(three dimension on flat surface). Now my problem is on mini data 2. Let's unfold the mini data 2.
> 
> 
>   r1dn   r2dn   tdn    fdn
>      x,     y,     z,     v
>     0.8    0.8    0.1    0.3
>     0.8    0.8    0.2    0.2
>     0.8    0.8    0.3    0.1
>     0.8    0.8    0.4    0
>     0.8    0.8    0.5    0
>     0.8    0.8    0.6    0
>     0.8    0.8    0.7    0
>     0.8    0.8    0.8    0
>     0.8    0.8    0.9    0
>     0.8    1.8    0.1    0.3
>     0.8    1.8    0.2    0.2
>     0.8    1.8    0.3    0.1
>     0.8    1.8    0.4    0.2
>     0.8    1.8    0.5    0.3
>     0.8    1.8    0.6    0.4
>     0.8    1.8    0.7    0.4
>     0.8    1.8    0.8    0.5
>     0.8    1.8    0.9    0.5
>     0.8    2.8    0.1    0.3
>     0.8    2.8    0.2    0.4
>     0.8    2.8    0.3    0.5
>     0.8    2.8    0.4    0.5
>     0.8    2.8    0.5    0.6
>     0.8    2.8    0.6    0.6
>     0.8    2.8    0.7    0.6
>     0.8    2.8    0.8    0.7
>     0.8    2.8    0.9    0.7
>     1.8    0.8    0.1    0.3
>     1.8    0.8    0.2    0.2
>     1.8    0.8    0.3    0.2
>     1.8    0.8    0.4    0.2
>     1.8    0.8    0.5    0.3
>     1.8    0.8    0.6    0.2
>     1.8    0.8    0.7    0
>     1.8    0.8    0.8    0
>     1.8    0.8    0.9    0
>     1.8    1.8    0.1    0.3
>     1.8    1.8    0.2    0.4
>     1.8    1.8    0.3    0.3
>     1.8    1.8    0.4    0.2
>     1.8    1.8    0.5    0.3
>     1.8    1.8    0.6    0.5
>     1.8    1.8    0.7    0.5
>     1.8    1.8    0.8    0.6
>     1.8    1.8    0.9    0.6
>     1.8    2.8    0.1    0.5
>     1.8    2.8    0.2    0.4
>     1.8    2.8    0.3    0.5
>     1.8    2.8    0.4    0.6
>     1.8    2.8    0.5    0.6
>     1.8    2.8    0.6    0.7
>     1.8    2.8    0.7    0.6
>     1.8    2.8    0.8    0.8
>     1.8    2.8    0.9    0.8
>     2.8    0.8    0.1    0.3
>     2.8    0.8    0.2    0.4
>     2.8    0.8    0.3    0.4
>     2.8    0.8    0.4    0.4
>     2.8    0.8    0.5    0.5
>     2.8    0.8    0.6    0.5
>     2.8    0.8    0.7    0.5
>     2.8    0.8    0.8    0.5
>     2.8    0.8    0.9    0.5
>     2.8    1.8    0.1    0.3
>     2.8    1.8    0.2    0.2
>     2.8    1.8    0.3    0.4
>     2.8    1.8    0.4    0.5
>     2.8    1.8    0.5    0.5
>     2.8    1.8    0.6    0.6
>     2.8    1.8    0.7    0.6
>     2.8    1.8    0.8    0.7
>     2.8    1.8    0.9    0.8
>     2.8    2.8    0.1    0.3
>     2.8    2.8    0.2    0.5
>     2.8    2.8    0.3    0.5
>     2.8    2.8    0.4    0.6
>     2.8    2.8    0.5    0.7
>     2.8    2.8    0.6    0.7
>     2.8    2.8    0.7    0.9
>     2.8    2.8    0.8    0.8
>     2.8    2.8    0.9    0.9
> 
> 
> When x is 0.8, y is 0.8, z is 0.1, then v is 0.3.  So v is not limited by a function like v ~ f(x,y,z). I mean x,y,z is just like labels on the 3d space.
> You have give me many suggestions. I think that maybe scatterplot with colors on v on 3d space is a solution. I will try it later. At the beginning, I was wondering 
> a 3d surface plot or 3d filled.contour on 3d space. I am not sure whether it is possble.


First of all, v is function of x,y,z according to sample above wheter you accept or not.  Because x,y,z represents coordinates of v in the 3D space and if someone wants to plot contours between those points, he/she needs a v(x,y,z) function that can calculate values (v) between x,y,z coordinates. Let’s get back to main question.

I think you want something like as [1] and [2]. Your best shot is misc3d package. I created a simple example how to plot isosurfaces and you can find different strategies to plot 4D data in 3D space at [3].

#——————————
library(misc3d)
# let's create a function to create a sample data to visualize
f <- function(x, y, z) sin(x) * sin(y) * sin(z)
x <- z <- seq(0, pi, length.out = 15)
y <- seq(-pi, pi, length.out = 15)
d <- expand.grid(x, y, z)
colnames(d) <- c("x", "y", "z")
d$v <- with(d, f(x, y, z))

# this is your initial data
head(d)

k <- 16 # number of contours
alpha_min_max <- c(0.2, 0.6)
colf <- colorRampPalette(rev(RColorBrewer::brewer.pal(11, "RdBu")))

# isosurfaces are at here
lev <- seq(min(d$v), max(d$v), length.out = k)
# inner isosurfaces are more solid, outer ones are more transparent.
# So, we can see inner isosurfaces by x-ray vision.
alpha <- seq(alpha_min_max[1], alpha_min_max[2], length.out = k)

rgl::plot3d(x, y, z, type = "n", aspect = TRUE) # create scene
misc3d::contour3d(f, lev, x, y, z, color = colf(k), alpha = alpha,
          smooth = 3, engine = "rgl", add = TRUE)
rgl::aspect3d(1, 2, 1) # set aspect, y is different from x and z.
#——————————


1- http://stackoverflow.com/a/11319175/557884 <http://stackoverflow.com/a/11319175/557884>
2- http://mathematica.stackexchange.com/a/19819 <http://mathematica.stackexchange.com/a/19819>
3- https://www.jstatsoft.org/article/view/v028i01 <https://www.jstatsoft.org/article/view/v028i01>






	[[alternative HTML version deleted]]



More information about the R-help mailing list