-
Notifications
You must be signed in to change notification settings - Fork 36
/
subplot.R
156 lines (133 loc) · 5.36 KB
/
subplot.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
setMethod("make_trace", signature = c(x = "GenericPlot"),
function(x, xaxes, yaxes, colorbars, colorbar_grid, ...){
xa <- xaxes[[xaxis_name(x)]]
ya <- yaxes[[yaxis_name(x)]]
trace <- modifyList(get_data(x),
list(xaxis = id(xa),
yaxis = id(ya)))
return(trace)
})
#' add_subplot
#'
#' Adds an arbitrary subplot to iheatmap
#' @param p iheatmap object
#' @param ... arguments to pass to plotly trace, see plotly.js documentation at
#' \url{https://plotly.com/javascript/reference/}
#' @param side which side of the current plot to add this heatmap? "right",
#' "left","top", or "bottom"
#' @param layout axis layout parameters (list)
#' @param size relative size of plot. size relative to first heatmap
#' @param buffer amount of space to leave empty before this plot, relative to
#' size of first heatmap
#' @param xname internal name of xaxis
#' @param yname internal name of yaxis
#' @param pname internal name of plot
#'
#' @seealso \code{\link{iheatmap}}
#' @return \code{\link{Iheatmap-class}} object, which can be printed to generate
#' an interactive graphic
#' @export
#' @rdname add_subplot
#' @name add_subplot
#' @aliases add_subplot,Iheatmap-method
#' @author Alicia Schep
#'
#' @examples
#' mat <- matrix(rnorm(24), ncol = 6)
#' hm <- iheatmap(mat) %>% add_subplot(x = 1:5, y=1:5, side = "top")
#'
#' # Print heatmap if interactive session
#' if (interactive()) hm
setMethod("add_subplot", c(p = "Iheatmap"),
function(p,
...,
side = c("top","bottom","right","left"),
layout = list(),
size = 1,
buffer = 0.1,
xname = if (side %in% c("top","bottom"))
current_xaxis(p) else NULL,
yname = if (side %in% c("left","right"))
current_yaxis(p) else NULL,
pname = "subplot"){
side <- match.arg(side)
if (side %in% c("left","right")){
p <- add_subplot_horizontal(p,
...,
xname = xname,
yname = yname,
size = size,
buffer = buffer,
side = side,
layout = layout,
pname = pname)
} else{
p <- add_subplot_vertical(p,
...,
xname = xname,
yname = yname,
size = size,
buffer = buffer,
side = side,
layout = layout,
pname = pname)
}
validObject(p)
p
})
setMethod("add_subplot_horizontal", c(p = "Iheatmap"),
function(p,
...,
xname = NULL,
yname = current_yaxis(p),
pname = "subplot",
size = 1,
buffer = 0.1,
side = c("right","left"),
layout = list()){
side <- match.arg(side)
new_x <- new_xaxis(p, yname, layout = layout)
if (is.null(xname)) xname <- id(new_x)
new_plot <- new("GenericPlot",
xaxis = xname,
yaxis = yname,
data = list(...))
p <- add_axis(p,
new_x,
yname = yname,
xname = xname,
size = size,
buffer = buffer,
side = side)
p <- add_plot(p, new_plot, pname)
validObject(p)
p
})
setMethod("add_subplot_vertical", c(p = "Iheatmap"),
function(p,
...,
xname = current_xaxis(p),
yname = NULL,
pname = "subplot",
size = 1,
buffer = 0.1,
side = c("top","bottom"),
layout = list()){
side <- match.arg(side)
new_y <- new_yaxis(p, xname, layout = layout)
if (is.null(yname)) yname <- id(new_y)
new_plot <- new("GenericPlot",
xaxis = xname,
yaxis = yname,
data = list(...))
p <- add_axis(p,
new_y,
yname = yname,
xname = xname,
size = size,
buffer = buffer,
side = side)
p <- add_plot(p, new_plot, pname)
validObject(p)
p
})