我在R中有以下代码来创建一个简单的热图。
data <- c(rnorm(80), rep(NA,20))
image(matrix(sample(data),nrow=50, ncol=2), las=1)
我想做以下两件事: -
NA
s具有对角红色(或黑色)线。 我如何实现(1)和(2)?
答案 0 :(得分:3)
这应该是关闭的:
# fix the data because we're going to use it twice:
d=sample(data)
# plot it with X and Y coords known (note 1 larger than dimension)
image(1:51,1:3,matrix(d,nrow=50, ncol=2),las=1)
# add diagonal shading
polygon(x=c(1,51,51,1),y=c(1,1,3,3),density=3,col="red")
# replot. The NA's show through:
image(1:51,1:3,matrix(d,nrow=50, ncol=2),las=1,add=TRUE)
# outline the cells.
abline(h=1:3)
abline(v=1:51)
如果你真的想在每个方框中加一个红色斜杠:
x0=c(1:50,1:50)
x1=x0+1
y0=c(rep(1,50),rep(2,50))
y1=y0+1
image(1:51,1:3,matrix(d,nrow=50, ncol=2),las=1)
segments(x0,y0,x1,y1,col="red",lwd=2)
image(1:51,1:3,matrix(d,nrow=50, ncol=2),las=1,add=TRUE)
abline(v=1:51);abline(h=1:3)