[R] Matrix 3d plot

Duncan Murdoch murdoch.duncan at gmail.com
Tue Jul 5 19:46:21 CEST 2011


On 05/07/2011 1:36 PM, петрович wrote:
> I have a problem with a 3d plot, suppose we have a matrix like this:
>
>                     v1        v2       v3      v4
> jan-2010     0.5     0.25    0.25    0.3
> feb-2010     0.35   0.12   0.12    0.4
> mar-2010    0.15    0.25    0.25  0.1
>
> and i want to plot this matrix in 3d plot where x-axis is the first
> column of the matrix above, y - axis is the first row of the matrix
> above and the z-axis is the numbers corresponding, so
> z(jan-2010,v3)=0.25
>
> I was trying with persp() but i see that in this function z is a
> vector but in my case is a matrix, so i receive an error message
> I think this sound no so hard, but actually i couldn´t find a function
> doing this, is there actually such a function?

persp() handles exactly the case you describe, but it wants numeric x 
and y.  You didn't show what you tried, but this works:

x <- 1:3
y <- 1:4
z <- matrix(rnorm(12), nrow=3)
persp(x,y,z, col="red")

It's not a very useful plot with the random data; it might be better 
with yours.

Duncan Murdoch



More information about the R-help mailing list