2015-02-17 49 views
1

我繪製以下csv文件的熱圖:積熱圖包括NA

"people","1","2","3","4","5","6","7","8","9" 
"Ej1",0,0,0,1,0,1,1,1,0 
"Ej2",0,1,1,0,0,0,1,1,0 
"Ej3",0,1,1,1,0,0,0,1,1 
"Ej4",0,1,0,0,1,1,0,0,1 
"Ej5",1,0,1,1,0,1,1,1,1 
"Ej6",1,1,0,1,1,1,0,0,0 
"Ej7",0,1,1,0,0,0,0,1,1 
"Ej8",0,0,1,1,1,1,1,0,0 
"Ej9",1,1,0,0,1,0,0,1,1 

使用下面的代碼,我得到下面的熱圖和工程罰款

library(reshape2) 
library(ggplot2) 
library(scales) 
library(plyr) 
data <- read.csv("fruits2.txt", head=TRUE, sep=",") 
data$people <- factor(data$people,levels=rev(data$people)) 
data.m = melt(data) 
#data.m <- ddply(data.m, .(variable), transform, rescale = rescale(value)) 
data.m[,"rescale"]<-rescale(data.m[,"value"],to=c(0,1)) 
fewer.labels <- c("Ej9","Ej8","Ej7","Ej6","Ej5","Ej4","Ej3","Ej2","Ej1") 
p <- ggplot(data.m, aes(variable, people)) + 
    geom_tile(aes(fill = rescale), colour = "white") + 
    scale_y_discrete(labels=fewer.labels) + 
    scale_fill_gradient(low = "red", high = "green") + 
    theme(axis.text=element_text(size=8)) 

enter image description here

現在我試圖繪製相同的fruits.txt文件,但包含一些缺失的值,因此該文件包含NA值。當整個列是NA,那麼它不會繪製該列,是否可以繪製該列的陰影,因爲它與孤立的NA值一樣?

"people","1","2","3","4","5","6","7","8","9" 
"Ej1",0,0,0,1,0,1,1,NA,0 
"Ej2",0,1,1,0,0,0,1,NA,0 
"Ej3",0,1,1,1,0,0,0,NA,1 
"Ej4",0,NA,0,0,NA,1,0,NA,1 
"Ej5",1,0,1,1,0,1,1,NA,1 
"Ej6",1,1,0,1,1,1,0,NA,0 
"Ej7",0,1,1,0,0,0,0,NA,1 
"Ej8",0,0,1,1,1,1,1,NA,0 
"Ej9",1,1,0,0,1,0,0,NA,NA 

回答

2

如果不在melt指定id變量,二者people和列8被視爲ID,以及在熔融數據的可變列你鬆散X8。然後,您不會在熱圖中繪製變量X8。

使用melt(data,id='people')應該解決它。

enter image description here

+0

非常感謝@NicE! – user3437823 2015-02-17 23:08:49