R-scatter 3 (matlab)

Is there an equivalent function in R of scatter3 function in matlab? scatterplot3d does not have some parameters.

Example: (In Matlab)

    N = 2^11; 
    t = rand(1,N);
    t = sort(4*pi*sqrt(t))'; 
    z = 8*pi*rand(N,1); 
    x = (t+.1).*cos(t);
    y = (t+.1).*sin(t);

    cmap = jet(N);
    scatter3(x,y,z,20,cmap);

Graphical representation to be reproduced in R

The "equivalent" in R does not give the same representation:

    N = 2^11 
    t = runif(N)
    t = sort(4*pi*sqrt(t))
    z = 8*pi*runif(N) 
    x = (t+0.1)*cos(t)
    y = (t+0.1)*sin(t)

    library(gplots)
    cmap = rich.colors(N)

    library(scatterplot3d)
    par(mfrow = c(1,1))
    scatterplot3d(x,y,z, color = cmap, cex.symbols = 3, type = "b")

Not the nice graphical representation as in matlab

+4
source share
3 answers

tl; dr , to my surprise, youโ€™re right that you scatterplot3dreally donโ€™t have that kind of flexibility (I would have sworn it was). However, a very easy hack allows you to do this with persp().

Set up the data (thanks for the reproduced example).

N <- 2^11 
t <- runif(N)
t <- sort(4*pi*sqrt(t))
z <- 8*pi*runif(N) 
x <- (t+0.1)*cos(t)
y <- (t+0.1)*sin(t)

The color map corresponding to the one you got pretty close:

cmap <- colorRampPalette(c("blue","cyan"))(N)

, persp() , phi () theta r . (phi=90 x-y.) trans3d() , persp(), .

## empty plot
pp <- persp(range(x),range(y),matrix(c(min(z),max(z),0,0),2),
      col=NA,border=NA,phi=90,zlab="")
## plot:
points(trans3d(x,y,z,pp),col=cmap)

enter image description here

, ( ) . , (.. r ):

pp2 <- persp(range(x),range(y),matrix(c(min(z),max(z),0,0),2),
      col=NA,border=NA,phi=90,r=1000)
points(trans3d(x,y,z,pp2),col=cmap)

enter image description here

( z ).

, , , - x-y, plot() z???

plot(x,y,cex=5,col=cmap)

enter image description here

+4

:

scatterplot3d(x,z,y, color = cmap, cex.symbols = 1.2, pch=19, type = "b")

enter image description here

+2

If you are interested in interactivity and mirroring what is available in Matlab, take a look at the function scatter3doffered through the package car.

Code available through the help section:

require(car)
if(interactive() && require(rgl) && require(mgcv)){
    scatter3d(prestige ~ income + education, data=Duncan)
    Sys.sleep(5) # wait 5 seconds
    scatter3d(prestige ~ income + education | type, data=Duncan)
    Sys.sleep(5)
    scatter3d(prestige ~ income + education | type, surface=FALSE,
              ellipsoid=TRUE, revolutions=3, data=Duncan)
    scatter3d(prestige ~ income + education, fit=c("linear", "additive"),
              data=Prestige)
    Sys.sleep(5)
    scatter3d(prestige ~ income + education | type,
              radius=(1 + women)^(1/3), data=Prestige)
}

You will get a rather complicated 3D graph:

interactive

+1
source

Source: https://habr.com/ru/post/1664050/


All Articles