Reputation: 395
We need to plot several surfaces in R, invoking one by one the corresponding plotting commands (using plot3D
library; but the same applies for any other environment with basic R plot style). Each surface is built from data and contains data in different ranges. We can color each surface by some coloring scheme as follows:
We take some fake data as XYZ points:
out.trial<-read.csv(text="1, 1, 30
1, 2, 35
1, 3, 29
1, 4, 33
2, 1, 31
2, 2, 32
2, 3, 34
2, 4, 35
3, 1, 28
3, 2, 29
3, 3, 29.5
3, 4, 31
4, 1, 30
4, 2, 31
4, 3, 33
4, 4, 33",header=FALSE)
Now we build two colored "surfaces" as scatter plots. The second "surface" is produced just by shifting all values by 20.
library("plot3D")
scatter3D(out.trial$V1, out.trial$V2, -out.trial$V3, phi = 0, bty = "g",
col = gg.col(100,alpha=0.8), pch = 18, ticktype="detailed", xlab = "lon",
ylab ="lat", zlab = "depth",colvar=-out.trial$V3,zlim=c(-65,-25))
scatter3D(out.trial$V1, out.trial$V2, -20-out.trial$V3, phi = 0, bty =
"g", col = gg.col(100,alpha=0.8), pch = 18,
colvar=-20-out.trial$V3,zlim=c(-65,-25), add=TRUE)
plotdev()
But a standard color key function invoked in a plot command assigns colors by default for each surface from min/max range of the variables. So two surfaces with different range of values are colored each in the same manner. In the example above first surface (group of scatter points) contains values in the range 28-35, second surface - 48-55, and they both are colored in a palette from blue to maroon. I need to set the color scheme ranging (in this case) from 28 to 55 for each surface. So upper points will be yellowish, and bottom points will be bluish. How to amend the directive colvar =
to set a customized range for assigning colors?
Upvotes: 1
Views: 472
Reputation: 395
The solution is to use the parameter clim=
in all plotting directives. Next, we need to suppress showing color menu in all plot commands but one. The range of values to be colored in the example is from -55 to -29, hence clim=c(-55,-29)
:
scatter3D(out.trial$V1, out.trial$V2, -out.trial$V3, phi = 0, bty = "g",
col = gg.col(100,alpha=0.8), pch = 18, ticktype="detailed", xlab = "lon",
ylab ="lat", zlab = "depth",colvar=-out.trial$V3,zlim=c(-65,-25),clim=c(-55,-29))
scatter3D(out.trial$V1, out.trial$V2, -20-out.trial$V3, phi = 0, bty =
"g", col = gg.col(100,alpha=0.8), pch = 18,
colvar=-20-out.trial$V3,zlim=c(-65,-25), add=TRUE, clim=c(-55,-29), colkey=FALSE)
Upvotes: 1