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

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 • 19k views
ADD COMMENT
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.

ADD REPLY
3
Entering edit mode
9.3 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)
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode
9.3 years ago
Shicheng Guo ★ 9.5k
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")
ADD COMMENT

Login before adding your answer.

Traffic: 1983 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6