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

Ismail SEZEN sezenismail at gmail.com
Tue Apr 11 21:15:06 CEST 2017


> On 11 Apr 2017, at 20:55, dncdd <dncdd at aliyun.com <mailto:dncdd at aliyun.com>> wrote:
> 
> Thank you Ismail SEZEN.
> The link you give is filled.contour code which only works with my first mini data .

filled.contour + lattice::levelplot solution can handle either matrix or a 3 column (x,y,z) data.frame by using the formula z ~ x + y. 

> 
> The code in the link for R 3.3.2 is :
> ****** code ***** in **** link **** for R 3.3.2 ****
> panel.filledcontour <- function(x, y, z, subscripts, at, col.regions = 
> cm.colors, 
>                                 col = col.regions(length(at) - 1), ...) 
> { 
>   stopifnot(require("gridBase")) 
>   z <- matrix(z[subscripts], 
>               nrow = length(unique(x[subscripts])), 
>               ncol = length(unique(y[subscripts]))) 
>   if (!is.double(z)) storage.mode(z) <- "double" 
>   opar <- par(no.readonly = TRUE) 
>   on.exit(par(opar)) 
>   if (panel.number() > 1) par(new = TRUE) 
>   par(fig = gridFIG(), omi = c(0, 0, 0, 0), mai = c(0, 0, 0, 0)) 
>   cpl <- current.panel.limits() 
>   plot.window(xlim = cpl$xlim, ylim = cpl$ylim, 
>               log = "", xaxs = "i", yaxs = "i") 
>   # paint the color contour regions 
>   .filled.contour(as.double(do.breaks(cpl$xlim, nrow(z) - 1)), 
>                           as.double(do.breaks(cpl$ylim, ncol(z) - 1)), 
>                           z, levels = as.double(at), col = col)
>   # add contour lines 
>   contour(as.double(do.breaks(cpl$xlim, nrow(z) - 1)), 
>           as.double(do.breaks(cpl$ylim, ncol(z) - 1)), 
>           z, levels = as.double(at), add=T, 
>           col = "gray", # color of the lines 
>           drawlabels=F  # add labels or not 
>          ) 
> } 
> plot.new() 
> 
> print(levelplot(volcano, panel = panel.filledcontour, 
>           col.regions = terrain.colors, 
>           cuts = 10, 
>           plot.args = list(newpage = FALSE)))
> *** END *** code *** in *** link *** for R 3.3.2 *** 
> 
> first mini data
> which should be a three dimensinal data either and the data in matrix is not a function of rdn and tdn
> which means z matrix is not function of x,y. 


Here we are not interested in z is function of x and y but at the and, if you want to plot a 3D data on a flat surface, you need x and y for each z in the 3D space.

> 
>     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

As I mentioned above, you have z values for each x and y values in the space. one of elements of z might be NA/NaN but at last you have.

> 
> 
> Well, now I realized that the second data might (my current problem) be afour dimensional data:
> 
> 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)


Thing are changed now. We are living in a 4D space (at least that we can sense) and there are only 2 ways to visualize 4D data on a flat surface. These are your only chance.

1- 3D perspective plotting (one of rgl, misc3d, scatterplot3d). Plot 'points' in the 3D space and set 4th dimension as color for each point.  Does it make sense? I don’t know, it’s up to you.

2- for the (x,y,z,v) dimensions, plot contours of (x,y,v) for each z. I mean, assume z is height, slice it and plot each slice by filled.contour/levelplot or what ever you want.

> 
> 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 ------three matrix----------------
> 
>     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  
> 
> The three matrix is not the function of r1dn, r2dn, tdn.  r1dn, r2dn, tdn can be labels. So there are four dimensional data. x is r1dn, y is r2dn, z is tdn and the three matrix is, let's say, vdn.
> Four dimension: r1dn r2dn tdn fdn as x,y,z,v.  And v is not the function of x,y,z. So there are might need a 3d filled.contour. But I did not find it. All the code I found is that x,y,z and z is a function of x,y. Another situation I found is that x,y,z,v and v is function of x,y,z. But in my data, v is not a function of x,y,z.


Actually, you need to detail (in your mind and to us) what do you mean by being function of. A point can be represented by 4 points in 4D space (x,y,z,t). According to this; v is short for value (measurement); v(x,y,z,t) can represent a value in space and time. So this is a 5D data I assume. We mostly use one or 2 or 3 of this dimensions. So, v is function of (x, y, z, t) and change by (x, y, z, t). For instance, if v is not function of time (t), it is always constant and does not change in time. Hence, I dont need 4th dimension (t here) and I don’t need to plot v in a 4D space. This is what I know about being function of.
	[[alternative HTML version deleted]]



More information about the R-help mailing list