热图中的ggplot2值范围

时间:2014-08-23 14:27:33

标签: r ggplot2

要在热图中绘制一系列值,geom_point(子集)应该如此处所示

How to draw only a range of values in geom_point from the ggplot2 package?

但我正在尝试将其用于此数据

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

限制数据的代码应与“people”exe的类似,但我遗漏了一些内容:

geom_point(data=subset(people >= "Ej3" & people <= "Ej7"))

enter image description here

1 个答案:

答案 0 :(得分:1)

就像SimonG说的那样,你的设置不正确。

ggplot(subset(data.m,people %in% c("Ej7","Ej6","Ej5","Ej4","Ej3") & variable %in% c("X5","X6", "X7","X8")), 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))

生成此图,忽略scale_y_discrete

enter image description here