R: heatmap for triangular matrix
2
4
Entering edit mode
7.0 years ago
Shicheng Guo ★ 9.1k

Hi All,

Is there any suggestion to prepare the heatmap in the following figure with R?  Thanks

By the way, How to control the color in the way as the figure.

<>

R heatmap • 15k views
0
Entering edit mode

Hi I am trying to the get the lower part of the square matrix (, but it didn't turn out to be a perfect triangular matrix, may I know what should I do? Thanks.

3
Entering edit mode
7.0 years ago

This looks like the rotated lower/upper part of a square matrix. You can get the upper part of the matrix with something like

matrix[lower.tri(matrix)] <- NA
heatmap(matrix)

I am not sure how to rotate the image in R but you could always export it and rotate it with the GIMP or Inkscape. If other elements were to be added like in your example figure, I would export the heatmap and do the final figure assembly (including the rotation) in Inkscape.

Edit: You can rotate an image in R with rasterImage(). Something like this might work:

# create empty plot area first, d is the size of the matrix diagonal
plot(NA, type="n", xlim=c(0, d), ylim=c(0, d), xlab="", ylab="", asp=1)
rasterImage(as.raster(matrix),angle=45)
0
Entering edit mode

Hi Jean, Do you know how to control the color as the figures I showed in the question. Thanks

0
Entering edit mode

You could try replacing as.raster(matrix) with an image or a matrix of colors.

1
Entering edit mode
7.0 years ago
Shicheng Guo ★ 9.1k
library("grDevices")
plot(1:20,pch=20,col=col)
col=colorRampPalette(c("white", "red"))(20)
M <- matrix(runif(100),10,10)
M[lower.tri(M)] <- NA
image(M,col = col,frame=F,xaxt="n",yaxt="n")