2017-03-29 60 views
2

我在檢查此帖子:R ggplot2 - How do I specify out of bounds values' colour關於如何「指定超出範圍值爲彩色」。使用oob在ggplot2中將值更改爲另一種顏色

給出的答案是使用obs=squish,但不允許您指定超出範圍的顏色。在我的例子中,它只是在我的值範圍內使用最接近的顏色。如果我想用另一個突出顯示那些,該怎麼辦?

實施例:

這裏所有的值超出了範圍,以及NA被視爲NA並且被染成灰色。

dat <- matrix(rnorm(100, 3, 1), ncol=10) 
dat.m <- melt(dat) 
dat.m[c(1,5,10),3] <- NA 

ggplot(dat.m, aes(Var1, Var2)) + 
    geom_tile(aes(fill = value)) + 
    geom_text(aes(label = round(value, 1))) + 
    scale_fill_continuous("",limits=c(1,2), low = "#d73027", 
         high = "#4575b4", na.value = "grey") 

但現在我想一切都超出範圍有另一種顏色的值,說白了,但NA仍與灰色。如果我使用oob=squish,它會將它們的顏色設置爲最接近的值,但我可以在哪裏指定顏色?

ggplot(dat.m, aes(Var1, Var2)) + 
    geom_tile(aes(fill = value)) + 
    geom_text(aes(label = round(value, 1))) + 
    scale_fill_continuous("",limits=c(1,2), low = "#d73027", 
         high = "#4575b4", na.value = "grey",oob=squish) 
+0

如果我只是改變NA爲白色,然後是有方法來手動選擇哪些單元格顏色?這樣我可以選擇NA並將它們上色爲灰色 – GabrielMontenegro

回答

2

它可能最容易只使用兩個geom_tile S和依靠overplotting對於真正NA值:

ggplot(dat.m, aes(factor(Var1), factor(Var2))) + 
    geom_tile(aes(fill = value)) + 
    geom_tile(fill = 'grey', data = subset(dat.m, is.na(value))) + 
    geom_text(aes(label = round(value, 1))) + 
    scale_fill_continuous("",limits=c(1,2), low = "#d73027", 
         high = "#4575b4", na.value = "lightgreen") + 
    coord_fixed() 

enter image description here

+0

魔法!還有9個去 – GabrielMontenegro

相關問題