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

15 views
Skip to first unread message

dncdd via R-help

unread,
Apr 11, 2017, 9:58:00 AM4/11/17
to r-help
ENV 

R 3.3.2

When I have data like:

    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.

 
**My current problem** is:

What If I have three dimension 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)

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


  [1]: https://i.stack.imgur.com/z6u3p.png
  [2]: https://i.stack.imgur.com/MEnFn.png
  [3]: https://i.stack.imgur.com/Ri29w.png
  [4]: https://i.stack.imgur.com/CdCqL.jpg
  [5]: https://i.stack.imgur.com/Pt1Nw.jpg
[[alternative HTML version deleted]]

______________________________________________
R-h...@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Jeff Newmiller

unread,
Apr 11, 2017, 10:27:48 AM4/11/17
to dncdd, dncdd via R-help
Ggplot does not do xyz. Look at package lattice or package rgl instead.
--
Sent from my phone. Please excuse my brevity.

Ismail SEZEN

unread,
Apr 11, 2017, 12:49:03 PM4/11/17
to dncdd, r-help
After lon long search, my best shot was to use filled.contour + lattice::levelplot together [a] to represent 3 dimensional data on a flat surface.

a) http://r.789695.n4.nabble.com/Creating-smooth-color-regions-with-panel-contourplot-td866253.html <http://r.789695.n4.nabble.com/Creating-smooth-color-regions-with-panel-contourplot-td866253.html>

Some Details:

> On 11 Apr 2017, at 09:16, dncdd via R-help <r-h...@r-project.org> wrote:
>
> ENV
>
> R 3.3.2
>
> When I have data like:
>
> 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.

If you have a rectangular data like matrix, you can use filled.contour (the best one) or rasterVis::levelplot.

>
>
> **My current problem** is:
>
> What If I have three dimension 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)

At this point, you have to expand your data to x,y,z data pairs. I mean you have to reshaphe your data as a z value for each x and y point.

data <- expand.grid(x = x, y = y)
dta$z <- z

Now, you have 3 columns + 9 rows data. You can use this data in lattice::levelplot by awesome formula interface.

>
> I googled many surface and contour codes but I still not find some code for three dimension data yet.

Actually you found but they didn’t fill you requirements.

> How to do that in R? Say, x is r1dn, y is r2dn, z is tdn, what about the three dimension data? Does ggplot can plot three dimension filled contour or surface plot? Or another alternative solutions?

The problem here is, you want a filled/contour plot on a flat surface or 3D perspective visualisation? If you want 3D perspective visualisation, your only chances are rgl and misc3d packages. (as far as I know)

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

All the solutions above may have (or not) smooth colour change and grid depending on your settings. What do you mean exactly "no grid on it”? If you choose that you don’t have a grid, then you don’t. You only need to search a bit more.

Ismail SEZEN

unread,
Apr 11, 2017, 3:15:26 PM4/11/17
to dncdd, r-help

> On 11 Apr 2017, at 20:55, dncdd <dn...@aliyun.com <mailto:dn...@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.

dncdd via R-help

unread,
Apr 11, 2017, 8:44:53 PM4/11/17
to Ismail SEZEN, r-help
Thank you Ismail SEZEN.The link you give is filled.contour code which only works with my first mini data .
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 = 

          plot.args = list(newpage = FALSE)))*** END *** code *** in *** link *** for R 3.3.2 *** 
first mini datawhich should be a three dimensinal data either and the data in matrix is not a function of rdn and tdnwhich means z matrix is not function of x,y. 


    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

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)

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.
------------------------------------------------------------------发件人:Ismail SEZEN <sezen...@gmail.com>发送时间:2017年4月12日(星期三) 00:48收件人:dncdd <dn...@aliyun.com>抄 送:r-help <r-h...@r-project.org>主 题:Re: [R] how to plot three dimension data to filled contour plot or surface plot in R Ask Question
After lon long search, my best shot was to use filled.contour + lattice::levelplot together [a] to represent 3 dimensional data on a flat surface. 
a) http://r.789695.n4.nabble.com/Creating-smooth-color-regions-with-panel-contourplot-td866253.html


Some Details:
On 11 Apr 2017, at 09:16, dncdd via R-help <r-h...@r-project.org> wrote:

ENV 

R 3.3.2

When I have data like:

    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.

If you have a rectangular data like matrix, you can use filled.contour (the best one) or rasterVis::levelplot.

 
**My current problem** is:

What If I have three dimension 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)

At this point, you have to expand your data to x,y,z data pairs. I mean you have to reshaphe your data as a z value for each x and y point. 
data <- expand.grid(x = x, y = y)dta$z <- z 


