3D or 4D plot

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

3D or 4D plot

kapo coulibaly-2
I have data on a regular grid in the format: x  y  z data. I would like to
be able to plot them in 3d kind of like a volume or a mesh with colors. Is
that possible in R and how?

Thanks

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: 3D or 4D plot

Duncan Murdoch
On 2/20/2009 10:50 AM, kapo coulibaly wrote:
> I have data on a regular grid in the format: x  y  z data. I would like to
> be able to plot them in 3d kind of like a volume or a mesh with colors. Is
> that possible in R and how?

There are lots of ways, depending on what you want the plot to look
like.  One is

library(rgl)
plot3d(x, y, z, col=color)

(assuming you also have a variable indicating the color to plot).

Duncan Murdoch

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: 3D or 4D plot

Guo-Hao Huang
In reply to this post by kapo coulibaly-2
You can try rgl package.
It utilizes OpenGL library.

Or, check the CRAN to find other gtk based package

----- Original Message -----
From: "kapo coulibaly" <[hidden email]>
To: <[hidden email]>
Sent: Friday, February 20, 2009 11:50 PM
Subject: [R] 3D or 4D plot


I have data on a regular grid in the format: x  y  z data. I would like to
be able to plot them in 3d kind of like a volume or a mesh with colors. Is
that possible in R and how?

Thanks

