简体   繁体   中英

how to plot three dimension data or four dimension data to filled contour plot or surface plot in R

ENV

R 3.3.2

When I have data like mini data 1 :

rdn<-c(0.8,1.8,2.8)
tdn<-c(1,2,3,4,5,6,7,8,9)

idn<-matrix(c(0.3, 0.3, 0.3, 0.2, 0.2, 0.4, 0.1, 0.1, 0.5, 0, 0.2, 0.5, 0, 0.3, 0.6, 0, 0.4, 0.6, 0, 0.4, 0.6, 0, 0.5, 0.7, 0, 0.5, 0.7), nrow=9, ncol=3, byrow=T)

And the matrix looks like(3*9 = 27 data elements):

0.3, 0.3, 0.3, 
0.2, 0.2, 0.4, 
0.1, 0.1, 0.5, 
0, 0.2, 0.5, 
0, 0.3, 0.6, 
0, 0.4, 0.6, 
0, 0.4, 0.6, 
0, 0.5, 0.7, 
0, 0.5, 0.7

Then I can get a filled.contour with parameters x,y,z. x is tdn, y is rdn, z is the matrix. I already get this several month ago using filled.contour . rdn and tdn are only works as labels of x, y. The matrix seems be looks contours lines. And the matrix data is not a function of rdn and tdn.

在此处输入图片说明

My current problem is:

What If I have three dimension data mini data 2

r1dn<-c(0.8,1.8,2.8)
r2dn<-c(0.8,1.8,2.8)
tdn<-c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9)

And (3*3*9 = 81 data elements):

 0.8                  1.8                  2.8
0.8  1.8  2.8       0.8  1.8  2.8        0.8  1.8  2.8

--------------- 81 ---- elements ----------------------

0.3, 0.3, 0.3,      0.3, 0.3, 0.5,       0.3, 0.3, 0.3, 
0.2, 0.2, 0.4,      0.2, 0.4, 0.4,       0.4, 0.2, 0.5,
0.1, 0.1, 0.5,      0.2, 0.3, 0.5,       0.4, 0.4, 0.5, 
0, 0.2, 0.5,        0.2, 0.2, 0.6,       0.4, 0.5, 0.6, 
0, 0.3, 0.6,        0.3, 0.3, 0.6,       0.5, 0.5, 0.7, 
0, 0.4, 0.6,        0.2, 0.5, 0.7,       0.5, 0.6, 0.7, 
0, 0.4, 0.6,        0, 0.5, 0.6,         0.5, 0.6, 0.9,  
0, 0.5, 0.7,        0, 0.6, 0.8,         0.5, 0.7, 0.8, 
0, 0.5, 0.7         0, 0.6, 0.8          0.5, 0.8, 0.9       

I googled many surface and contour codes but I still not find some code for three dimension data yet. How to do that in R? Say, x is r1dn, y is r2dn, z is tdn, what about the three dimension data(I mean the 81 elements data)? Does ggplot can plot three dimension filled contour or surface plot? Or another alternative solutions?

All I expected is a 3d plot with color changes smoothly and no grid on it.

Looks like:

在此处输入图片说明

no grid for next three figures

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

Those should be 3d filled contour or 3d surface plot.

Thanks for your time.

EDIT

IT LOOKS LIKE four dimension either after unfold all the data mini data 2 :

r1dn   r2dn   tdn    fdn
 x,     y,     z,     f
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

Well, data mini data 1 can be unfolded to three dimension either and can be plotted by filled.contour in 2d plot, so there should be a way 3d filled.contour to plot mini data 2 right?

This should get you started. I tried to use your data but when you expand the grid you get a volume of points when you want a surface outlining the volume.

require(akima)
require(rgl)

r1dn<-runif(5, min = 3, max = 10)
r2dn<-runif(5, min = 0, max = 5)
tdn<-rnorm(5, 0, 5)

#interp is from the akima package so you can work with non-square data in your contour
  df <- as.data.frame(interp2xyz(interp(x=r1dn, y=r2dn, z=tdn)))

plot3d(df) # What it looks like in 3d

#what it looks like projected into 2d
  ggplot(df, aes(x=x,y=y,z=z, fill=z)) + 
    geom_contour(binwidth=0.1, aes(color= ..level..)) + 
    theme_classic()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM