【问题标题】:Overlay of color plots with 2 color scales ggplot2具有 2 个色阶 ggplot2 的颜色图叠加
【发布时间】:2015-01-02 07:06:27
【问题描述】:

尝试使用 ggplot2 覆盖彩色图(如下所示)

尝试使用 geom_tile 但 ggplot 不允许我添加 2 个色标。这是一个示例代码。

df <- data.frame(expand.grid(1:5,1:5))
df$z1 <- runif(nrow(df))
df$z2 <- runif(nrow(df))
g1 <- ggplot(df,aes(Var1,Var2)) + theme_bw()
#layer 1
g11 <- g1  + geom_tile(aes(fill=z1),alpha=0.5) + scale_fill_gradient(low="white", high="red")
#layer 2
g12 <- g1  + geom_tile(aes(fill=z2),alpha=0.5) + scale_fill_gradient(low="white", high="green")
g11
g12

一种可能的方法是将 2 层作为不同的组。但结果看起来并不直观。

mdf=melt(df,'id'=1:2)
g2 <- ggplot(mdf,aes(Var1,Var2,fill = factor(variable),alpha = value)) + 
     geom_tile() + scale_fill_manual(values = c('red','green')) + theme_bw()
g2

【问题讨论】:

    标签: r plot ggplot2


    【解决方案1】:

    你真的很接近:只是不要将 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 强度。在第二种情况下,我们分别缩放它们,因此红色更加鲜艳。目前尚不清楚哪个是“正确”的方法。

    【讨论】:

      【解决方案2】:

      一种可能的解决方案是手动计算颜色,然后将其传递给 geom_tile。 (继续上面的代码)

      cols=rgb(red=df$z1/2,green=df$z2/2,blue=rep(0,nrow(df)))
      g1 <- ggplot(df,aes(Var1,Var2)) + theme_bw() + geom_tile(fill=cols)
      g1
      

      如何使颜色变亮?

      【讨论】:

        猜你喜欢
        • 2012-06-14
        • 1970-01-01
        • 2012-05-10
        • 1970-01-01
        • 2015-04-16
        • 2013-07-17
        • 2016-01-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多