-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathhmAxis.R
140 lines (116 loc) · 4.1 KB
/
hmAxis.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#' Modify axis options
#'
#' Provides options for modifying the x and y axes of a D3 Heatmap widget
#'
#' @param d3heatmap a d3heatmap object created from the d3heatmap()
#'
#' @param axis Name of the axis to modify; either "x", "y", "row", or "column"
#'
#' @param size Size of axes, in pixels.
#'
#' @param labels character vectors with axis labels to use (top to bottom for y axis, left to right for x); default to rownames(x) or colnames(x).
#'
#' @param font.size integer Font size of axis labels, in pixels (i.e., will be translated to a
#' character string with 'px' appended)
#'
#' @param angle Angle of x axis labels (x axis only). Defaults to 60. Maximum of 90 (vertical), minimum of 25.
#'
#' @param location Location of the axis, either "bottom" or "top" for the x axis, and
#' either "right" or "left" for the y axis. Defaults to "bottom" and "right".
#'
#' @param title Title text
#'
#' @param title.font.size Font size of axis title in pixels. Defaults to 14.
#'
#' @return Modified d3heatmap object
#'
#' @source
#' The interface was inspired by \cite{dygraphs}
#'
#' @seealso
#' \link{heatmap}, \link[gplots]{heatmap.2}
#'
#' @examples
#' \dontrun{
#'
#' d3heatmap(mtcars, scale = "column", col = "Blues") %>%
#' hmAxis("x", angle = 30, title = "test", location = 'top', font.size = '24px') %>%
#' hmAxis("y", title = "test", location = 'right')
#'
#' }
#'
#' @export
hmAxis <- function(d3heatmap
, axis = c("x", "y", "row", "column")
, size
, labels
, font.size
, angle
, location
, title
, title.font.size) {
if (missing(d3heatmap))
stop("hmAxis: no d3heatmap provided")
if (missing(axis)) {
message("hmAxis: no axis specified... returning original heatmap")
return(d3heatmap)
}
params <- d3heatmap$x$params
x <- params$x
options <- d3heatmap$x$options
axis <- match.arg(axis)
if (axis %in% c("x", "column")) {
if (missing(size)) size <- options$xaxis_height
if (missing(title)) title <- options$xaxis_title
if (missing(title.font.size)) title.font.size <- options$xaxis_title_font_size
if (!missing(location)) xaxis_location <- match.arg(location, c('bottom', 'top'))
else xaxis_location <- options$xaxis_location
if (missing(font.size)) font.size <- options$xaxis_font_size
if (!missing(labels)) colnames(x) <- labels
cellnote_col <- options$cellnote_col
if (is.null(cellnote_col)) cellnote_col <- title
if (missing(angle)) angle <- options$srtCol
angle <- min(90, max(angle, 25))
opts <- list(
xaxis_height = size,
xaxis_font_size = font.size,
xaxis_angle = angle,
xaxis_location = xaxis_location,
xaxis_title = title,
xaxis_title_font_size = title.font.size,
cellnote_col = cellnote_col
)
} else if (axis %in% c("y", "row")) {
if (missing(size)) size <- options$yaxis_width
if (missing(title)) title <- options$yaxis_title
if (missing(title.font.size)) title.font.size <- options$yaxis_title_font_size
if (missing(font.size)) font.size <- options$yaxis_font_size
if (!missing(location)) yaxis_location <- match.arg(location, c('right', 'left'))
else yaxis_location <- options$yaxis_location
if (!missing(labels)) rownames(x) <- labels
cellnote_row <- options$cellnote_row
if (is.null(cellnote_row)) cellnote_row <- title
opts <- list(
yaxis_width = size,
yaxis_font_size = font.size,
yaxis_location = yaxis_location,
yaxis_title = title,
yaxis_title_font_size = title.font.size,
cellnote_row = cellnote_row
)
} else return(d3heatmap)
# we only have to re-run the heatmap creation if we've
# changed the row or columns names for the matrix
if (missing(labels)) {
params$x <- x
## call heatmap with the updated params and save
## the params with the heatmap for later use
##==============================================
hm <- do.call(heatmap, args = params)
d3heatmap$x$matrix <- hm$mtx
d3heatmap$x$params <- params
}
options <- mergeLists(options, opts)
d3heatmap$x$options <- options
return(d3heatmap)
}