R: 3D scatter plot from multiple 3D arrays in scatterplot3d - r

I'm new to R so please bear with me; I have six 3D arrays (c0, ..., c5) and I need to overlap the individual arrays into one 3D scatter plot. I have one scatter plot using the c0 array with...
library(scatterplot3d) ; scatterplot3d(c0)
... and get the following plot:
Any idea how to add on the other five arrays, with different colors & shapes?
Also, any other suggestion for interactive use? Like python's 3D plots allow for zooming, rotating plot for different views, etc.
UPDATE:
The c0 array is as follows (used read.table('...')); there are 1000 elements, but this will give an idea.
> c0
C4_1548 Mg2_2796 Si4_1393
1 0.703216519 0.611332440 0.715913967
4 -0.073868874 0.333571615 0.174178337
5 0.584873346 -0.398325128 -0.038525721

Related

Rotate a 2D plot with respect to another 2D plot in R

By using this data:
x <- (-9.56257588328661,-9.32048556072206,-9.07839522647581,-8.8363048806259,-8.59421452325033,-8.35212415442714,-8.11003377423433,-7.86794338274994,-7.62585298005199,-7.38376256621848,-7.14167214132746,
-6.89958170545693,-6.65749125868491,-6.41540080108944,-6.17331033274852,-5.93121985374018,-5.68912936414245,-5.44703886403333,-5.20494835349086,-4.96285783259305,-4.72076730141793,-4.47867676004351,
-4.23658620854782,-3.99449564700888,-3.7524050755047,-3.51031449411332,-3.26822390291274,-3.026133301981,-2.7840426913961,-2.54195207123608,-2.29986144157896,-2.05777080250275,-1.81568015408548,
-1.57358949640516,-1.33149882953982,-1.08940815356747,0.847317468566159,0.605226774613874,0.363136071788658,0.121045360168516,0.121045360168516,0.363136089144421,0.605226826681189,0.847317572700783,
1.08940832712519,1.3314990898764,1.57358986087637,1.81568064004709,2.05777142731055,2.2998622225887,2.54195302580355,2.78404383687707,3.02613465573123,3.26822548228801,3.5103163164694,3.75240715819737,
3.9944980073939,4.23658886398097,4.47867972788057,4.72077059901465,4.96286147730522,5.20495236267425,5.44704325504371,5.68913415433558,5.93122506047186,6.1733159733745,6.4154068929655,6.65749781916683,
6.89958875190046,7.14167969108839,7.38377063665259,7.62586158851503,7.8679525465977,8.11004351082257,8.35213448111163,8.59422545738686,8.83631643957023,9.07840742758371,9.32049842134931,9.56258942078898)
y <- (19.0666307196427,18.0071347931614,16.9804259667753,15.9862852584879,15.0244936863046,14.0948322682304,13.1970820222693,12.3310239664272,11.4964391187079,10.6931084971172,9.92081311965921,9.17933400433844,
8.46845216916017,7.7879486321292,7.13760441125009,6.51720052452788,5.92651798996712,5.36533782557308,4.83344104934986,4.33060867930271,3.85662173343621,3.41126122975516,2.99430818626433,2.605543620969,
2.24474855187304,1.91170399698262,1.60619097430072,1.32799050183303,1.07688359758527,0.85265127956086,0.655074565765517,0.483934474203352,0.33901202287916,0.220088229798193,0.12694411296502,0.0593606903842101,
0.0171189800610136,0,0.00778476820619289,0.0402543026837066,0.0971896214380195,0.178371742473928,0.283581683795319,0.412600463408353,0.565209099316689,0.741188609525352,0.94032001203982,1.16238432486421,
1.40716256600308,1.67443575346238,1.9639849052453,2.27559103935823,2.60903517380461,2.96409832658992,3.34056151571917,3.73820575919694,4.1568120750278,4.59616148121631,5.05603499576796,5.53621363668708,
6.03647842197847,6.55661036964739,7.09639049769794,7.65559982413561,8.23401936696428,8.83143014418988,9.44761317381585,10.0823494738486,10.7354200622915,11.4066059571505,12.0956881764293,12.8024477381327,
13.5266656602669,14.2681229608356,15.0266006578429,15.8018797692955,16.5937413131967,17.4019663075519,18.2263357703655,19.0666307196427)
df <- data.frame(x = x,y = y)
I am plotting a 2D plot which looks like this:
I have several plots like this. Now I want to combine those several 2D plots to form a 3D plot. Such that, 2nd 2D plot is rotated with respect to 1st 2D plot by some angle say 45 degrees. And similarly, all those 2D plots are needed to fit into a 3D plot with a certain angle in between each of them.
The final 3D plot looks like this:
FYI: Each 2D plot is stored in a separate data frame as mentioned above. I need to access and transform each data frame with respect to others.

Why Scilab does not show 3D plots?

