Skip to content

Commit

Permalink
fix overlapping color breaks
Browse files Browse the repository at this point in the history
  • Loading branch information
rasbt committed Aug 3, 2016
1 parent c269c6f commit 99d91d2
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 29 deletions.
31 changes: 24 additions & 7 deletions heatmaps/h1_simple.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ library(RColorBrewer)
data <- read.csv("dataset.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
rownames(mat_data) <- rnames # assign row names



Expand All @@ -20,27 +20,44 @@ rownames(mat_data) <- rnames # assign row names
my_palette <- colorRampPalette(c("red", "yellow", "green"))(n = 299)

# (optional) defines the color breaks manually for a "skewed" color transition
col_breaks = c(seq(-1,0,length=100), # for red
seq(0,0.7,length=100), # for yellow
seq(0.7,1,length=100)) # for green
col_breaks = c(seq(-1,0,length=100), # for red
seq(0.01,0.7,length=100), # for yellow
seq(0.71,1,length=100)) # for green

# creates a 5 x 5 inch image
png("h1_simple.png",
png("h1_simple.png",
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,
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
col=my_palette, # use on color palette defined earlier
breaks=col_breaks, # enable color transition at specified limits
dendrogram="row", # only draw a row dendrogram
Colv="NA") # turn off column clustering

##############################################################################
# NOTE
##############################################################################
# The color breaks above will yield a warning
# "...unsorted 'breaks' will be sorted before use" since they contain
# (due to the negative numbers). To avoid this warning, you can change the
# manual breaks to:
#
# col_breaks = c(seq(0,1,length=100), # for red
# seq(1.01,1.7,length=100), # for yellow
# seq(1.71,2,length=100)) # for green
#
# However, the problem is then that our heatmap contains negative values
# which will then not be colored correctly. Remember that you don't need to
# provide manual color breaks at all, this is entirely optional.
##############################################################################

dev.off()
Binary file modified heatmaps/h1_simple.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 7 additions & 12 deletions heatmaps/h2_default_clustering.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ library(RColorBrewer)
data <- read.csv("dataset.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
rownames(mat_data) <- rnames # assign row names



Expand All @@ -19,13 +19,8 @@ rownames(mat_data) <- rnames # assign row names
# creates a own color palette from red to green
my_palette <- colorRampPalette(c("red", "yellow", "green"))(n = 299)

# (optional) defines the color breaks manually for a "skewed" color transition
col_breaks = c(seq(-1,0,length=100), # for red
seq(0,0.7,length=100), # for yellow
seq(0.7,1,length=100)) # for green

# creates a 5 x 5 inch image
png("h2_default_clustering.png",
png("h2_default_clustering.png",
width = 5*300, # 5 x 300 pixels
height = 5*300,
res = 300, # 300 pixels per inch
Expand All @@ -37,19 +32,19 @@ png("h2_default_clustering.png",
# Distance options: euclidean (default), maximum, canberra, binary, minkowski, manhattan
# Cluster options: complete (default), single, average, mcquitty, median, centroid, ward
row_distance = dist(mat_data, method = "manhattan")
row_cluster = hclust(row_distance, method = "ward")
row_cluster = hclust(row_distance, method = "ward.D")
col_distance = dist(t(mat_data), method = "manhattan")
col_cluster = hclust(col_distance, method = "ward")
col_cluster = hclust(col_distance, method = "ward.D")

heatmap.2(mat_data,
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
breaks = col_breaks, # enable color transition at specified limits
col = my_palette, # use on color palette defined earlier
Rowv = as.dendrogram(row_cluster), # apply default clustering method
Colv = as.dendrogram(col_cluster)) # apply default clustering method

dev.off()
Binary file modified heatmaps/h2_default_clustering.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 4 additions & 10 deletions heatmaps/h3_categorizing.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ library(RColorBrewer)
data <- read.csv("dataset.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
rownames(mat_data) <- rnames # assign row names



Expand All @@ -19,19 +19,14 @@ rownames(mat_data) <- rnames # assign row names
# creates a own color palette from red to green
my_palette <- colorRampPalette(c("red", "yellow", "green"))(n = 299)

# (optional) defines the color breaks manually for a "skewed" color transition
col_breaks = c(seq(-1,0,length=100), # for red
seq(0,0.7,length=100), # for yellow
seq(0.7,1,length=100)) # for green

# creates a 5 x 5 inch image
png("h3_categorizing.png",
png("h3_categorizing.png",
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,
heatmap.2(mat_data,
cellnote = mat_data, # same data set for cell labels
RowSideColors = c( # grouping row-variables into different
rep("gray", 3), # categories, Measurement 1-3: green
Expand All @@ -43,7 +38,6 @@ heatmap.2(mat_data,
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
breaks=col_breaks, # enable color transition at specified limits
dendrogram="row", # only draw a row dendrogram
Colv="NA") # turn off column clustering

Expand All @@ -54,6 +48,6 @@ legend("topright", # location of the legend on the heatmap plot
col = c("gray", "blue", "black"), # color key
lty = 1, # line style
lwd = 10, # line width
)
)

dev.off()
Binary file modified heatmaps/h3_categorizing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 99d91d2

Please sign in to comment.