Rotate a 2D plot with respect to another 2D plot in R - 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.

Related

3D plot with additional graph in 2D plane, 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!

How to combine multiple 2D R plots into a single 3D plot

I am trying to stack multiple 2d plots into a single 3d plot in R.
For example, I want to try and put these plots
into a single 3d image. The z axis would be the biomass density, y axis be the size and the x axis be the time step (and then hopefully I could use a surface plot to join up all the lines). My aim is to try and show the wave travelling through the size over a long period of time instead of showing lots of images.
Does anyone know how to do this?

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 ])

polar image plot, how to do it with R?

I cannot find a straightforward way to make a nice image plot in R, but in polar coordinates. I'm basically attempting to find a R equivalent for the 'polarplot3d' function in MATLAB. I've been playing around with ggplot2 package but without much luck. Am I missing a package that contains functionality for what I'm attempting? thanks in advance for any pointers.
Ok, I'm trying to be more clear about what I'm trying to do. Lets say I want to define a polar coordinate grid, increments in the radial direction are 50m and 2.5 degrees in theta. This should look like a dartboard.
My data (r and angle in below code) are correspond to a radial distance measure and an angle. My desired z-value is the counts of a bivariate histogram between r and angle within the increments described above defining the grid.
My data is like the following:
# synthetic data for angle and distance #
angle <- rnorm(500,mean=90,sd=15)
r <- rnorm(500,mean=700,sd=200)
# bivariate histogram #
observations <- table(cut(angle,breaks=c(seq(0,360,by=2.5))),cut(r,breaks=c(seq(0,1400,by=50))))
# the 'z' data are in observations for each bin of bivariate histogram #
# hot to plot a polar coord image? #
It's very slow to render on my system, but
library(reshape2)
library(ggplot2)
mm <- melt(counts)
ggplot(mm,aes(Var1,Var2,fill=value))+geom_tile()+coord_polar()
ggsave("polar1.png")
appears to work.
I think the following could work. Use mapproject() from the maproj library to transform my xy coordinates acording to a polar projection (or another), Then use as.image() (from fields package) function to build a image object from my new coordiantes and my Z values. Eventually use image.plot().
library("mapproj")
xyProj <- mapproject(x, y, projection="conic", parameters=-90)
library("fields")
im <- as.image(z, x=xyProj)
image.plot(im)

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