Skip to content

Instantly share code, notes, and snippets.

@bluepnume
Created July 24, 2015 06:11
Show Gist options
  • Select an option

  • Save bluepnume/2721a913902691ae3701 to your computer and use it in GitHub Desktop.

Select an option

Save bluepnume/2721a913902691ae3701 to your computer and use it in GitHub Desktop.
#########################################################
### A) Installing and loading required packages
#########################################################
if (!require("gplots")) {
install.packages("gplots", dependencies = TRUE, repos = "http://cran.us.r-project.org")
library(gplots)
}
if (!require("RColorBrewer")) {
install.packages("RColorBrewer", dependencies = TRUE, repos = "http://cran.us.r-project.org")
library(RColorBrewer)
}
#########################################################
### B) Reading in data and transform it into matrix format
#########################################################
data <- read.csv("data.csv", comment.char="#")
rnames <- data[,1] # assign labels in column 1 to "rnames"
mat_data <- data.matrix(data[,2:ncol(data)]) # transform column 2-5 into a matrix
rownames(mat_data) <- rnames # assign row names
#########################################################
### C) Customizing and plotting the heat map
#########################################################
# creates a own color palette from red to green
my_palette <- colorRampPalette(c("blue", "white", "red"))(n = 299)
# creates a 5 x 5 inch image
png("heatmap.png", # create PNG for the heat map
width = 5*300, # 5 x 300 pixels
height = 5*300,
res = 300, # 300 pixels per inch
pointsize = 8) # smaller font size
heatmap.2(mat_data,
cellnote = mat_data, # same data set for cell labels
main = "Correlation", # heat map title
notecol="black", # change font color of cell labels to black
density.info="none", # turns off density plot inside color legend
trace="none", # turns off trace lines inside the heat map
margins =c(12,9), # widens margins around plot
col=my_palette, # use on color palette defined earlier
dendrogram="none", # only draw a row dendrogram
Colv="NA") # turn off column clustering
dev.off() # close the PNG device
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment