histelheim
histelheim

Reputation: 5098

Scaling heatmaps of different sizes

I have several square matrices containing correlations, but of very different sizes (ranging from 300 rows/columns, to 3000 rows/columns). For example, two of them could be:

small_matrix <- replicate(10, rnorm(10)) 
large_matrix <- replicate(100, rnorm(100)) 

However, if you make a heatmap of these, they will be difficult to compare:

heatmap(small_matrix, Rowv = NA, Colv = NA, labRow = NA, labCol = NA)
heatmap(large_matrix, Rowv = NA, Colv = NA, labRow = NA, labCol = NA)

I want to make the visualizations comparable, at least on an "impressionistic" level. Therefore, I want to rescale them, so that they are all, say 10 rows/columns large. That way you can at least get make a visual, impressionistic, comparison of the matrices.

Probably this would entail somehow dividing each matrix in 10x10 submatrices, and then taking the average of each submatrix.

What would be a good approach to doing this?

Upvotes: 2

Views: 146

Answers (2)

histelheim
histelheim

Reputation: 5098

Another approach is to use the raster library:

library(raster)
library(RColorBrewer)

large_matrix <- replicate(100, rnorm(100)) 
matrix_rasterized <- raster(large_matrix)

extent(matrix_rasterized) <- extent(c(-180, 180, -90, 90))
size <- raster(nrow=10, ncol=10)
matrix_resampled <- resample(matrix_rasterized, size)
matrix_rescaled <- as.matrix(matrix_resampled)

greyscale <- brewer.pal(9, "Greys")

heatmap(matrix_rescaled, labRow = NA, labCol = NA, 
main = "Large matrix - Rescaled", col = greyscale)

enter image description here

Upvotes: 1

Pigeon
Pigeon

Reputation: 423

You can do some interpolating(but it will be weaker as taking averages) using ggplot2 (also you need reshape, for melt function)

p1 <- qplot(X1, X2, data = melt(small_matrix), fill = value, geom = "raster")
p2 <- qplot(X1, X2, data = melt(large_matrix), fill = value, geom = "raster")
p3 <- qplot(X1, X2, data = melt(small_matrix), fill = value, geom = "raster",interpolate = TRUE)
p4 <- qplot(X1, X2, data = melt(large_matrix), fill = value, geom = "raster",interpolate = TRUE)

p <- grid.arrange(p1,p2,p3,p4)

enter image description here

Upvotes: 3

Related Questions