3D plot with additional graph in 2D plane, R - r

I am looking to replicate this reference plot with my dataset "raw_new" of 50 rows and 3 columns x=resistance, y=reactance and z=frequency. If have come so far as to use plotly:
fig <- plot_ly(raw_new, x = ~resistance, y = ~reactance, z = ~frequency)
to get this test plot. To replicate the reference plot, I still need to plot the x-y and x-z 2D-scatterplots in the respective 2D planes. How would you go about this? Thanks!

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.

Creating a hexplot

I am trying to create a figure like the one depicted in the third column of the following image:
Link for the image in case of backup.
Basically I have x and y positions of 200 particles and I have the MSD data for these 200 positions. I'd like MSD to be the value that should determine a color map for the particles in coordinates (x,y). So MSD should be like the height, or the z position corresponding to each particle in (x,y).
I am surprised at my incompetence, because I have been trying to solve this problem for the last couple of days but none of the Google searches gave me any result. The closest thing that I have found is the concept of "self-organizing map" in Matlab and R, but I do not know how to use R and Matlab's toolbox for SOM was utterly useful for my needs.
I tried the following code in Matlab and get the attached plot as a result:
clear all; close all; clc;
x = (dlmread('xdata.dat'))'; % x is 1x200 array
y = (dlmread('ydata.dat'))'; % y is 1x200 array
msd = (dlmread('msd_field.txt'))'; % msd is 1x200 array
[X,Y] = meshgrid(x,y);
Z = meshgrid(msd);
z = [X; Y; Z];
surf(z)
But I think this plot is not useful at all. What I want is a 2D scatter plot of (x,y) depicting particle positions and on top of that color code this scatter plot with the values stored in msd like the plot I showed in the beginning. How can I create this through Matlab, or any other visualization tool? Thank you in advance.
It is not clear whay you want to have. Here a scatter plot using ggplot2.
## some reproducible data
set.seed(1)
dat <- data.frame(
x = round(runif(200,-30,30),2),
y = round(runif(200,-2,30),2),
msd = sample(c(0,2,3),200,rep=T))
## scatter plot where the size/color of points depends in msd
library(ggplot2)
ggplot(dat) +
geom_point(aes(x,y,size=msd,color=msd)) +
theme_bw()

How to plot a stack of matrices or 3d volume in r

I have a 3d matrix of numbers (16x16x5). One layer is depicted by following matrix:
m = matrix (sample(0:127,256,replace=TRUE), nrow=16)
Similar 5 matrices are there in the stack.
How can I depict them as R objects? Following code does not work:
m = list(list(128),list(128), list(128), list(128), list(128))
for(i in 1:5) m[i] = matrix (sample(0:127,256,replace=TRUE), nrow=10)
How can I plot them so that each matrix is plotted as a contour-plot with semitransparent color (only one shade needed, eg gray) and 5 matrices are plotted on top of each other. It should be preferably possible to move the plot with mouse (as with plot3d or rgl.surface of rgl).
The question is similar to that of volume rendering as on this page: http://www.mathworks.in/help/matlab/ref/smooth3.html
or on this page: How can I visualize volume data as shown here, in MATLAB?
Edit: following works to produce the 3d matrix:
array(sample(0:127,256*5,replace=TRUE), c(16,16,5))
require(rgl)
plot3d(rep(1:128, 128*5),
rep(1:128,each=128*5),
rep(1:5, 128*128),
col=colorRampPalette(c(rgb(1,0,0,.1), rgb(0,0,1,.1)), alpha=TRUE)(128)[ m ])

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)

Plotting a 3D surface plot with contour map overlay, using R

I have a 3-tuple data set (X,Y,Z points) that I want to plot using R.
I want to create a surface plot from the data, and superimpose a contour map on the surface plot, so as to create the impression of the contour map being the "shadow" or projection from the surface plot. The contour map is to appear below the surface plot.
My data set looks somewhat like this:
Axis | Data Type
-------------------
X | Date value
Y | Float value
Z | Float value
How can I achieve this?
Edit:
I just saw that you pointed out one of your dimensions is a date. In that case, have a look at Jeff Ryan's chartSeries3d which is designed to chart 3-dimensional time series. Here he shows the yield curve over time:
Original Answer:
As I understand it, you want a countour map to be the projection on the plane beneath the 3D surface plot. I don't believe that there's an easy way to do this other than creating the two plots and then combining them. You may find the spatial view helpful for this.
There are two primary R packages for 3D plotting: rgl (or you can use the related misc3d package) and scatterplot3d.
rgl
The rgl package uses OpenGL to create interactive 3D plots (read more on the rgl website). Here's an example using the surface3d function:
library(rgl)
data(volcano)
z <- 2 * volcano # Exaggerate the relief
x <- 10 * (1:nrow(z)) # 10 meter spacing (S to N)
y <- 10 * (1:ncol(z)) # 10 meter spacing (E to W)
zlim <- range(z)
zlen <- zlim[2] - zlim[1] + 1
colorlut <- terrain.colors(zlen,alpha=0) # height color lookup table
col <- colorlut[ z-zlim[1]+1 ] # assign colors to heights for each point
open3d()
rgl.surface(x, y, z, color=col, alpha=0.75, back="lines")
The alpha parameter makes this surface partly transparent. Now you have an interactive 3D plot of a surface and you want to create a countour map underneath. rgl allows you add more plots to an existing image:
colorlut <- heat.colors(zlen,alpha=1) # use different colors for the contour map
col <- colorlut[ z-zlim[1]+1 ]
rgl.surface(x, y, matrix(1, nrow(z), ncol(z)),color=col, back="fill")
In this surface I set the heights=1 so that we have a plane underneath the other surface. This ends up looking like this, and can be rotated with a mouse:
scatterplot3d
scatterplot3d is a little more like other plotting functions in R (read the vignette). Here's a simple example:
temp <- seq(-pi, 0, length = 50)
x <- c(rep(1, 50) %*% t(cos(temp)))
y <- c(cos(temp) %*% t(sin(temp)))
z <- c(sin(temp) %*% t(sin(temp)))
scatterplot3d(x, y, z, highlight.3d=TRUE,
col.axis="blue", col.grid="lightblue",
main="scatterplot3d - 2", pch=20)
In this case, you will need to overlay the images. The R-Wiki has a nice post on creating a tanslucent background image.

Resources