绘制热图,包括NA

时间:2015-02-17 22:40:47

标签: r plot

我正在绘制以下csv文件的热图:

"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

1 个答案:

答案 0 :(得分:2)

如果您未在id中指定melt变量,则people和列8都将被视为ID,并且您在变量列中放宽了X8熔化的数据。然后,您不会在热图中绘制变量X8。

使用melt(data,id='people')应该解决它。

enter image description here