I'm new to scilab so working out basics.Below script opens the graphics window shows the empty box.I guess a straight line should be shown for these x,y,z points which doesn't show up here.Why is that so?
x=linspace(1,100)
y=linspace(1,100)
z=linspace(1,100)
plot3d(x,y,z)
plot3d plots surfaces, and you give it 3 vectors instead of matrices. With 3 vectors you can plot a (parametric) curve in 3 dimensions with param3d:
x=linspace(1,100)
y=linspace(1,100)
z=linspace(1,100)
param3d(x,y,z)

R: How to plot a 3D array

I want to plot a 3D array M where
M <- array(runif(64),dim=c(4,4,4))
A similar question is here with comments that this can be done using a common 3D plot in R, but I could find no such function in R which can be used to plot multidimensional arrays (say, a 3D array as in the above example). Any suggestion how to do it? Thanks.
Use melt to create a table of x,y,z,value, and then rgl to do a 3d plot:
library(reshape2)
library(rgl)
M=melt(M)
points3d(M$Var1,M$Var2,M$Var3)
That's just 64 points in a cube. You can scale and colour them:
points3d(M$Var1,M$Var2,M$Var3,size=10,color=rainbow(10)[M$value*10])
Use whatever method of mapping M$value to colour you prefer. Don't use rainbow palettes for real!

Why is my plot3d white in SciLab?

t = 0:%pi/50:10*%pi;
plot3d(sin(t),cos(t),t)
When I execute this code the plot is done but the line is not visible, only the box. Any ideas which property I have to change?
Thanks
The third argument should, in this case, be a matrix of the size (length arg1) x (length arg2).
You'd expect plot3d to behave like an extension of plot and plot2d but it isn't quite the case.
The 2d plot takes a vector of x and a vector of y and plots points at (x1,y1), (x2,y2) etc., joined with lines or not as per style settings. That fits the conceptual model we usually use for 2d plots - charting the relationship of one thing as a function of another, in most cases (y = f(x)). THere are other ways to use a 2d plot: scatter graphs are common but it's easy enough to produce one using the two-rows-of-data concept.
This doesn't extend smoothly to 3d though as there are many other ways you could use a 3d plot to represent data. If you gave it three vectors of coordinates and asked it to draw a line between them all what might we want to use that for? Is that the most useful way of using a 3d plot?
Most packages give you different visualisation types for the different kinds of data. Mathematica has a lot of 3d visualisation types and Python/Scipy/Mayavi2 has even more. Matlab has a number too but Scilab, while normally mirroring Matlab, in this case prefers to handle it all with the plot3d function.
I think of it like a contour plot: you give it a vector of x and a vector of y and it uses those to create a grid of (x,y) points. The third argument is then a matrix whose dimensions match those of the (x,y) grid holding the z-coordinates of each point. The first example in the docs does what I think you're after:
t=[0:0.3:2*%pi]';
z=sin(t)*cos(t');
plot3d(t,t,z);
The first line creates a column vector of length 21
-->size(t)
ans =
21. 1.
The second line computes a 21 x 21 matrix of products of the permutations of sin(t) with cos(t) - note the transpose in the cos(t') element.
-->size(z)
ans =
21. 21.
Then when it plots them it draws (x1,y1,z11), (x1,y2,x12), (x2,y2,z22) and so on. It draws lines between adjacent points in a mesh, or no lines, or just the surface.

Plotting lines between two points in 3D

I am writing an regression algorithm which tries to "capture" points inside boxes. The algorithm tries to keep the boxes as small as possible, so usually the edges/corners of the boxes go through points, which determines the size of the box.
Problem: I need graphical output of the boxes in R. In 2D it is easy to draw boxes with segments(), which draws a line between two points. So, with 4 segments I can draw a box:
plot(x,y,type="p")
segments(x1,y1,x2,y2)
I then tried both the scatterplot3d and plot3d package for 3D plotting. In 3D the segments() command is not working, as there is no additional z-component. I was surprised that apparently (to me) there is no adequate replacement in 3D for segments()
Is there an easy way to draw boxes / lines between two points when plotting in three dimensions ?
The scatterplot3d function returns information that will allow you to project (x,y,z) points into the relevant plane, as follows:
library(scatterplot3d)
x <- c(1,4,3,6,2,5)
y <- c(2,2,4,3,5,9)
z <- c(1,3,5,9,2,2)
s <- scatterplot3d(x,y,z)
## now draw a line between points 2 and 3
p2 <- s$xyz.convert(x[2],y[2],z[2])
p3 <- s$xyz.convert(x[3],y[3],z[3])
segments(p2$x,p2$y,p3$x,p3$y,lwd=2,col=2)
The rgl package is another way to go, and perhaps even easier (note that segments3d takes points in pairs from a vector)
plot3d(x,y,z)
segments3d(x[2:3],y[2:3],z[2:3],col=2,lwd=2)

Resources