[英]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.