Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 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 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...