Here a simple and stupid approach using base graphics' rasterImage
:
plotTriMatrix <- function(x) {
## clear lower triangle
x[lower.tri(x)] <- NA
## calculate diag
nr <- nrow(x)
nc <- ncol(x)
d <- sqrt(nr^2 + nc^2)
d2 <- 0.5 * d
## empty plot area
plot(NA, type="n", xlim=c(0, d), ylim=c(0, d), xlab="", ylab="", asp=1)
## plot matrix and rotate 45
rasterImage(as.raster(x),
xleft=d2, xright=d2+nc, ybottom=-d2, ytop=-d2+nr,
interpolate=FALSE, angle=45)
}
Example:
set.seed(123)
m <- matrix(runif(100), 10, 10)
plotTriMatrix(m)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…