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.
Related
I have two vectors representing x and y-coordinates in a scatter plot, and a thrid variable (z) for each (x,y)-coordinate representing the variable from which to draw contour lines. Example data are given as follows:
df<-data.frame(x=runif(n=30,min=-6,max=6),
y=runif(n=30,min=-6,max=10),
z=seq(1,100,length.out=30))
I use the R-package akima to generate the z-matrix for the contour plot
library(akima)
M1 <- interp(x=df$x,y=df$y,z=df$z)
contour(x=M1$x,y=M1$y,z=M1$z)
I now want to draw arrows perpendicular to the contourlines, preferably using something like the function "quiver" in the R-package pracma, with the origin of an arrow at every (x,y)-coordinate and with the arrow pointing in the direction of the gradient of the contourlines. Is there a way to do this?
My best idea so far is to somehow extract (x,y)-gradients of the contourlines and use these as velocities in the quiver function.
Grateful for any assistance.
The pracma package has a gradient function that can do this for you using the original M1$z values. For example, using your code to get M1 after set.seed(123):
contour(x=M1$x,y=M1$y,z=M1$z, asp = 1) # asp = 1 needed so things look perpendicular
library(pracma)
g <- gradient(M1$z, M1$x, M1$y)
x <- outer(M1$x, M1$y, function(x, y) x)
y <- outer(M1$x, M1$y, function(x, y) y)
quiver(x, y, g$Y, g$X, scale = 0.02, col = "blue")
Note that the gradient labels in the quiver plot have been swapped. Maybe I set up the x and y values transposed from the way the package expects. Here's what you get:
I want to create 50 concentric circles. I did it with python but now I want to do this in R. I have tried the symbols function but with no result. I want my circles to start from x,y coordinates and the radius of each circle to be 3times bigger than the previous.
step=1
for(i in seq(1,50,1)){
symbols (x, y, circles=50, col="grey")
step=step+3
}
From this I get one circle as a result.
I am new in programming so it is probably very simple. Should I use a specific package?
The beauty of R is that many things can be vectorized, including the imput to the 'symbols' function. Here's an example for you:
#vector of radii
#written in a way that's easily changable
n_circles <- 50
my_circles <- seq(1,by=1,length.out = n_circles)
#generate x and y
x <- rep(1,n_circles)
y <- rep(1, n_circles)
#plot
symbols(x,y,1:n_circles)
Recently I stumbled over the rgl-Package in R, which can be used to create interactive 3d plots. Now I want to visualize a set of boxes in one 3d plot. A Box B has cartesian coordinates B_coord=[x,y,z], which correspond to the lower left back corner and dimensions B_dim=[x1,y1,z1].
Apparently it is easy to draw, scale and position some cubes with the following exemplary code:
open3d()
printBox <- function(x,y,z,x1,y1,z1) {
mycube <- scale3d(cube3d(),x1,y1,z1)
wire3d(translate3d(mycube,x,y,z))
}
printBox(0,0,0,1,1,1)
With this code the boxes are moved to x,y,z and scaled to x1,y1,z1. My question is how to write a similar function with the same input which positions the boxes by the coordinates of their lower left back corner and draws a box with the dimensions x1, y1, z1. I am not tied to the rgl package and R, but I like its interactive 3d view.
Thank you for your ideas!
I think your code already does that. So as to make it more clear, and explain what those rgl functions do, I unrolled your function and commented it and put it in a more illustrative example.
library(rgl)
open3d()
# create and plot a box at (x,y,z) of size (x1,y1,z1)
printBox <- function(x, y, z, x1, y1, z1) {
mycube <- cube3d() # create a cube as mesh object
mycube <- scale3d(mycube, x1, y1, z1) # now scale that object by x1,y1,z1
mycube <- translate3d(mycube, x, y, z) # now move it to x,y,z
wire3d(mycube) # now plot it to rgl as a wireframe
}
# Display 5 boxes along a diagonal line
n <- 5
for (i in 1:n) {
x <- i/n
y <- i/n
z <- i/n
sz <- 1/(2*n)
printBox(x, y, z, sz,sz,sz )
}
axes3d() # add some axes
Is there any way to input a fixed vector of colours to any 3D rgl plots? If so it would be possible to extrude a map tile to a 3D surface based on a raster of the same area. But I'm finding the surface3d function behaves the same as raster::plot by insisting on mapping the input colour vector to the z variable. Is this beyond rgl's functionality at present?
I don't actually know if what you say about the coloring is correct for all rgl coloring functions, but it is not correct for rgl.surface(). This is a corruption of the example on the ?rgl.surface page. The color vector index was formed from the x-y (actually x-z) coordinates and gives a striping effect because they were modulo-ized to pull values from from a limited range.
library(rgl)
data(volcano)
y <- 2 * volcano
x <- 10 * (1:nrow(y))
z <- 10 * (1:ncol(y))
ylim <- range(y)
ylen <- ylim[2] - ylim[1] + 1
colorlut <- terrain.colors(ylen)
col <- colorlut[(x+length(x)*y +1)%%ylen ]
rgl.open()
rgl.surface(x, z, y, color=col, back="lines")
rgl.snapshot("striped_volcano.png")
3-D graphing with Google(http://www.r-bloggers.com/3-d-graphing-with-google/)
(mu1=0 mu2=0 sigma1=1 sigma2=1 pho=0)
exp((-1/2)*(x^2+y^2))/(2*pi) from -3 to 3
The rotate plot will be showd from google. The profile was a circle.
Dear Prof. Bolker gave me the R code:
library("emdbook")
library("rgl")
curve3d(dmvnorm(c(x,y),mu=c(0,0),Sigma=diag(2)),
sys3d="rgl",front="line",back="line",
xlim=c(-3,3),ylim=c(-3,3))
How to specify z axis range and get the plot like google's plot?
If pho=0 then the profile parallel to XY plane was circle.If pho<>0 then the profile parallel to XY plane was ellipse. How to add circle or ellipse in 3D plot? Thanks.
I am not sure that I fully understand your question but:
1/ I do not think rgl allow to specify z axis range (and curve3d seems to allow it only for xlim, ylim) so you probably need to do it by hand
2/ You can rescale axis in rgl using rgl.viewpoint : e.g., rgl.viewpoint(scale=c(1,1,0.1))
3/ You can draw circle or ellipse using:
t <- matrix(seq(-pi/2,pi/2, len=50), 50, 50, byrow=TRUE)
p <- matrix(seq(-pi, pi, len=50), 50, 50)
r <- 10
x <- r*cos(t)*cos(p)
y <- r*cos(t)*sin(p)
z <- r*sin(t)
persp3d(x, y, z)