Skip to content

Instantly share code, notes, and snippets.

@bmiles
Last active August 29, 2015 14:04
Show Gist options
  • Save bmiles/bd2599eba38ce216c5d4 to your computer and use it in GitHub Desktop.
Save bmiles/bd2599eba38ce216c5d4 to your computer and use it in GitHub Desktop.
R function to rebin a matrix by taking averages, then plots the heatmap
# Simple ggplot2 heatmap
# with colorBrewer "spectral" palette
doInstall <- FALSE # Change to FALSE if you don't want packages installed.
toInstall <- c("ggplot2", "reshape2", "RColorBrewer")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
require('rje')
require('reshape2')
# Generate a random matrix
# This can be any type of numeric matrix,
# though we often see heatmaps of square correlation matrices.
rebin <- function(myData, binW, binL) {
# Set up starting matrix
matDims <- dim(myData)
rownames(myData) <- c(1:matDims[1])
colnames(myData) <- c(1:matDims[2])
# Begin rebinned matrix
b <- myData
print(dim(b))
# Check and change matrix length to be a multple of the bin factors
if (matDims[1] %% binL > 0) {
x <- floor(matDims[1] / binL)
lengthDiff <- matDims[1] - x * binL
delRange <- ((matDims[1]+1)-lengthDiff):matDims[1]
b <- b[-(delRange),]
}
print(dim(b))
#get the new dimenions
matDims <- dim(myData)
# Get the Means of the matrix reshaped to the bin factor
b <- colMeans(matrix(b, nrow=binL),na.rm=TRUE)
# reshape matrix and transpose back
b <- matrix(b, nrow=matDims[1]/binL)
matDims <- dim(myData)
# Check and change matrix width to be a multple of the bin factors
if (matDims[2] %% binW > 0) {
x <- floor(matDims[2] / binW)
lengthDiff <- matDims[2] - x * binW
delRange <- ((matDims[2]+1)-lengthDiff):matDims[1]
b <- b[,-(delRange)]
}
#get the new dimenions
matDims <- dim(myData)
# Get the Means of the matrix reshaped to the bin factor
b <- rowMeans(matrix(b, ncol=binW), na.rm=TRUE)
# reshape matrix and transpose back
b <- matrix(b, ncol=matDims[2]/binW)
return(b)
}
heatmap <- function(myData, binSize){
nRow <- dim(myData)[1]
nCol <- dim(myData)[2]
#myData <- matrix(mapfile, ncol = nCol)
rownames(myData) <- c(1:nRow)
colnames(myData) <- c(1:nCol)
# # Replace with numbers that actually have a relationship:
# for(ii in 2:ncol(myData)){ myData[, ii] <- myData[, ii-1] + rnorm(nrow(myData)) }
# for(ii in 2:nrow(myData)){ myData[ii, ] <- myData[ii-1, ] + rnorm(ncol(myData)) }
# For melt() to work seamlessly, myData has to be a matrix.
longData <- melt(myData)
head(longData, 20)
# Optionally, reorder both the row and column variables in any order
# Here, they are sorted by mean value
# longData$Var1 <- factor(longData$Var1, names(sort(with(longData, by(value, Var1, mean)))))
# longData$Var2 <- factor(longData$Var2, names(sort(with(longData, by(value, Var2, mean)))))
# Define palette
#myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")), space="Lab")
myPalette <- cubeHelix(100, start=0.5,r=-1.5,hue=2, gamma=1 )
zp1 <- ggplot(longData,
aes(x = Var2, y = Var1, fill = value))
zp1 <- zp1 + geom_tile()
zp1 <- zp1 + scale_fill_gradientn(colours = myPalette)
zp1 <- zp1 + scale_x_discrete(expand = c(0, 0))
zp1 <- zp1 + scale_y_discrete(expand = c(0, 0))
zp1 <- zp1 + coord_equal()
zp1 <- zp1 + theme_bw()
print(zp1) # Your plot will look different, depending on the seed
}
myData <- as.matrix(read.csv("map data.csv"))
mat <- matrix(c(1:262144),nrow=512,ncol=512)
heatmap(rebin(myData,2,2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment