你真的很接近:只是不要将 z1 和 z2 除以 2。
还有另一个问题需要考虑。如果 z1 和 z2 不在同一个比例上,你应该为两者使用一个共同的比例,还是应该独立地对它们进行缩放?结果(可能)不同,如下图所示。
gg.overlay <- function(df) { # produces 2 color channels and the overlay
require(ggplot2)
require(gridExtra)
gg.z1 <- ggplot(df, aes(x,y))+
geom_tile(fill=rgb(red=df$z1.scale,green=0,blue=0))+
scale_x_continuous(expand=c(0,0))+
scale_y_continuous(expand=c(0,0))+
coord_fixed()
gg.z2 <- ggplot(df, aes(x,y))+
geom_tile(fill=rgb(red=0,green=df$z2.scale,blue=0))+
scale_x_continuous(expand=c(0,0))+
scale_y_continuous(expand=c(0,0))+
coord_fixed()
gg <- ggplot(df, aes(x,y))+
geom_tile(fill=rgb(red=df$z1.scale,green=df$z2.scale,blue=0))+
scale_x_continuous(expand=c(0,0))+
scale_y_continuous(expand=c(0,0))+
coord_fixed()
library(gridExtra)
grid.arrange(gg.z1, gg.z2, gg, ncol=3)
}
使用一个稍微接近您问题中图片的示例:
library(mvtnorm) # just for this example
df <- expand.grid(x=seq(-3,3,len=100),y=seq(-3,3,len=100))
df$z1 <- with(df,dmvnorm(cbind(x,y),mean=c(0,0),sigma=matrix(c(1,-1,-1,2),nc=2)))
df$z2 <- with(df,3*dmvnorm(cbind(x,y),mean=c(0,0),sigma=matrix(c(1,0,0,1),nc=2)))
# scale z1 and z2 together
max.z <- with(df,max(z1,z2))
min.z <- with(df,min(z1,z2))
df$z1.scale <- with(df, (z1-min.z)/(max.z-min.z))
df$z2.scale <- with(df, (z2-min.z)/(max.z-min.z))
gg.overlay(df)
# scale z1 and z2 separately
df$z1.scale <- with(df, (z1-min(z1))/diff(range(z1)))
df$z2.scale <- with(df, (z2-min(z2))/diff(range(z2)))
gg.overlay(df)
在第一种情况下,红色被静音,因为z1 强度低于z2 强度。在第二种情况下,我们分别缩放它们,因此红色更加鲜艳。目前尚不清楚哪个是“正确”的方法。