-
-
Save accaldwell/4027017 to your computer and use it in GitHub Desktop.
ggplot2 heatmap with "spectral" palette
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
heatmapdata <- read.csv("~/Subsystem_by_Phylum-absolute.csv", sep="\t", check.names=FALSE) | |
row.names(heatmapdata) <- heatmapdata$Phylum | |
hmdata <- heatmapdata[,2:29] | |
myData <- data.matrix(hmdata) | |
# 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$Phylum <- factor(longData$X1, names(sort(with(longData, by(value, X1, mean))))) | |
longData$Subsystem <- factor(longData$X2, names(sort(with(longData, by(value, X2, mean))))) | |
# Define palette | |
myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral"))) | |
zp1 <- ggplot(longData,aes(x = Subsystem, y = Phylum, 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() | |
zp1 <- zp1 + theme(axis.text.x=element_text(angle=-90, hjust=0)) | |
print(zp1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment