0

I want to make a 3d plot out of a number of 5 filled.contour plots by stacking them vertically. It is possible with other software, but I need to use R. I suppose more exactly this is a 4d plot

The plots should have the same colour scale and a legend bar down the side as you get with the code below

A complex example of the kind of plot I want is provided here
http://www.originlab.com/doc%5Cen/Tutorial/images/Stacked_3D_Surface_Plots/Stacked_3D_Surface.png

So far my code reads as follows:

varx=seq(-250, 250, 25)
vary=seq(-250, 250, 25)

# Import data from csv file
var1=read.csv("~/100.csv", header=F)
var2=read.csv("~/200.csv", header=F)
var3=read.csv("~/300.csv", header=F)
var4=read.csv("~/400.csv", header=F)
var5=read.csv("~/500.csv", header=F)

# Plot filled contour for z1
filled.contour(varx, vary, varz1, nlevels=25, color.palette=heat.colors, xlab="Arbitrary distance", ylab="Arbitrary distance", las=T, key.title=title(main="Intensity"), main="Intensity \nas a function of distance")

I know I can plot my variable varz1 in 3D, but essentially this is no different as the 3rd and colour dimensions are the same.

 library("plot3D")
 persp3D(x=varx, y=vary, z=varz1, clab = "Energy \nIntensity", bty="b2", xlab="Arbitrary X distance", ylab="Arbitrary Y distance", zlab="Z distance (mm)", theta=35, phi=20, ticktype = "detailed", main="Energy intensity \nas a function of distance")

Anyone have any thoughts?

TANGENT: I'm relatively new to R and haven't figured out how to iterate the filled.contourcommand for the 5 variables that I am importing. I'm sure there's a way though!

EDIT: Example code

 x1=seq(2, 10, 2)
 y1=seq(2, 10, 2)
 z1=matrix(c(0,1,2,1,0,1,2,3,2,1,2,3,4,3,2,1,2,3,2,1,0,1,2,1,0), nrow=5, byrow=T)
 z2=z1^2/3
 z3=z1^3/10

 library(rgl)
 open3d()
 bg3d("white")
 elv=0
 offs=2*elv+1
 nbcol=25
 color=rev(rainbow(nbcol, start = 0/6, end = 4/6))
 zcol=cut(z3, nbcol)
 persp3d(x=x1, y=y1, z=elv*z1+0*offs, col=color[zcol], add=T, axes=F, alpha=0.7,theta=50, phi=25, expand=1);
 persp3d(x=x1, y=y1, z=elv*z2+10*offs, col=color[zcol], add=T, axes=T, alpha=0.7);
 persp3d(x=x1, y=y1, z=elv*z3+20*offs, col=color[zcol], add=T, axes=F, alpha=0.7)

You can see that the colour contours on each level of the plot are the same when they shouldn't be. I can't figure out how to change this.

Apologies for the delay in coming back to this. Appreciate your inputs

  • 2
    You could try sth in the veins of `library(plot3D); persp3D(z = volcano, zlim=c(min(volcano), max(volcano)*3), theta=30, phi=30); persp3D(z = volcano+max(volcano)*1.5, add=T, axes=F, colkey=F)` -> http://imgur.com/lDEJtLU – lukeA Nov 13 '15 at 17:53
  • As to your tangent: read your data [into a list of data frames](http://stackoverflow.com/a/24376207/903061), not just sequentially named variables. Then iterating over it is easy with a `for` loop and easier with `lapply`. – Gregor Thomas Nov 13 '15 at 18:58
  • LukeA. That's the solution I was looking for, thanks! However, this code seems to plot the same data (varz1 say) 5 times (i.e the data in varz2, *z3.... is ignored). I take it this shouldn't happen.... – Magiclamarshmallow Nov 14 '15 at 21:31
  • @Magiclamarshmallow Yes, Luke's code just translates the volcano data up. You would, presumably, not have the same data for every level, but translate it up in the z direction for each layer just like Luke's code. – Gregor Thomas Nov 16 '15 at 22:24
  • @Gregor Yes, that's correct - different data up the z-dir, but when I plot it, it's all the same. Tried using the same structure, but with different commands (`library("rgl"), surface3d`) but no change. – Magiclamarshmallow Nov 24 '15 at 08:30
  • Well, if you make a **minimal** reproducible example, hopefully sharing a **small** illustrative data set, maybe we can help. But the volcano code in lukeA's comment looks straightforward and works nicely. If you can't make it work using unshared data and different commands not much we can do about it. – Gregor Thomas Nov 25 '15 at 19:05
  • @Gregor `x1=seq(2, 10, 2) y1=seq(2, 10, 2) z1=matrix(c(0,1,2,1,0,1,2,3,2,1,2,3,4,3,2,1,2,3,2,1,0,1,2,1,0), nrow=5, byrow=T) z2=z1^2/3 z3=z1^3/10 library(rgl) open3d() bg3d("white") elv=0 offs=2*elv+1 nbcol=100 color=rev(rainbow(nbcol, start = 0/6, end = 4/6)) zcol=cut(z1, nbcol) persp3d(x=x1, y=y1, z=elv*z1+0*offs, col=color[zcol], add=T, axes=F, alpha=0.7,theta=50, phi=25, expand=1); persp3d(x=x1, y=y1, z=elv*z2+10*offs, col=color[zcol], add=T, axes=T, alpha=0.7); persp3d(x=x1, y=y1, z=elv*z3+20*offs, col=color[zcol], add=T, axes=F, alpha=0.7) axes3d(c("x--", 'y--', 'z'))` – Magiclamarshmallow Jan 07 '16 at 14:32
  • Edit code and data into your question, don't just put it in comments. – Gregor Thomas Jan 07 '16 at 17:03
  • Inputted above now. Cheers @Gregor – Magiclamarshmallow Jan 07 '16 at 17:33

0 Answers0