1

Assume a matrix m of integer values:

m <- matrix(sample(1:10, 100, replace = TRUE), nrow = 10)

Given a colour palette that maps those values from 1 to 10 to some colours, how to show matrix m as a heatmap in R with OpenGL graphics, e.g. using the rgl package? (Preferably in the most efficient way.)

user2554330
  • 30,741
  • 4
  • 34
  • 76
plant
  • 2,782
  • 14
  • 28

1 Answers1

1

The very thorough answer here suggests this may not be what you want; you might want to try the solution below against the other solutions benchmarked there. Nonetheless:

Set up data and colour map

set.seed(101)
library(viridisLite)
vv <- viridis(10)
m <- matrix(sample(1:10, 100, replace = TRUE), nrow = 10)

Draw the picture:

library(rgl)
view3d(theta=0, phi=0)  ## head-on view
par3d(zoom=0.7)         ## (almost) fill window
surface3d(x = 1:10, y = 1:10, z = matrix(0, 10,10), 
          color = vv[m],
          smooth=FALSE, lit=FALSE  ## turn off smoothing/lights
 )

You may need to use pop3d() between surfaces to clear the previous surface ...

Ben Bolker
  • 192,494
  • 24
  • 350
  • 426