Skip to content

Commit

Permalink
Added angle_col parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Raivo Kolde committed Dec 26, 2018
1 parent 48bad0d commit 50ee175
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 22 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
@@ -1,8 +1,8 @@
Package: pheatmap
Type: Package
Title: Pretty Heatmaps
Version: 1.0.11
Date: 2018-05-18
Version: 1.0.12
Date: 2018-12-26
Author: Raivo Kolde
Maintainer: Raivo Kolde <rkolde@gmail.com>
Depends:
Expand Down
5 changes: 4 additions & 1 deletion NEWS
@@ -1,9 +1,12 @@
Version 1.0.12
-------------------------------------------------------------------------------
* New option angle_col that allows to change the orientation of column labels

Version 1.0.11
-------------------------------------------------------------------------------
* Now values outside of provided breaks correspond to highese/lowest supplied
color (thanks Manuela Hummel)


Version 1.0.10
-------------------------------------------------------------------------------
* Fixed the blank page issue for RMarkdown notebooks and printing heatmaps to
Expand Down
62 changes: 47 additions & 15 deletions R/pheatmap.r
@@ -1,4 +1,4 @@
lo = function(rown, coln, nrow, ncol, cellheight = NA, cellwidth = NA, treeheight_col, treeheight_row, legend, annotation_row, annotation_col, annotation_colors, annotation_legend, annotation_names_row, annotation_names_col, main, fontsize, fontsize_row, fontsize_col, gaps_row, gaps_col, ...){
lo = function(rown, coln, nrow, ncol, cellheight = NA, cellwidth = NA, treeheight_col, treeheight_row, legend, annotation_row, annotation_col, annotation_colors, annotation_legend, annotation_names_row, annotation_names_col, main, fontsize, fontsize_row , fontsize_col, angle_col, gaps_row, gaps_col, ...){
# Get height of colnames and length of rownames
if(!is.null(coln[1]) | (!is.na2(annotation_row) & annotation_names_row)){
if(!is.null(coln[1])){
Expand All @@ -13,7 +13,7 @@ lo = function(rown, coln, nrow, ncol, cellheight = NA, cellwidth = NA, treeheigh
}
longest_coln = which.max(tw)
gp = list(fontsize = ifelse(longest_coln <= length(coln), fontsize_col, fontsize), ...)
coln_height = unit(1, "grobheight", textGrob(t[longest_coln], rot = 90, gp = do.call(gpar, gp))) + unit(10, "bigpts")
coln_height = unit(1, "grobheight", textGrob(t[longest_coln], rot = angle_col, gp = do.call(gpar, gp))) + unit(10, "bigpts")
}
else{
coln_height = unit(5, "bigpts")
Expand All @@ -28,7 +28,7 @@ lo = function(rown, coln, nrow, ncol, cellheight = NA, cellwidth = NA, treeheigh
}
longest_rown = which.max(tw)
gp = list(fontsize = ifelse(longest_rown <= length(rown), fontsize_row, fontsize), ...)
rown_width = unit(1, "grobwidth", textGrob(t[longest_rown], gp = do.call(gpar, gp))) + unit(10, "bigpts")
rown_width = unit(1, "grobwidth", textGrob(t[longest_rown], rot = 0, gp = do.call(gpar, gp))) + unit(10, "bigpts")
}
else{
rown_width = unit(5, "bigpts")
Expand Down Expand Up @@ -218,11 +218,11 @@ draw_matrix = function(matrix, border_color, gaps_rows, gaps_cols, fmat, fontsiz
return(res)
}

draw_colnames = function(coln, gaps, ...){
draw_colnames = function(coln, gaps, vjust_col, hjust_col, angle_col, ...){
coord = find_coordinates(length(coln), gaps)
x = coord$coord - 0.5 * coord$size

res = textGrob(coln, x = x, y = unit(1, "npc") - unit(3, "bigpts"), vjust = 0.5, hjust = 0, rot = 270, gp = gpar(...))
res = textGrob(coln, x = x, y = unit(1, "npc") - unit(3, "bigpts"), vjust = vjust_col, hjust = hjust_col, rot = angle_col, gp = gpar(...))

return(res)
}
Expand Down Expand Up @@ -307,7 +307,7 @@ draw_annotations = function(converted_annotations, border_color, gaps, fontsize,
return(res)
}

draw_annotation_names = function(annotations, fontsize, horizontal){
draw_annotation_names = function(annotations, fontsize, horizontal, hjust_col, vjust_col, angle_col){
n = ncol(annotations)

x = unit(3, "bigpts")
Expand All @@ -323,7 +323,7 @@ draw_annotation_names = function(annotations, fontsize, horizontal){
x = unit(1, "npc") - y
y = unit(1, "npc") - a

res = textGrob(colnames(annotations), x = x, y = y, vjust = 0.5, hjust = 0, rot = 270, gp = gpar(fontsize = fontsize, fontface = 2))
res = textGrob(colnames(annotations), x = x, y = y, vjust = vjust_col, hjust = hjust_col, rot = angle_col, gp = gpar(fontsize = fontsize, fontface = 2))
}

return(res)
Expand Down Expand Up @@ -378,9 +378,9 @@ vplayout = function(x, y){
return(viewport(layout.pos.row = x, layout.pos.col = y))
}

heatmap_motor = function(matrix, border_color, cellwidth, cellheight, tree_col, tree_row, treeheight_col, treeheight_row, filename, width, height, breaks, color, legend, annotation_row, annotation_col, annotation_colors, annotation_legend, annotation_names_row, annotation_names_col, main, fontsize, fontsize_row, fontsize_col, fmat, fontsize_number, number_color, gaps_col, gaps_row, labels_row, labels_col, ...){
heatmap_motor = function(matrix, border_color, cellwidth, cellheight, tree_col, tree_row, treeheight_col, treeheight_row, filename, width, height, breaks, color, legend, annotation_row, annotation_col, annotation_colors, annotation_legend, annotation_names_row, annotation_names_col, main, fontsize, fontsize_row, fontsize_col, hjust_col, vjust_col, angle_col, fmat, fontsize_number, number_color, gaps_col, gaps_row, labels_row, labels_col, ...){
# Set layout
lo = lo(coln = labels_col, rown = labels_row, nrow = nrow(matrix), ncol = ncol(matrix), cellwidth = cellwidth, cellheight = cellheight, treeheight_col = treeheight_col, treeheight_row = treeheight_row, legend = legend, annotation_col = annotation_col, annotation_row = annotation_row, annotation_colors = annotation_colors, annotation_legend = annotation_legend, annotation_names_row = annotation_names_row, annotation_names_col = annotation_names_col, main = main, fontsize = fontsize, fontsize_row = fontsize_row, fontsize_col = fontsize_col, gaps_row = gaps_row, gaps_col = gaps_col, ...)
lo = lo(coln = labels_col, rown = labels_row, nrow = nrow(matrix), ncol = ncol(matrix), cellwidth = cellwidth, cellheight = cellheight, treeheight_col = treeheight_col, treeheight_row = treeheight_row, legend = legend, annotation_col = annotation_col, annotation_row = annotation_row, annotation_colors = annotation_colors, annotation_legend = annotation_legend, annotation_names_row = annotation_names_row, annotation_names_col = annotation_names_col, main = main, fontsize = fontsize, fontsize_row = fontsize_row, fontsize_col = fontsize_col, angle_col = angle_col, gaps_row = gaps_row, gaps_col = gaps_col, ...)

res = lo$gt
mindim = lo$mindim
Expand Down Expand Up @@ -413,7 +413,7 @@ heatmap_motor = function(matrix, border_color, cellwidth, cellheight, tree_col,
# gt = heatmap_motor(matrix, cellwidth = cellwidth, cellheight = cellheight, border_color = border_color, tree_col = tree_col, tree_row = tree_row, treeheight_col = treeheight_col, treeheight_row = treeheight_row, breaks = breaks, color = color, legend = legend, annotation_col = annotation_col, annotation_row = annotation_row, annotation_colors = annotation_colors, annotation_legend = annotation_legend, filename = NA, main = main, fontsize = fontsize, fontsize_row = fontsize_row, fontsize_col = fontsize_col, fmat = fmat, fontsize_number = fontsize_number, number_color = number_color, labels_row = labels_row, labels_col = labels_col, gaps_col = gaps_col, gaps_row = gaps_row, ...)

f(filename, height = height, width = width)
gt = heatmap_motor(matrix, cellwidth = cellwidth, cellheight = cellheight, border_color = border_color, tree_col = tree_col, tree_row = tree_row, treeheight_col = treeheight_col, treeheight_row = treeheight_row, breaks = breaks, color = color, legend = legend, annotation_col = annotation_col, annotation_row = annotation_row, annotation_colors = annotation_colors, annotation_legend = annotation_legend, annotation_names_row = annotation_names_row, annotation_names_col = annotation_names_col, filename = NA, main = main, fontsize = fontsize, fontsize_row = fontsize_row, fontsize_col = fontsize_col, fmat = fmat, fontsize_number = fontsize_number, number_color = number_color, labels_row = labels_row, labels_col = labels_col, gaps_col = gaps_col, gaps_row = gaps_row, ...)
gt = heatmap_motor(matrix, cellwidth = cellwidth, cellheight = cellheight, border_color = border_color, tree_col = tree_col, tree_row = tree_row, treeheight_col = treeheight_col, treeheight_row = treeheight_row, breaks = breaks, color = color, legend = legend, annotation_col = annotation_col, annotation_row = annotation_row, annotation_colors = annotation_colors, annotation_legend = annotation_legend, annotation_names_row = annotation_names_row, annotation_names_col = annotation_names_col, filename = NA, main = main, fontsize = fontsize, fontsize_row = fontsize_row, fontsize_col = fontsize_col, hjust_col = hjust_col, vjust_col = vjust_col, angle_col = angle_col, fmat = fmat, fontsize_number = fontsize_number, number_color = number_color, labels_row = labels_row, labels_col = labels_col, gaps_col = gaps_col, gaps_row = gaps_row, ...)
grid.draw(gt)
dev.off()

Expand Down Expand Up @@ -447,7 +447,7 @@ heatmap_motor = function(matrix, border_color, cellwidth, cellheight, tree_col,

# Draw colnames
if(length(labels_col) != 0){
pars = list(labels_col, gaps = gaps_col, fontsize = fontsize_col, ...)
pars = list(labels_col, gaps = gaps_col, fontsize = fontsize_col, hjust_col = hjust_col, vjust_col = vjust_col, angle_col = angle_col, ...)
elem = do.call(draw_colnames, pars)
res = gtable_add_grob(res, elem, t = 5, l = 3, clip = "off", name = "col_names")
}
Expand Down Expand Up @@ -482,7 +482,7 @@ heatmap_motor = function(matrix, border_color, cellwidth, cellheight, tree_col,

# Draw names
if(annotation_names_row){
elem = draw_annotation_names(annotation_row, fontsize, horizontal = F)
elem = draw_annotation_names(annotation_row, fontsize, horizontal = F, hjust_col = hjust_col, vjust_col = vjust_col, angle_col = angle_col)
res = gtable_add_grob(res, elem, t = 5, l = 2, clip = "off", name = "row_annotation_names")
}
}
Expand Down Expand Up @@ -521,7 +521,7 @@ generate_breaks = function(x, n, center = F){
return(res)
}

scale_vec_colours <- function (x, col = rainbow(10), breaks = NA, na_col){
scale_vec_colours <- function(x, col = rainbow(10), breaks = NA, na_col){
res <- col[as.numeric(cut(x, breaks = breaks, include.lowest = T))]
res[is.na(res)] <- na_col
return(res)
Expand Down Expand Up @@ -748,6 +748,8 @@ identity2 = function(x, ...){
#' @param fontsize base fontsize for the plot
#' @param fontsize_row fontsize for rownames (Default: fontsize)
#' @param fontsize_col fontsize for colnames (Default: fontsize)
#' @param angle_col angle of the column labels, right now one can choose only from few
#' predefined options (0, 45, 90, 270 and 315)
#' @param display_numbers logical determining if the numeric values are also printed to
#' the cells. If this is a matrix (with same dimensions as original matrix), the contents
#' of the matrix are shown instead of original values.
Expand Down Expand Up @@ -877,7 +879,7 @@ identity2 = function(x, ...){
#' }
#'
#' @export
pheatmap = function(mat, color = colorRampPalette(rev(brewer.pal(n = 7, name = "RdYlBu")))(100), kmeans_k = NA, breaks = NA, border_color = "grey60", cellwidth = NA, cellheight = NA, scale = "none", cluster_rows = TRUE, cluster_cols = TRUE, clustering_distance_rows = "euclidean", clustering_distance_cols = "euclidean", clustering_method = "complete", clustering_callback = identity2, cutree_rows = NA, cutree_cols = NA, treeheight_row = ifelse((class(cluster_rows) == "hclust") || cluster_rows, 50, 0), treeheight_col = ifelse((class(cluster_cols) == "hclust") || cluster_cols, 50, 0), legend = TRUE, legend_breaks = NA, legend_labels = NA, annotation_row = NA, annotation_col = NA, annotation = NA, annotation_colors = NA, annotation_legend = TRUE, annotation_names_row = TRUE, annotation_names_col = TRUE, drop_levels = TRUE, show_rownames = T, show_colnames = T, main = NA, fontsize = 10, fontsize_row = fontsize, fontsize_col = fontsize, display_numbers = F, number_format = "%.2f", number_color = "grey30", fontsize_number = 0.8 * fontsize, gaps_row = NULL, gaps_col = NULL, labels_row = NULL, labels_col = NULL, filename = NA, width = NA, height = NA, silent = FALSE, na_col = "#DDDDDD", ...){
pheatmap = function(mat, color = colorRampPalette(rev(brewer.pal(n = 7, name = "RdYlBu")))(100), kmeans_k = NA, breaks = NA, border_color = "grey60", cellwidth = NA, cellheight = NA, scale = "none", cluster_rows = TRUE, cluster_cols = TRUE, clustering_distance_rows = "euclidean", clustering_distance_cols = "euclidean", clustering_method = "complete", clustering_callback = identity2, cutree_rows = NA, cutree_cols = NA, treeheight_row = ifelse((class(cluster_rows) == "hclust") || cluster_rows, 50, 0), treeheight_col = ifelse((class(cluster_cols) == "hclust") || cluster_cols, 50, 0), legend = TRUE, legend_breaks = NA, legend_labels = NA, annotation_row = NA, annotation_col = NA, annotation = NA, annotation_colors = NA, annotation_legend = TRUE, annotation_names_row = TRUE, annotation_names_col = TRUE, drop_levels = TRUE, show_rownames = T, show_colnames = T, main = NA, fontsize = 10, fontsize_row = fontsize, fontsize_col = fontsize, angle_col = c("270", "0", "45", "90", "315"), display_numbers = F, number_format = "%.2f", number_color = "grey30", fontsize_number = 0.8 * fontsize, gaps_row = NULL, gaps_col = NULL, labels_row = NULL, labels_col = NULL, filename = NA, width = NA, height = NA, silent = FALSE, na_col = "#DDDDDD", ...){

# Set labels
if(is.null(labels_row)){
Expand Down Expand Up @@ -1053,9 +1055,39 @@ pheatmap = function(mat, color = colorRampPalette(rev(brewer.pal(n = 7, name = "
labels_col = NULL
}

# Set colname rotating parameters
angle_col = as.character(angle_col)
angle_col = match.arg(angle_col)

if(angle_col == "0"){
angle_col = 0
hjust_col = 0.5
vjust_col = 1
}
if(angle_col == "45"){
angle_col = 45
hjust_col = 1
vjust_col = 1
}
if(angle_col == "90"){
angle_col = 90
hjust_col = 1
vjust_col = 0.5
}
if(angle_col == "270"){
angle_col = 270
hjust_col = 0
vjust_col = 0.5
}
if(angle_col == "315"){
angle_col = 315
hjust_col = 0
vjust_col = 1
}

# Draw heatmap
pdf(file = NULL)
gt = heatmap_motor(mat, border_color = border_color, cellwidth = cellwidth, cellheight = cellheight, treeheight_col = treeheight_col, treeheight_row = treeheight_row, tree_col = tree_col, tree_row = tree_row, filename = filename, width = width, height = height, breaks = breaks, color = color, legend = legend, annotation_row = annotation_row, annotation_col = annotation_col, annotation_colors = annotation_colors, annotation_legend = annotation_legend, annotation_names_row = annotation_names_row, annotation_names_col = annotation_names_col, main = main, fontsize = fontsize, fontsize_row = fontsize_row, fontsize_col = fontsize_col, fmat = fmat, fontsize_number = fontsize_number, number_color = number_color, gaps_row = gaps_row, gaps_col = gaps_col, labels_row = labels_row, labels_col = labels_col, ...)
gt = heatmap_motor(mat, border_color = border_color, cellwidth = cellwidth, cellheight = cellheight, treeheight_col = treeheight_col, treeheight_row = treeheight_row, tree_col = tree_col, tree_row = tree_row, filename = filename, width = width, height = height, breaks = breaks, color = color, legend = legend, annotation_row = annotation_row, annotation_col = annotation_col, annotation_colors = annotation_colors, annotation_legend = annotation_legend, annotation_names_row = annotation_names_row, annotation_names_col = annotation_names_col, main = main, fontsize = fontsize, fontsize_row = fontsize_row, fontsize_col = fontsize_col, hjust_col = hjust_col, vjust_col = vjust_col, angle_col = angle_col, fmat = fmat, fontsize_number = fontsize_number, number_color = number_color, gaps_row = gaps_row, gaps_col = gaps_col, labels_row = labels_row, labels_col = labels_col, ...)
dev.off()

if(is.na(filename) & !silent){
Expand Down
12 changes: 8 additions & 4 deletions man/pheatmap.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 50ee175

Please sign in to comment.