我为heatmap.2
绘制了一个图,用于在[0,1]上定义的两个变量。我现在想画一个abline(v=0.2)
。这将是错误的" (相对于我想要的,而不是R的内部logid,我推测)因为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
答案 0 :(得分:0)
如评论中所述,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)