Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a plane to a scatterplot3d

Tags:

r

I have an equation of a line

y=sqrt(c+x^2)

and I want to add a plane to a 3d scatterplot, such that my plane is perpendicular to the x y plane and the line given above is the intersection line of the two planes.

How do I do this? I don't quite understand how plane3d works. I've read http://svitsrv25.epfl.ch/R-doc/library/scatterplot3d/html/scatterplot3d.html

But still don't get it.

like image 495
user1760348 Avatar asked Nov 25 '12 11:11

user1760348


People also ask

How do you make a 3D scatter plot?

After adding data, go to the 'Traces' section under the 'Structure' menu on the left-hand side. Choose the 'Type' of trace, then choose '3D Scatter' under '3D' chart type. Next, select 'X', 'Y' and 'Z' values from the dropdown menus. This will create a 3D scatter trace, as seen below.

What package is scatter3d in R?

The function scatter3d() uses the rgl package to draw and animate 3D scatter plots.


2 Answers

This might be what you are looking for:

library(scatterplot3d)
# y=sqrt(a+x^2) with x in (-0.5,0.5), z in (0,1) and a=0
a <- 0
x <- rep(seq(-0.5, 0.5, length = 200), each = 200)
y <- sqrt(a + x^2)
z <- rep(seq(0, 1, length = 200), 200)
scatterplot3d(x, y, z, highlight.3d = TRUE, pch = 20)

enter image description here

Edit: That would be helpful to see how did you add these other points, but let us take the second example from ?scatterplot3d

  temp <- seq(-pi, 0, length = 50)
  x2 <- c(rep(1, 50) %*% t(cos(temp)))
  y2 <- c(cos(temp) %*% t(sin(temp)))
  z2 <- c(sin(temp) %*% t(sin(temp)))

Now combining x with x2 and doing the same with others we get:

scatterplot3d(c(x,x2), c(y,y2), c(z,z2), highlight.3d = TRUE, pch = 20)

enter image description here

like image 108
Julius Vainora Avatar answered Oct 13 '22 07:10

Julius Vainora


In addition to the previous answer, once you construct a 3-D scatterplot, you can add a plane to it by creating a model and parsing it using a function nested within your scatterplot3d() container. It should look something like this:

plot3d <- scatterplot3d(x, y, z, ... )
model  <- lm(y ~ sqrt(c + x^2) + z)
plot3d$plane3d(model)

It's a very weird syntax to have a function within a container like that, but it works, giving you something like this (the dotted-line plane is visible near the center of the cube):

3-D scatterplot with plane through the center

If you would like to create one or multiple planes manually, I would use Uwe's method that I re-posted here:

spd <- scatterplot3d(1:10, 1:10, 1:10)

# xy
spd$plane3d(0.3549896,0,0,lty="dotted")

# yz
x0 <- 5
xyz1 <- spd$xyz.convert(rep(x0, 6), rep(0, 6), seq(0, 10, by=2))
xyz2 <- spd$xyz.convert(rep(x0, 6), rep(10, 6), seq(0, 10, by=2))
segments(xyz1$x, xyz1$y, xyz2$x, xyz2$y, lty="dotted")

xyz1 <- spd$xyz.convert(rep(x0, 6), seq(0, 10, by=2), rep(0, 6))
xyz2 <- spd$xyz.convert(rep(x0, 6), seq(0, 10, by=2), rep(10, 6))
segments(xyz1$x, xyz1$y, xyz2$x, xyz2$y, lty="dotted")

# zx
y0 <- 6
xyz1 <- spd$xyz.convert(rep(0, 6), rep(y0, 6), seq(0, 10, by=2))
xyz2 <- spd$xyz.convert(rep(10, 6), rep(y0, 6), seq(0, 10, by=2))
segments(xyz1$x, xyz1$y, xyz2$x, xyz2$y, lty="dotted")

xyz1 <- spd$xyz.convert(seq(0, 10, by=2), rep(y0, 6), rep(0, 6))
xyz2 <- spd$xyz.convert(seq(0, 10, by=2), rep(y0, 6), rep(10, 6))
segments(xyz1$x, xyz1$y, xyz2$x, xyz2$y, lty="dotted")

This produces planes through manual specification:

enter image description here

like image 28
Adam Erickson Avatar answered Oct 13 '22 08:10

Adam Erickson