Skip to content

Commit 99d91d2

Browse files
committed
fix overlapping color breaks
1 parent c269c6f commit 99d91d2

6 files changed

+35
-29
lines changed

heatmaps/h1_simple.R

+24-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ library(RColorBrewer)
88
data <- read.csv("dataset.csv", comment.char="#")
99
rnames <- data[,1] # assign labels in column 1 to "rnames"
1010
mat_data <- data.matrix(data[,2:ncol(data)]) # transform column 2-5 into a matrix
11-
rownames(mat_data) <- rnames # assign row names
11+
rownames(mat_data) <- rnames # assign row names
1212

1313

1414

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

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

2727
# creates a 5 x 5 inch image
28-
png("h1_simple.png",
28+
png("h1_simple.png",
2929
width = 5*300, # 5 x 300 pixels
3030
height = 5*300,
3131
res = 300, # 300 pixels per inch
3232
pointsize = 8) # smaller font size
3333

34-
heatmap.2(mat_data,
34+
heatmap.2(mat_data,
3535
cellnote = mat_data, # same data set for cell labels
3636
main = "Correlation", # heat map title
3737
notecol="black", # change font color of cell labels to black
3838
density.info="none", # turns off density plot inside color legend
3939
trace="none", # turns off trace lines inside the heat map
4040
margins =c(12,9), # widens margins around plot
41-
col=my_palette, # use on color palette defined earlier
41+
col=my_palette, # use on color palette defined earlier
4242
breaks=col_breaks, # enable color transition at specified limits
4343
dendrogram="row", # only draw a row dendrogram
4444
Colv="NA") # turn off column clustering
4545

46+
##############################################################################
47+
# NOTE
48+
##############################################################################
49+
# The color breaks above will yield a warning
50+
# "...unsorted 'breaks' will be sorted before use" since they contain
51+
# (due to the negative numbers). To avoid this warning, you can change the
52+
# manual breaks to:
53+
#
54+
# col_breaks = c(seq(0,1,length=100), # for red
55+
# seq(1.01,1.7,length=100), # for yellow
56+
# seq(1.71,2,length=100)) # for green
57+
#
58+
# However, the problem is then that our heatmap contains negative values
59+
# which will then not be colored correctly. Remember that you don't need to
60+
# provide manual color breaks at all, this is entirely optional.
61+
##############################################################################
62+
4663
dev.off()

heatmaps/h1_simple.png

86.4 KB
Loading

heatmaps/h2_default_clustering.R

+7-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ library(RColorBrewer)
88
data <- read.csv("dataset.csv", comment.char="#")
99
rnames <- data[,1] # assign labels in column 1 to "rnames"
1010
mat_data <- data.matrix(data[,2:ncol(data)]) # transform column 2-5 into a matrix
11-
rownames(mat_data) <- rnames # assign row names
11+
rownames(mat_data) <- rnames # assign row names
1212

1313

1414

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

22-
# (optional) defines the color breaks manually for a "skewed" color transition
23-
col_breaks = c(seq(-1,0,length=100), # for red
24-
seq(0,0.7,length=100), # for yellow
25-
seq(0.7,1,length=100)) # for green
26-
2722
# creates a 5 x 5 inch image
28-
png("h2_default_clustering.png",
23+
png("h2_default_clustering.png",
2924
width = 5*300, # 5 x 300 pixels
3025
height = 5*300,
3126
res = 300, # 300 pixels per inch
@@ -37,19 +32,19 @@ png("h2_default_clustering.png",
3732
# Distance options: euclidean (default), maximum, canberra, binary, minkowski, manhattan
3833
# Cluster options: complete (default), single, average, mcquitty, median, centroid, ward
3934
row_distance = dist(mat_data, method = "manhattan")
40-
row_cluster = hclust(row_distance, method = "ward")
35+
row_cluster = hclust(row_distance, method = "ward.D")
4136
col_distance = dist(t(mat_data), method = "manhattan")
42-
col_cluster = hclust(col_distance, method = "ward")
37+
col_cluster = hclust(col_distance, method = "ward.D")
4338

44-
heatmap.2(mat_data,
39+
heatmap.2(mat_data,
4540
cellnote = mat_data, # same data set for cell labels
4641
main = "Correlation", # heat map title
4742
notecol = "black", # change font color of cell labels to black#
4843
density.info = "none", # turns off density plot inside color legend
4944
trace = "none", # turns off trace lines inside the heat map
5045
margins = c(12,9), # widens margins around plot
51-
col = my_palette, # use on color palette defined earlier
52-
breaks = col_breaks, # enable color transition at specified limits
46+
col = my_palette, # use on color palette defined earlier
5347
Rowv = as.dendrogram(row_cluster), # apply default clustering method
5448
Colv = as.dendrogram(col_cluster)) # apply default clustering method
49+
5550
dev.off()

heatmaps/h2_default_clustering.png

-2.79 KB
Loading

heatmaps/h3_categorizing.R

+4-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ library(RColorBrewer)
88
data <- read.csv("dataset.csv", comment.char="#")
99
rnames <- data[,1] # assign labels in column 1 to "rnames"
1010
mat_data <- data.matrix(data[,2:ncol(data)]) # transform column 2-5 into a matrix
11-
rownames(mat_data) <- rnames # assign row names
11+
rownames(mat_data) <- rnames # assign row names
1212

1313

1414

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

22-
# (optional) defines the color breaks manually for a "skewed" color transition
23-
col_breaks = c(seq(-1,0,length=100), # for red
24-
seq(0,0.7,length=100), # for yellow
25-
seq(0.7,1,length=100)) # for green
26-
2722
# creates a 5 x 5 inch image
28-
png("h3_categorizing.png",
23+
png("h3_categorizing.png",
2924
width = 5*300, # 5 x 300 pixels
3025
height = 5*300,
3126
res = 300, # 300 pixels per inch
3227
pointsize = 8) # smaller font size
3328

34-
heatmap.2(mat_data,
29+
heatmap.2(mat_data,
3530
cellnote = mat_data, # same data set for cell labels
3631
RowSideColors = c( # grouping row-variables into different
3732
rep("gray", 3), # categories, Measurement 1-3: green
@@ -43,7 +38,6 @@ heatmap.2(mat_data,
4338
trace="none", # turns off trace lines inside the heat map
4439
margins =c(12,9), # widens margins around plot
4540
col=my_palette, # use on color palette defined earlier
46-
breaks=col_breaks, # enable color transition at specified limits
4741
dendrogram="row", # only draw a row dendrogram
4842
Colv="NA") # turn off column clustering
4943

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

5953
dev.off()

heatmaps/h3_categorizing.png

-5.38 KB
Loading

0 commit comments

Comments
 (0)