簡體   English   中英

在R中訪問坐標heatmap.2

[英]access coordinates heatmap.2 in R

我已經用heatmap.2繪制了兩個在[0,1]上定義的變量的圖。 我現在想畫一個abline(v=0.2) 這將被“放錯位置”(相對於我想要的,不是我想要的R的內部logid),因為heatmap.2還會在繪圖區域中放置其他內容,例如顏色鍵(和heatmap.2的軸是,我猜是,內部也有其他事情,因為在下面的示例中我只是重命名了軸)。

我的問題是:我怎樣才能最方便地放置像是abline類的東西,最好是根據實際熱圖的坐標而不是整個繪圖區域。

rm(list=ls())
library(rpart)
library(gplots)
library(RColorBrewer)

N <- 150

# just some data
X1 <- runif(N)
X2 <- runif(N)
y <- as.factor((X1<.2+rnorm(N,sd=0.2))|(X2>.7+rnorm(N,sd=0.1)))   

theData <- data.frame(y,X1,X2)

X1grid <- seq(0,1,by=.02)
X2grid <- seq(0,1,by=.02)
newData <- expand.grid(X2=X2grid,X1=X1grid)

FitSingle <- rpart(y~.,data=theData)
PredictionsSingle <- matrix(as.logical(predict(FitSingle, newdata=newData, type="class"))+0,nrow=length(X2grid))

names <- rep("", length(X2grid)) 
names[seq(1,length(X2grid), length(X2grid)/10)] <- seq(0, .9, by=.1) # not fully checked, but should be OK
rownames(PredictionsSingle) <- names
colnames(PredictionsSingle) <- names

rc <- colorRampPalette(c("darkgreen", "white", "purple3"))(n = nrow(X2grid))
heatmapplotSingle <- heatmap.2(PredictionsSingle, Rowv=NA, Colv=NA, col=rc,density.info="none",trace="none",revC=T)
abline(v=0.2) # should be at 0.2, not at roughly .6. should also stay in the heatmap

如評論中所述,add.expr是解決方法:

heatmapplotSingle <- heatmap.2(PredictionsSingle, Rowv = NA, Colv = NA, col = rc, 
                           density.info = "none", trace = "none", revC = T,
                           main = "Classification Tree full sample", 
                           add.expr  = { segments(0.2*ncol(MeanPrediction), 0, 0.2*ncol(MeanPrediction),
                                                  .7*nrow(MeanPrediction), lwd = 3, col="darkslateblue");
                                         segments(0.2*ncol(MeanPrediction), .7*nrow(MeanPrediction), 1*ncol(MeanPrediction), 
                                                  .7*nrow(MeanPrediction), lwd = 3, col = "darkslateblue")}
                           , srtCol = 0)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM