Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

r - Visualising and rotating a matrix

I am trying to visualise an upper triangular matrix that is approximately 500x500. Alongside this I am also trying to rotate that image so that it looks like the triangle is pointing upward:

enter image description here

(This was achieved by taking a snapshot of a graphics device and then rotating that image).

As in that image each column and row needs to have it's width specified.

I have tried using the image() function with the grid package (using a viewpanel that is rotated by 45 degrees) however that does not work. Does anybody know a better solution?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...