Thuong Nguyen
Thuong Nguyen

Reputation: 17

Plot two 3-d graphs together in r

I am amateur in R and want to plot two 3-d graphs in R and compare them. For example, here is my R-code:

theta <- 0.3

f <- function(u,v,theta=0.5){return(1/(1-theta*u*v-theta*(1-theta*u*v)^(-2)*(1-2*u-2*v+3*u*v+(1-theta*u*v)^(-3)*2*theta^2*u*v*(1-u)*(1-v))))}
x<- seq(0,1,length=20)
y<-x
z<-outer(x,y,Vectorize(f))
z[is.na(z)]<-1
op<-par(bg = "white")
persp(x,y,z,theta=40, phi=30, expand = 0.5,ltheta=120,shade =0.1, col="tomato",zlim=c(0,2),main="", zlab="",xlab="X", ylab="Y")
z2<-matrix(rep(1,c(20*20)),ncol=20)
par(new=TRUE)
persp(x,y, z2, theta = 40, phi = 30, expand = 0.5, col = NA, border="lightgreen", zlab="",zlim=c(0,2))

The problem is, I can not see clearly from the graph at which part, the points of f is greater than 1 and vice versa. I want to make this to be more visible. Any suggestion would be greatly appreciated.

Upvotes: 0

Views: 132

Answers (1)

Patric
Patric

Reputation: 2131

How about plot the difference between z and 1, and then distinguish by different color for large or less then 1.

Very simple code (just show function) as below:

 z3 <- z -z2
 zcol <- sapply(z3, FUN=function(x) if(x>0) "red" else "blue")
 # Trick in here, see help(presp):
 # col the color(s) of the surface facets...
 # This is recycled to the (nx-1)(ny-1) facets
 zcol <- matrix(zcol, ncol=20)[1:19, 1:19]
 persp(x,y, z3, theta = 40, phi = 30, expand = 0.5, col = zcol , border= NA, zlab="", zlim=c(-1,1))

Plotting as below and you can keep improve it by:

  1. using the dark/light color to show the distance;
  2. trying 2D plotting which will be more clear to show the difference.

color

Upvotes: 1

Related Questions