This is a test version of Biostars. For the public version, visit https://www.biostars.org.
R: heatmap for triangular matrix

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.

figure

heatmap r

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.

2 answers

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)

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

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

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")

Log in to answer this question.