Skip to content

Commit

Permalink
Changed cell_text to ht_text function
Browse files Browse the repository at this point in the history
SCIF Blackmednet Albright Rapid Reaction COSMOS Dirty bomb Taiwan
Emergency Sugar Grove INS SISMI Gorizont RL National Guard TELINT
  • Loading branch information
benjamin-james committed Jun 20, 2024
1 parent 5f8a54d commit 1cff0ce
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions R/plotting.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ htSortMatrix <- function(M, method="euclidean", ratio=0.5, cutoff=0.25, sort=c(1
#' @export
autoHeatmap <- function(M, ux=1.5, sort=c(1, 2), method="euclidean",
dimname_fontsize=3.5, ratio=0.5, cutoff=0.25,
aspect_ratio=1, cell_text=NULL, cell_text_fontsize=10,
aspect_ratio=1,
cluster_rows=FALSE, cluster_columns=FALSE, ...) {
if (is.logical(cluster_rows)) {
cluster_rows = cluster_rows || -1 %in% sort
Expand All @@ -75,29 +75,14 @@ autoHeatmap <- function(M, ux=1.5, sort=c(1, 2), method="euclidean",
cluster_columns = cluster_columns || -2 %in% sort
}
M = htSortMatrix(M, method=method, ratio=ratio, cutoff=cutoff, sort=sort)
cell_fun=NULL
if (!is.null(cell_text)) {
cell_fun = function(j, i, x, y, width, height, fill) {
grid.text(sprintf(cell_text, M[i, j]), x, y, gp = gpar(fontsize = cell_text_fontsize)) }
return(ComplexHeatmap::Heatmap(
M, cluster_rows=cluster_rows, cluster_columns=cluster_columns,
width = ncol(M)*grid::unit(aspect_ratio * ux, "mm"),
height = nrow(M)*grid::unit(ux, "mm"),
row_names_gp=grid::gpar(fontsize=dimname_fontsize),
column_names_gp=grid::gpar(fontsize=dimname_fontsize),
cell_fun=cell_fun,
...
))
} else {
return(ComplexHeatmap::Heatmap(
M, cluster_rows=cluster_rows, cluster_columns=cluster_columns,
width = ncol(M)*grid::unit(aspect_ratio * ux, "mm"),
height = nrow(M)*grid::unit(ux, "mm"),
row_names_gp=grid::gpar(fontsize=dimname_fontsize),
column_names_gp=grid::gpar(fontsize=dimname_fontsize),
...
))
}
return(ComplexHeatmap::Heatmap(
M, cluster_rows=cluster_rows, cluster_columns=cluster_columns,
width = ncol(M)*grid::unit(aspect_ratio * ux, "mm"),
height = nrow(M)*grid::unit(ux, "mm"),
row_names_gp=grid::gpar(fontsize=dimname_fontsize),
column_names_gp=grid::gpar(fontsize=dimname_fontsize),
...
))
}
#' Save ComplexHeatmap, modified from Carles Boix
#'
Expand Down Expand Up @@ -138,6 +123,13 @@ saveGGplot <- function(gp, pltprefix, w=7, h=7, dpi=600) {
print(pltprefix)
}

#' Draw text on a heatmap
#' @export
ht_text <- function(mat, format="%.2f", fontsize=4, ...) {
return(function(j, i, x, y, width, height, fill) {
grid::grid.text(sprintf(format, mat[i, j]), x, y, gp=gpar(fontsize=fontsize, ...))
})
}
#' Draw triangles in heatmap
#' Pass cell_fun=ht_triangle_split(...) to Heatmap()
#' @param mat.ul Upper left matrix
Expand Down

0 comments on commit 1cff0ce

Please sign in to comment.