How to add dashed lines parallel to axis lines in scatterplot 3D - r

I am trying to add dashed lines parallel to x,y,z axises in scatter plot 3D. Can I can modify following code to add dashed lines:
energy <- c(274.7539889,178.8493447,206.8084623,225.2049707,234.91386,359.9859873,251.4635995,406.480808,321.8857666,250.3560571)
time <- c(25.93572586,16.8826942,19.52192798,21.25848803,22.17497005,33.98130057,23.73720217,38.37023385,30.38478544,23.6326544)
cost <- c(6.861252677,13.22453603,13.53984311,4.355987685,21.9010971,16.41429768,15.33270222,27.78572681,26.44744434,24.62564853)
library(scatterplot3d)
with(mtcars, {scatterplot3d(
energy,time,cost,
log="xyz", zlab="Cost", ylab="Time", xlab="Energy",
cex.lab=1.5,font.lab=2,lwd=3,pch=20,
highlight.3d = T, angle = 55
)})

By saving the return value from scatterplot3d you can use some functions from it to plot points and lines. For example:
p3 = scatterplot3d(
energy,time,cost,
log="xyz", zlab="Cost", ylab="Time", xlab="Energy",
highlight.3d = T, angle = 55
)
p3$points3d(x=c(300,300),y=c(25,25),z=c(0,30),type="l",lty=2)
This is documented in the help for scatterplot3d:
Value:
[...]
points3d: function which draws points or lines into the existing plot.
There's also functions for drawing planes and a generic xyz.convert function to convert any x,y,z coordinate to a x,y coordinate on the graphics device.

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!

Plot a customized cube/box with coordinates into a larger plot3d / plot3drgl plot - R

I make a 3D Plot with the sizes -5 to 5 for x, y and z and add a point at (0, 0, 0):
library(plot3D)
library(plot3Drgl)
library(rgl)
points3D(0,0,0, xlim=c(-5,5), ylim=c(-5,5), zlim=c(-5,5), type = "p", plot=F)
plotrgl()
No I want to highlight a special area within this plot with a smaller cube/box.
The following coordinates should define the space:
xlow = -4.95
xhigh = 4.67
ylow = -3.77
yhigh = 4.88
zlow = -3.34
zhigh = 2.85
I tried to come up with the solution based on this:
draw cube into 3D scatterplot in RGL
Draw multiple 3D boxes of different dimensions and coordinates in R
But I can't customize it to my case.
Do you have any idea how I can easily add this smaller box into my 3D plot?

Get data from lines() function

I have a plot like this (black points):
I calculate the average of some points (smoothing - the red points in the plot).
Then I can draw with the lines() function a line between these points (blue in the plot). What I want now is the data of the blue line.
Does somebody know how to do this? Is it even possible?
I used the approx function which interpolates between points:
approx(x = r, y = ionizations, n = 1000)

Calculate area from Lat/Lon matrix or within a 50% contour for multiple polygons in R

I want to find the total area from multiple polygons within different contour lines from kernel densities (kde2d).
Here is an image of the kernel density and the 50% contour line. How do I calculate the area within the 50% contour line?
I also created a matrix of lat lon coordinates, which represents the points within this 50% contour line. Would it be easier to calculate the total area using these points.
Any suggestions would be greatly appreciated!
Once you have your coordinates in a cartesian system, and have done the kernel smoothing using those coordinates, you can use the contourLines function to get the coordinates of the lines, and then the areapl function from the splancs package to compute the area of each simple ring.
For example, using the example in help(kde2d):
attach(geyser)
plot(duration, waiting, xlim = c(0.5,6), ylim = c(40,100))
f1 <- kde2d(duration, waiting, n = 50, lims = c(0.5, 6, 40, 100))
image(f1)
contour(f1)
so that's our data set up - suppose we want the area in the 0.008 contour:
C8 = contourLines(f1,level=0.008)
length(C8)
[1] 3
Now C8 is a list of length 3. We need to apply the areapl function over each of these:
> sapply(C8,function(ring){areapl(cbind(ring$x,ring$y))})
[1] 14.65282 12.27329 14.75005
And we can obviously sum:
> sum(sapply(C8,function(ring){areapl(cbind(ring$x,ring$y))}))
[1] 41.67617
Now this only makes sense if the coordinates are cartesian, and if the contour lines are complete loops. If the 0.008 contour was near the edge then its possible for the contour to get clipped to the bounding box and then bad things happen. Check at least that the last point of each ring is the same as the first.

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