Now, you have 3 columns + 9 rows data. You can use this data in lattice::levelplot by awesome formula interface.

I googled many surface and contour codes but I still not find some code for three dimension data yet. 


Actually you found but they didn’t fill you requirements.

How to do that in R? Say, x is r1dn, y is r2dn, z is tdn, what about the three dimension data? Does ggplot can plot three dimension filled contour or surface plot? Or another alternative solutions?

The problem here is, you want a filled/contour plot on a flat surface or 3D perspective visualisation? If you want 3D perspective visualisation, your only chances are rgl and misc3d packages. (as far as I know)

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

All the solutions above may have (or not) smooth colour change and grid depending on your settings. What do you mean exactly "no grid on it”? If you choose that you don’t have a grid, then you don’t. You only need to search a bit more.

Looks like: 

Ismail SEZEN

unread,
Apr 12, 2017, 10:05:42 PM4/12/17
to dncdd, r-help

> On 12 Apr 2017, at 09:08, dncdd <dn...@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>

dncdd via R-help

unread,
Apr 13, 2017, 6:07:28 AM4/13/17
to Ismail SEZEN, r-help
This can be a solution. Thank you. Thanks for your time.
------------------------------------------------------------------发件人:Ismail SEZEN <sezen...@gmail.com>发送时间:2017年4月13日(星期四) 10:05收件人:dncdd <dn...@aliyun.com>抄 送:r-help <r-h...@r-project.org>主 题:Re: [R] how to plot three dimension data to filled contour plot or surface plot in R Ask Question

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 visualizef <- 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 datahead(d)

k <- 16 # number of contoursalpha_min_max <- c(0.2, 0.6)colf <- colorRampPalette(rev(RColorBrewer::brewer.pal(11, "RdBu")))
# isosurfaces are at herelev <- 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 scenemisc3d::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/5578842- http://mathematica.stackexchange.com/a/198193- https://www.jstatsoft.org/article/view/v028i01

dncdd via R-help

unread,
Apr 13, 2017, 11:29:51 AM4/13/17
to Ismail SEZEN, r-help

x<-c(0.8,1.8,2.8) 
y<-c(0.8,1.8,2.8) 
z<-c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9) 
nx<-sprintf("x%s",x)
ny<-sprintf("y%s",y)
nz<-sprintf("z%s",z)

v<-c(
     0.3, 0.2, 0.1, 0, 0, 0, 0, 0, 0,
     0.3, 0.2, 0.1, 0.2, 0.3, 0.4, 0.4, 0.5, 0.5,
     0.3, 0.4, 0.5, 0.5, 0.6, 0.6, 0.6, 0.7, 0.7,
     0.3, 0.2, 0.2, 0.2, 0.3, 0.2, 0, 0, 0,
     0.3, 0.4, 0.3, 0.2, 0.3, 0.5, 0.5, 0.6, 0.6,
     0.5, 0.4, 0.5, 0.6, 0.6, 0.7, 0.6, 0.8, 0.8,
     0.3, 0.4, 0.4, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5,
     0.3, 0.2, 0.4, 0.5, 0.5, 0.6, 0.6, 0.7, 0.8,
     0.3, 0.5, 0.5, 0.6, 0.7, 0.7, 0.9, 0.8, 0.9)
myarray<-array(v, c(length(z), length(y), length(x)), dimnames=list(z,y,x))
odftarget<-as.data.frame.table(myarray)
names(odftarget) <- c('z', 'y', 'x', 'v')
dftarget <- data.frame(
    z=as.numeric(as.character(odftarget$z)),
    y=as.numeric(as.character(odftarget$y)), 
    x=as.numeric(as.character(odftarget$x)), 
    v=odftarget$v
)
head(dftarget)

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

# isosurfaces are at here

lev <- seq(min(dftarget$v), max(dftarget$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(z, y, x, type = "n", aspect = TRUE) # create scene# I am using the array here.
misc3d::contour3d(myarray, lev, z, y, x, color = colf(k), alpha = alpha, smooth = 3, engine = "rgl", add = TRUE) 
rgl::aspect3d(2, 1, 1) 

It works.Thank you.


------------------------------------------------------------------发件人:dncdd via R-help <r-h...@r-project.org>发送时间:2017年4月13日(星期四) 18:07收件人:Ismail SEZEN <sezen...@gmail.com>抄 送:r-help <r-h...@r-project.org>主 题:[R] 回复: how to plot three dimension data to filled contour plot or surface plot in R Ask Question

Reply all
Reply to author
Forward
0 new messages