小编d2a*_*a2d的帖子

用2色标ggplot2叠加彩色图

尝试使用ggplot2叠加颜色图(如下所示)

红色+绿色在第3列中给出黄色

试图使用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
Run Code Online (Sandbox Code Playgroud)

一种可能的方法是将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
Run Code Online (Sandbox Code Playgroud)

我能想出最好的结果

plot r ggplot2

6
推荐指数
1
解决办法
1164
查看次数

标签 统计

ggplot2 ×1

plot ×1

r ×1