2015-01-02 17 views
6

Cố gắng che phủ lô màu (giống như bên dưới) sử dụng ggplot2Overlay màu lô với 2 thang màu ggplot2

Red + Green giving Yellow in the 3rd column

Cố gắng sử dụng geom_tile nhưng ggplot doesnt cho phép tôi thêm 2 thang màu. Đây là một mã ví dụ.

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 

Một cách để có thể làm điều này là làm cho 2 lớp thành các nhóm khác nhau. Nhưng kết quả không nhìn trực quan.

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 

best outcome i could come up with

Trả lời

0

Một giải pháp khả thi là để tự tính toán màu sắc và sau đó vượt qua nó để geom_tile. (Tiếp vào mã ở trên)

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 

but it is too dark

Làm thế nào để tôi làm sáng các màu sắc?

3

Bạn thực sự thân thiết: chỉ không chia z1 và z2 cho 2.

Có một vấn đề khác cần xem xét. Nếu z1 và z2 không có cùng tỷ lệ, bạn có nên sử dụng thang đo chung cho cả hai hoặc bạn có nên mở rộng quy mô một cách độc lập không? Kết quả là (có thể) khác nhau, như minh họa dưới đây.

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) 
} 

Sử dụng một ví dụ hơi gần gũi hơn với những hình ảnh trong câu hỏi của bạn:

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) 

Trong trường hợp đầu tiên màu đỏ là tắt tiếng vì z1 cường độ thấp hơn cường độ z2. Trong trường hợp thứ hai, chúng tôi chia tỷ lệ chúng một cách riêng biệt, do đó, màu đỏ rực rỡ hơn. Không rõ phương pháp "đúng" là gì.

Các vấn đề liên quan