Matching colors using multiple wireframes in R package lattice

Home / Uncategorized / Matching colors using multiple wireframes in R package lattice

Question:
Using the syntax below I managed to have 2 wireframes in 1 plot (after interpolation). However the color schemes differ because they are based on the range of the z values (which are different for both z1 and z2). Since in my case the z-axis is always between 0 and 1, i would like to match the color gradients for both plots. To clarify, the color gradient on the right side of the plot should always be between 0 and 1 (as is the z-axis) and not between the minimum and maximum of z1 and z2 vector respectively.

Can anyone help me out here?
library(lattice)
library(akima)
library(reshape2)

x <- rep(c(5,10,15),4) y <- c(7,7,7,14,14,14,30,30,30,60,60,60) z1 <- c(0.17, 0.3,0.38,0.14,0.24,0.32,0.1,0.17,0.18,0.08,0.12,0.23) z2 <- c(0.63,0.7,0.76,0.66,0.73,0.79,0.72,0.79,0.83,0.76,0.82,0.88) interp_z1 <-melt(interp(x,y,z1)) flat_z1 <- interp_z1 [!is.na(interp_z1$Var1)&!is.na(interp_z1$Var2),] interp_z2 <-melt(interp(x,y,z2)) flat_z2 <- interp_z2 [!is.na(interp_z2$Var1)&!is.na(interp_z2$Var2),] points_z1<-data.frame(x=min(x)+(flat_z1$Var1-1)/(40/diff(range(x))), y=min(y)+(flat_z1$Var2-1)/(40/diff(range(y))), z=flat_z1$value) points_z2<-data.frame(x=min(x)+(flat_z2$Var1-1)/(40/diff(range(x))), y=min(y)+(flat_z2$Var2-1)/(40/diff(range(y))), z=flat_z2$value) wf_z1 <- wireframe(points_z1$z ~ points_z1$x + points_z1$y ,zlim = c(0,1) ,scales = list(arrows=FALSE,cex=.7, tick.number = 4, z = list(arrows=F)) ,drape=T ,col.regions = rainbow(100, s = 1, v = 1, start = 0, end = max(1,100 - 1)/100, alpha = 1) ,screen = list(z = 60,x= -60) ,par.settings = list(box.3d = list(col=c(1,1,NA,NA,1,NA,1,1,1)))) wf_z2 <- wireframe(points_z2$z ~ points_z2$x + points_z2$y ,zlim = c(0,1) ,scales = list(arrows=FALSE,cex=.7, tick.number = 4, z = list(arrows=F)) ,drape=T ,col.regions = rainbow(100, s = 1, v = 1, start = 0, end = max(1,100 - 1)/100, alpha = 1) ,screen = list(z = 60,x= -60) ,par.settings = list(box.3d = list(col=c(1,1,NA,NA,1,NA,1,1,1)))) library(gridExtra) grid.arrange(wf_z1, wf_z2, ncol=2)


Answer:

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *