0
我使用ggplot2生成熱圖,但NA值導致熱圖全部變爲一種顏色。NA值是否打破ggplot2熱圖?
實施例數據幀:
id<-as.factor(c(1:5))
year<-as.factor(c("Y13", "Y14", "Y15"))
freq<-c(26, 137, 166, 194, 126, 8, 4, 76, 20, 92, 4, NA, 6, 6, 17)
test<-data.frame(id, year, freq)
test
id year freq
1 Y13 26
2 Y14 137
3 Y15 166
4 Y13 194
5 Y14 126
1 Y15 8
2 Y13 4
3 Y14 76
4 Y15 20
5 Y13 92
1 Y14 4
2 Y15 NA
3 Y13 6
4 Y14 6
5 Y15 17
我用於熱圖以下:
# set color palette
jBuPuFun <- colorRampPalette(brewer.pal(n = 9, "RdBu"))
paletteSize <- 256
jBuPuPalette <- jBuPuFun(paletteSize)
# heatmap
ggplot(test, aes(x = year, y = id, fill = freq)) +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) +
geom_tile() +
scale_fill_gradient2(low = jBuPuPalette[1],
mid = jBuPuPalette[paletteSize/2],
high = jBuPuPalette[paletteSize],
midpoint = (max(test$freq) + min(test$freq))/2,
name = "Number of Violations")
結果是灰色,在整個熱圖。
當我從數據框中刪除「NA」時,heatmap呈現正確。
我已經專門指定顏色TH「NA」值(例如,通過
scale_fill_gradient2(low = jBuPuPalette[1],
mid = jBuPuPalette[paletteSize/2],
high = jBuPuPalette[paletteSize],
na.value="yellow",
midpoint = (max(test$freq) + min(test$freq))/2,
name = "Number of Violations")
然而,這只是取得了全熱圖黃色與此實驗。
我缺少的東西?。很明顯任何建議都讚賞
感謝
嘗試運行剛剛行'分鐘(測試$頻率)'或'MAX(測試$頻率)'您使用設置調色板的中點。用'na.rm = T'試試......我不能在沒有'jBuPuPalette'的情況下測試,但我猜測這是你的問題。 – Gregor
@Gregor。謝謝 - 你撞到了頭上。 na.rm = TRUE工作。非常感激。 – wallisthedog