2

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?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Jan
  • 105
  • 2
  • 5
  • There is no `image` function in package 'grid'. Perhaps if you took a 10 x 10 section of that data and provided code that was being used we could help. – IRTFM Sep 02 '13 at 16:24
  • Not too sure what the data is that you are trying to visualise but perhaps maybe look at the concept of an arc diagram? http://gastonsanchez.wordpress.com/2013/02/03/arc-diagrams-in-r-les-miserables/ – h.l.m Sep 02 '13 at 17:00
  • Apologies @DWin, I worded that badly. I meant that I use the `image()` function form the base graphics and then use grid to take a snapshot of the graphics device and then rotate and plot that using `grid`. @h.l.m, I am aware of those diagrams, however it would be preferable to display the data in this way. – Jan Sep 02 '13 at 17:37

1 Answers1

4

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

sgibb
  • 25,396
  • 3
  • 68
  • 74
  • 1
    That example works quite well, cheers. However the width of each row and column needs to be adjustable. As far as I can tell, your example only creates a bitmap out of the matrix. – Jan Sep 02 '13 at 17:31
  • 1
    Well, since the `image` function "only creates a bitmap", I think sgibb is well within his rights to assume that is what you wanted. – IRTFM Sep 02 '13 at 17:58
  • Actually I see what @Jan is saying, the example will only look correct with very specific dimensions of the plot device, much unlike `image`. (The `plot` command needs `asp=1`.) – blmoore Feb 04 '14 at 16:54