[[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
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.

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: 3D or 4D plot

kapo coulibaly-2
In reply to this post by Duncan Murdoch
Ideally I would want it to look like a rubik cube with each little cube
color coded based on the fourth column (data column). Your suggestion might
work if I could color code based on data in the fourth column.
Thanks

On Fri, Feb 20, 2009 at 10:56 AM, Duncan Murdoch <[hidden email]>wrote:

> On 2/20/2009 10:50 AM, kapo coulibaly wrote:
>
>> I have data on a regular grid in the format: x  y  z data. I would like to
>> be able to plot them in 3d kind of like a volume or a mesh with colors. Is
>> that possible in R and how?
>>
>
> There are lots of ways, depending on what you want the plot to look like.
>  One is
>
> library(rgl)
> plot3d(x, y, z, col=color)
>
> (assuming you also have a variable indicating the color to plot).
>
> Duncan Murdoch
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: 3D or 4D plot

Duncan Murdoch
On 2/20/2009 1:46 PM, kapo coulibaly wrote:
> Ideally I would want it to look like a rubik cube with each little cube
> color coded based on the fourth column (data column). Your suggestion might
> work if I could color code based on data in the fourth column.
> Thanks

There's no primitive "cube" symbol in rgl, but you can get an array of
colour-coded spheres:

x <- rep(1:3, each=9)
y <- rep(rep(1:3, each=3), 3)
z <- rep(1:3, 9)
colours <- terrain.colors(27)
plot3d(x,y,z,col=colours, type="s", size=10)

If you really want cubes, you can put them together (start with cube3d()
to get one, and build on that), but it's a lot of work.

Duncan Murdoch

>
> On Fri, Feb 20, 2009 at 10:56 AM, Duncan Murdoch <[hidden email]>wrote:
>
>> On 2/20/2009 10:50 AM, kapo coulibaly wrote:
>>
>>> I have data on a regular grid in the format: x  y  z data. I would like to
>>> be able to plot them in 3d kind of like a volume or a mesh with colors. Is
>>> that possible in R and how?
>>>
>>
>> There are lots of ways, depending on what you want the plot to look like.
>>  One is
>>
>> library(rgl)
>> plot3d(x, y, z, col=color)
>>
>> (assuming you also have a variable indicating the color to plot).
>>
>> Duncan Murdoch
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> 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.

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: 3D or 4D plot

Duncan Murdoch
On 2/20/2009 1:55 PM, Duncan Murdoch wrote:

> On 2/20/2009 1:46 PM, kapo coulibaly wrote:
>> Ideally I would want it to look like a rubik cube with each little cube
>> color coded based on the fourth column (data column). Your suggestion might
>> work if I could color code based on data in the fourth column.
>> Thanks
>
> There's no primitive "cube" symbol in rgl, but you can get an array of
> colour-coded spheres:
>
> x <- rep(1:3, each=9)
> y <- rep(rep(1:3, each=3), 3)
> z <- rep(1:3, 9)
> colours <- terrain.colors(27)
> plot3d(x,y,z,col=colours, type="s", size=10)
>
> If you really want cubes, you can put them together (start with cube3d()
> to get one, and build on that), but it's a lot of work.

But it's Friday, so fun things like that are worth doing.  Here's some
code to draw a bunch of cubes with a variety of colours.  Elaborate on
it if you like.

Duncan Murdoch

cubes3d <- function(x,y,z,col="red",size=0.9,plot=TRUE) {

     xyz <- xyz.coords(x, y, z, recycle = TRUE)
     x <- xyz$x
     y <- xyz$y
     z <- xyz$z

     col <- rep(col, len=length(x))
     size <- rep(size/2, len=length(x))

     result <- list(vb=matrix(0, 4, 0), ib=matrix(1L, 4, 0),
primitivetype="quad",
                    material=list(color=NULL, normals=NULL))
     class(result) <- "qmesh3d"

     for (i in seq_along(x)) {
        cube <- translate3d(scale3d(cube3d(), size[i], size[i],
size[i]), x[i], y[i], z[i])
        offset <- ncol(result$vb)
        result$vb <- cbind(result$vb, cube$vb)
        result$ib <- cbind(result$ib, cube$ib + offset)
        result$material$color <- c(result$material$color, rep(col[i],
4*ncol(cube$ib)))
     }
     if (plot)
        shade3d(result)
     invisible(result)
}

x <- rep(1:5, each=25)
y <- rep(rep(1:5, each=5), 5)
z <- rep(1:5, 25)
cubes3d(x,y,z,col=terrain.colors(125))

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: 3D or 4D plot

Tony Breyal
Wow, i had no idea R was capable of producing graphics like that,
smegging awesome!

On 20 Feb, 19:28, Duncan Murdoch <[hidden email]> wrote:

> On 2/20/2009 1:55 PM, Duncan Murdoch wrote:
>
>
>
> > On 2/20/2009 1:46 PM, kapo coulibaly wrote:
> >> Ideally I would want it to look like a rubik cube with each little cube
> >> color coded based on the fourth column (data column). Your suggestion might
> >> work if I could color code based on data in the fourth column.
> >> Thanks
>
> > There's no primitive "cube" symbol in rgl, but you can get an array of
> > colour-coded spheres:
>
> > x <- rep(1:3, each=9)
> > y <- rep(rep(1:3, each=3), 3)
> > z <- rep(1:3, 9)
> > colours <- terrain.colors(27)
> > plot3d(x,y,z,col=colours, type="s", size=10)
>
> > If you really want cubes, you can put them together (start with cube3d()
> > to get one, and build on that), but it's a lot of work.
>
> But it's Friday, so fun things like that are worth doing.  Here's some
> code to draw a bunch of cubes with a variety of colours.  Elaborate on
> it if you like.
>
> Duncan Murdoch
>
> cubes3d <- function(x,y,z,col="red",size=0.9,plot=TRUE) {
>
>      xyz <- xyz.coords(x, y, z, recycle = TRUE)
>      x <- xyz$x
>      y <- xyz$y
>      z <- xyz$z
>
>      col <- rep(col, len=length(x))
>      size <- rep(size/2, len=length(x))
>
>      result <- list(vb=matrix(0, 4, 0), ib=matrix(1L, 4, 0),
> primitivetype="quad",
>                     material=list(color=NULL, normals=NULL))
>      class(result) <- "qmesh3d"
>
>      for (i in seq_along(x)) {
>         cube <- translate3d(scale3d(cube3d(), size[i], size[i],
> size[i]), x[i], y[i], z[i])
>         offset <- ncol(result$vb)
>         result$vb <- cbind(result$vb, cube$vb)
>         result$ib <- cbind(result$ib, cube$ib + offset)
>         result$material$color <- c(result$material$color, rep(col[i],
> 4*ncol(cube$ib)))
>      }
>      if (plot)
>         shade3d(result)
>      invisible(result)
>
> }
>
> x <- rep(1:5, each=25)
> y <- rep(rep(1:5, each=5), 5)
> z <- rep(1:5, 25)
> cubes3d(x,y,z,col=terrain.colors(125))
>
> ______________________________________________
> [hidden email] mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: 3D or 4D plot

Duncan Murdoch
In reply to this post by Duncan Murdoch
One more followup on this:  I just added a function shapelist3d() to rgl
(so far only on R-forge, not CRAN) that automates a lot of this.  To get
the plot as below, the following code works:

x <- rep(1:5, each=25)
y <- rep(rep(1:5, each=5), 5)
z <- rep(1:5, 25)
shapelist3d(cube3d(), x,y,z, size=0.45, col=terrain.colors(125))

But now you can do fancier stuff, like

shapelist3d(list(tetrahedron3d(), cube3d(), octahedron3d(),
                  dodecahedron3d(), icosahedron3d()),
                  x,y,z, size=0.45, col=terrain.colors(125))

Duncan Murdoch

On 2/20/2009 2:28 PM, Duncan Murdoch wrote:

> On 2/20/2009 1:55 PM, Duncan Murdoch wrote:
>> On 2/20/2009 1:46 PM, kapo coulibaly wrote:
>>> Ideally I would want it to look like a rubik cube with each little cube
>>> color coded based on the fourth column (data column). Your suggestion might
>>> work if I could color code based on data in the fourth column.
>>> Thanks
>>
>> There's no primitive "cube" symbol in rgl, but you can get an array of
>> colour-coded spheres:
>>
>> x <- rep(1:3, each=9)
>> y <- rep(rep(1:3, each=3), 3)
>> z <- rep(1:3, 9)
>> colours <- terrain.colors(27)
>> plot3d(x,y,z,col=colours, type="s", size=10)
>>
>> If you really want cubes, you can put them together (start with cube3d()
>> to get one, and build on that), but it's a lot of work.
>
> But it's Friday, so fun things like that are worth doing.  Here's some
> code to draw a bunch of cubes with a variety of colours.  Elaborate on
> it if you like.
>
> Duncan Murdoch
>
> cubes3d <- function(x,y,z,col="red",size=0.9,plot=TRUE) {
>
>      xyz <- xyz.coords(x, y, z, recycle = TRUE)
>      x <- xyz$x
>      y <- xyz$y
>      z <- xyz$z
>
>      col <- rep(col, len=length(x))
>      size <- rep(size/2, len=length(x))
>
>      result <- list(vb=matrix(0, 4, 0), ib=matrix(1L, 4, 0),
> primitivetype="quad",
>                     material=list(color=NULL, normals=NULL))
>      class(result) <- "qmesh3d"
>
>      for (i in seq_along(x)) {
>         cube <- translate3d(scale3d(cube3d(), size[i], size[i],
> size[i]), x[i], y[i], z[i])
>         offset <- ncol(result$vb)
>         result$vb <- cbind(result$vb, cube$vb)
>         result$ib <- cbind(result$ib, cube$ib + offset)
>         result$material$color <- c(result$material$color, rep(col[i],
> 4*ncol(cube$ib)))
>      }
>      if (plot)
>         shade3d(result)
>      invisible(result)
> }
>
> x <- rep(1:5, each=25)
> y <- rep(rep(1:5, each=5), 5)
> z <- rep(1:5, 25)
> cubes3d(x,y,z,col=terrain.colors(125))
>
>

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: 3D or 4D plot

Mike Prager
In reply to this post by kapo coulibaly-2
kapo coulibaly <[hidden email]> wrote:

> I have data on a regular grid in the format: x  y  z data. I would like to
> be able to plot them in 3d kind of like a volume or a mesh with colors. Is
> that possible in R and how?
>

I've posted code for a 4-D plot here:

http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=90

It seems to be quite close to what you're looking for.

Mike

--
Mike Prager, NOAA, Beaufort, NC
* Opinions expressed are personal and not represented otherwise.
* Any use of tradenames does not constitute a NOAA endorsement.

______________________________________________
[hidden email] mailing list
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.