public
Last active

ggplot2 heatmap with "spectral" palette

  • Download Gist
Heatmap.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
# Simple ggplot2 heatmap
# with colorBrewer "spectral" palette
 
doInstall <- TRUE # 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)
 
# Generate a random matrix
# This can be any type of numeric matrix,
# though we often see heatmaps of square correlation matrices.
nRow <- 9
nCol <- 16
 
myData <- matrix(rnorm(nRow * nCol), ncol = nCol)
rownames(myData) <- letters[1:nRow]
colnames(myData) <- LETTERS[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$X1 <- factor(longData$X1, names(sort(with(longData, by(value, X1, mean)))))
longData$X2 <- factor(longData$X2, names(sort(with(longData, by(value, X2, mean)))))
 
# Define palette
myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")), space="Lab")
 
zp1 <- ggplot(longData,
aes(x = X2, y = X1, fill = value))
zp1 <- zp1 + geom_tile()
zp1 <- zp1 + scale_fill_gradientn(colours = myPalette(100))
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

It should be:
longData$X1 <- factor(longData$Var1, names(sort(with(longData, by(value, Var1, mean)))))
longData$X2 <- factor(longData$Var2, names(sort(with(longData, by(value, Var2, mean)))))

Instead of:
longData$X1 <- factor(longData$X1, names(sort(with(longData, by(value, X1, mean)))))
longData$X2 <- factor(longData$X2, names(sort(with(longData, by(value, X2, mean)))))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.