2014-12-26 2 views
1

내가 txt 파일에서이 데이터를 내가 (검은 색 사각형없이) 히트 맵을 구축플롯은 사각형 4 점 ggplot의 히트 맵에 주어진

"people","1","2","3","4","5","6","7","8","9","10","11","12" 
"Ej1",0,0,0,0,0,0,0,0,0,0,0,0 
"Ej2",0,0,1,1,1,1,1,1,0,0,0,0 
"Ej3",0,0,0,0,0,0,1,0,0,0,0,0 
"Ej4",0,1,1,1,0,0,1,1,0,0,0,1 
"Ej5",1,1,1,1,1,1,1,1,0,0,0,1 
"Ej6",1,1,1,1,0,1,1,1,0,0,1,1 
"Ej7",1,1,1,1,1,1,1,1,0,0,0,0 
"Ej8",0,0,0,1,1,1,0,0,0,0,0,0 
"Ej9",0,0,1,1,1,1,1,1,0,0,0,0 
"Ej10",0,0,0,1,1,1,1,0,0,0,0,0 
"Ej11",0,0,0,0,1,0,0,0,0,1,1,0 
"Ej12",0,0,1,1,1,0,0,0,0,1,1,1 
"Ej13",0,1,1,1,0,0,0,1,1,1,1,1 
"Ej14",1,1,0,0,0,0,0,1,1,1,0,1 
"Ej15",0,0,0,0,0,0,0,1,1,1,1,1 
"Ej16",0,0,0,0,0,0,0,1,1,1,1,1 

(fruits2612e.txt)이 코드

library(reshape2) 
library(ggplot2) 
library(scales) 
library(plyr) 
data <- read.csv("fruits2612e.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("Ej16","Ej15","Ej14","Ej13","Ej12","Ej11","Ej10","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)) 
를 사용하여이

는 지금은 히트 맵에 검은 색 사각형을 추가하기 위해 노력하고있어,하지만 좌표는 어떻게 찾을 수 없습니다

maxR<-c(topLeftx,topLefty,botRightX,botRightY) 
[1] 5 1 7 8 

내 자신의 조언에 따라 93,578,776,는

+0

'annotate' 함수를보고'geom = 'rect''가 해결책을 제시하는지 조사해야합니다. –

+0

도움이 될 수 있습니다 : http://stackoverflow.com/questions/22024641/how-to-add-bounding-box-to-a-specific-area-in-ggplot2-heatmap – KFB

+0

정말 고마워요! – user3437823

답변

2

ggplot는 내부적으로 예를 들면 접근하여 그들의 코드를 사용하여 요소를 조작 as.integer(data.m$people) 등 타일의 모서리는 코드 +/- 0.5입니다. 그래서, 은 x 및 y 방향 모두, 다음이 당신이

maxR  <-c(topLeftx=5,topLefty=1,botRightX=7,botRightY=8) 
sub.data <- with(data.m, 
       with(as.list(maxR), 
         data.m[people %in% paste0("Ej",topLeftx:botRightX) 
          & variable %in% paste0("X",topLefty:botRightY),])) 

p+with(sub.data,annotate(geom="rect", fill="transparent",color="black", size=1.5, 
      xmin=min(as.integer(variable))-0.5,ymin=min(as.integer(people))-0.5, 
      xmax=max(as.integer(variable))+0.5,ymax=max(as.integer(people))+0.5)) 

시작 부분의 고문 코드의 때문에 필요 원하는 상자를 그립니다 것이기 요소를 사용하는 정말 가정 상자의 모서리를 지정하기 위해 선택한 기괴한 방법입니다.

+0

고마워요 !!! – user3437823

4

, 나는 "기하 구조 RECT 주석"에 대한 SO 검색 한 히트 발견 Draw multiple squares with ggplot

maxR<-c(topLeftx=5,topLefty=1,botRightX=7,botRightY=8) 
p + annotate(geom='rect', xmin= maxR['botRightX'], ymin= maxR['botRightY'], 
          xmax=maxR['topLeftx'], ymax= maxR['topLefty'], 
       fill="transparent", col="black", lwd=3) 

그래서 분명히 우리는 사각형의 인덱스를 지정하지만, 당신이해야하는 방법에 대한 다른 개념을 가지고이 여기에서 가져올 수 있습니다.

enter image description here

관련 문제