File: test-Legend.R

package info (click to toggle)
r-bioc-complexheatmap 2.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,632 kB
  • sloc: makefile: 5
file content (270 lines) | stat: -rw-r--r-- 10,857 bytes parent folder | download | duplicates (3)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
library(circlize)
library(ComplexHeatmap)
library(GetoptLong)

if(!exists("random_str")) {
	random_str = ComplexHeatmap:::random_str
}

lgd = Legend(at = 1:6, legend_gp = gpar(fill = 1:6))
draw(lgd, test = "default discrete legends style")

lgd = Legend(labels = 1:6, legend_gp = gpar(fill = 1:6))
draw(lgd, test = "only specify labels with no at")


lgd = Legend(labels = month.name[1:6], title = "foo", legend_gp = gpar(fill = 1:6))
draw(lgd, test = "add labels and title")

lgd = Legend(labels = month.name[1:6], title = "foo", legend_gp = gpar(fill = 1:6),
	title_position = "lefttop")
draw(lgd, test = "title put in the lefttop")

lgd = Legend(labels = month.name[1:6], title = "foo", legend_gp = gpar(fill = 1:6),
	title_position = "lefttop-rot")
draw(lgd, test = "title put in the lefttop-rot")

lgd = Legend(labels = month.name[1:6], title = "foo", legend_gp = gpar(fill = 1:6),
	title_position = "leftcenter-rot")
draw(lgd, test = "title put in the leftcenter-rot")

lgd = Legend(labels = 1:6, title = "fooooooo", legend_gp = gpar(fill = 1:6))
draw(lgd, test = "title is longer than the legend body")

lgd = Legend(at = 1:6, legend_gp = gpar(fill = 1:6), grid_height = unit(1, "cm"), 
	title = "foo", grid_width = unit(5, "mm"))
draw(lgd, test = "grid size")

lgd = Legend(labels = month.name[1:6], legend_gp = gpar(fill = 1:6), title = "foo", 
	labels_gp = gpar(col = "red", fontsize = 14))
draw(lgd, test = "labels_gp")

lgd = Legend(labels = month.name[1:6], legend_gp = gpar(fill = 1:6), title = "foo", 
	title_gp = gpar(col = "red", fontsize = 14))
draw(lgd, test = "title_gp")

lgd = Legend(labels = month.name[1:6], legend_gp = gpar(fill = 1:6), title = "foo", 
	border = "red")
draw(lgd, test = "legend border")

lgd = Legend(labels = month.name[1:10], legend_gp = gpar(fill = 1:10), title = "foo", 
	ncol = 3)
draw(lgd, test = "in 3 columns")

lgd = Legend(labels = month.name[1:10], legend_gp = gpar(fill = 1:10), title = "foo", 
	ncol = 3, title_position = "topcenter")
draw(lgd, test = "in 3 columns, title in the center")

lgd = Legend(labels = month.name[1:10], legend_gp = gpar(fill = 1:10), title = "foo", 
	ncol = 3, by_row = TRUE)
draw(lgd, test = "in 3 columns and by rows")

lgd = Legend(labels = month.name[1:10], legend_gp = gpar(fill = 1:10), title = "foo", 
	ncol = 3, gap = unit(1, "cm"))
draw(lgd, test = "in 3 columns with gap between columns")

lgd = Legend(labels = month.name[1:10], legend_gp = gpar(fill = 1:10), title = "foo", 
	nrow = 3)
draw(lgd, test = "in 3 rows")

lgd = Legend(labels = month.name[1:6], legend_gp = gpar(fill = 1:6), title = "foooooo", 
	nrow = 1, title_position = "lefttop")
draw(lgd, test = "1 row and title is on the left")

lgd = Legend(labels = month.name[1:6], legend_gp = gpar(fill = 1:6), title = "foooooo", 
	nrow = 1, title_position = "lefttop-rot")
draw(lgd, test = "1 row and title is on the left, 90 rotation")

lgd = Legend(labels = month.name[1:6], legend_gp = gpar(fill = 1:6), title = "foooooo", 
	nrow = 1, title_position = "leftcenter")
draw(lgd, test = "1 row and title is on the left, 90 rotation")

lgd = Legend(labels = month.name[1:6], title = "foo", type = "points", pch = 1:6, 
	legend_gp = gpar(col = 1:6), background = "red")
draw(lgd, test = "points as legends")

lgd = Legend(labels = month.name[1:6], title = "foo", type = "points", pch = letters[1:6], 
	legend_gp = gpar(col = 1:6), background = "white")
draw(lgd, test = "letters as legends")

lgd = Legend(labels = month.name[1:6], title = "foo", type = "lines", 
	legend_gp = gpar(col = 1:6, lty = 1:6))
draw(lgd, test = "lines as legends")

###### vertical continous legend #######
col_fun = colorRamp2(c(0, 0.5, 1), c("blue", "white", "red"))
lgd = Legend(col_fun = col_fun, title = "foo")
draw(lgd, test = "only col_fun")

lgd = Legend(col_fun = col_fun, title = "foo", at = c(0, 0.25, 0.5, 0.75, 1))
draw(lgd, test = "with at")

lgd = Legend(col_fun = col_fun, title = "foo", at = rev(c(0, 0.25, 0.5, 0.75, 1)))
draw(lgd, test = "with at")


lgd = Legend(col_fun = col_fun, title = "foo", at = c(0, 0.5, 1), labels = c("low", "median", "high"))
draw(lgd, test = "with labels")

lgd = Legend(col_fun = col_fun, title = "foo", legend_height = unit(6, "cm"))
draw(lgd, test = "set legend_height")

lgd = Legend(col_fun = col_fun, title = "foo", labels_gp = gpar(col = "red"))
draw(lgd, test = "set label color")

lgd = Legend(col_fun = col_fun, title = "foo", border = "red")
draw(lgd, test = "legend border")

lgd = Legend(col_fun = col_fun, title = "foooooooo", title_position = "lefttop-rot")
draw(lgd, test = "lefttop rot title")

lgd = Legend(col_fun = col_fun, title = "foooooooo", title_position = "leftcenter-rot")
draw(lgd, test = "leftcenter top title")


lgd = Legend(col_fun = col_fun, title = "foo", title_position = "lefttop", direction = "horizontal")
draw(lgd, test = "lefttop title")

###### horizontal continous legend #######
col_fun = colorRamp2(c(0, 0.5, 1), c("blue", "white", "red"))
lgd = Legend(col_fun = col_fun, title = "foo", direction = "horizontal")
draw(lgd, test = "only col_fun")

lgd = Legend(col_fun = col_fun, title = "foo", at = c(0, 0.25, 0.5, 0.75, 1), direction = "horizontal")
draw(lgd, test = "with at")

lgd = Legend(col_fun = col_fun, title = "foo", at = rev(c(0, 0.25, 0.5, 0.75, 1)), direction = "horizontal")
draw(lgd, test = "with at")

lgd = Legend(col_fun = col_fun, title = "foo", at = c(0, 0.5, 1), labels = c("low", "median", "high"),
	direction = "horizontal")
draw(lgd, test = "with labels")

lgd = Legend(col_fun = col_fun, title = "foo", legend_width = unit(6, "cm"), direction = "horizontal")
draw(lgd, test = "set legend_width")

lgd = Legend(col_fun = col_fun, title = "foo", labels_gp = gpar(col = "red"), direction = "horizontal")
draw(lgd, test = "set label color")

lgd = Legend(col_fun = col_fun, title = "foo", border = "red", direction = "horizontal")
draw(lgd, test = "legend border")

lgd = Legend(col_fun = col_fun, title = "foooooooo", direction = "horizontal", 
	title_position = "topcenter")
draw(lgd, test = "topcenter title")

lgd = Legend(col_fun = col_fun, title = "foooooooo", direction = "horizontal", 
	title_position = "lefttop")
draw(lgd, test = "lefttop title")

lgd = Legend(col_fun = col_fun, title = "foooooooo", direction = "horizontal", 
	title_position = "leftcenter")
draw(lgd, test = "leftcenter title")


###### pack legend
lgd1 = Legend(at = 1:6, legend_gp = gpar(fill = 1:6), title = "legend1")
lgd2 = Legend(col_fun = col_fun, title = "legend2", at = c(0, 0.25, 0.5, 0.75, 1))

pd = packLegend(lgd1, lgd2)
draw(pd, test = "two legends")

pd = packLegend(list = list(lgd1, lgd2))
draw(pd, test = "two legends specified as a list")

pd = packLegend(lgd1, lgd2, direction = "horizontal")
draw(pd, test = "two legends packed horizontally")

lgd3 = Legend(at = 1:6, legend_gp = gpar(fill = 1:6), title = "legend1")
lgd4 = Legend(col_fun = col_fun, title = "legend2", at = c(0, 0.25, 0.5, 0.75, 1), direction = "horizontal")
pd = packLegend(lgd3, lgd4)
draw(pd, test = "two legends with different directions")
pd = packLegend(lgd3, lgd4, direction = "horizontal")
draw(pd, test = "two legends with different directions")

pd = packLegend(lgd1, lgd2, lgd1, lgd2)
draw(pd, test = "many legends with same legends")

lgd3 = Legend(at = 1:6, legend_gp = gpar(fill = 1:6), title = "legend1")
lgd4 = Legend(col_fun = col_fun, title = "legend2", at = c(0, 0.25, 0.5, 0.75, 1))
pd = packLegend(lgd1, lgd2, lgd3, lgd4)
draw(pd, test = "many legends with all different legends")

pd = packLegend(lgd1, lgd2, lgd1, lgd2, lgd1, lgd2)
draw(pd, test = "many legends")

pd = packLegend(lgd1, lgd2, lgd1, lgd2, lgd1, lgd2, max_height = unit(1, "npc"))
draw(pd, test = "many legends, max_height = unit(1, 'npc')")
## reduce the height of the interactive window and rerun draw()

pd = packLegend(lgd1, lgd2, lgd1, lgd2, lgd1, lgd2, max_height = unit(10, "cm"))
draw(pd, test = "many legends, max_height = unit(10, 'cm')")

pd = packLegend(lgd1, lgd2, lgd1, lgd2, lgd1, lgd2, max_height = unit(10, "cm"), gap = unit(1, "cm"))
draw(pd, test = "many legends, max_height = unit(10, 'cm'), with gap")

lgd_long = Legend(at = 1:50, legend_gp = gpar(fill = 1:50))
pd = packLegend(lgd1, lgd2, lgd1, lgd2, lgd1, lgd2, lgd_long, max_height = unit(10, "cm"))
draw(pd, test = "many legends with a long one, max_height = unit(10, 'cm')")

lgd1 = Legend(at = 1:6, legend_gp = gpar(fill = 1:6), title = "legend1",
	nr = 1)
lgd2 = Legend(col_fun = col_fun, title = "legend2", at = c(0, 0.25, 0.5, 0.75, 1),
	direction = "horizontal")
pd = packLegend(lgd1, lgd2, lgd1, lgd2, lgd1, lgd2, direction = "horizontal")
draw(pd, test = "many legends")

pd = packLegend(lgd1, lgd2, lgd1, lgd2, lgd1, lgd2, max_width = unit(1, "npc"), direction = "horizontal")
draw(pd, test = "many legends, max_width = unit(1, 'npc')")
## reduce the height of the interactive window and rerun draw()

pd = packLegend(lgd1, lgd2, lgd1, lgd2, lgd1, lgd2, max_width = unit(10, "cm"), direction = "horizontal")
draw(pd, test = "many legends, max_width = unit(10, 'cm')")


####### unequal interval breaks
col_fun = colorRamp2(c(0, 0.5, 1), c("blue", "white", "red"))
lgd = Legend(col_fun = col_fun, title = "foo", at = c(0, 0.1, 0.15, 0.5, 0.9, 0.95, 1))
draw(lgd, test = "unequal interval breaks")
lgd = Legend(col_fun = col_fun, title = "foo", at = c(0, 0.3, 1), legend_height = unit(4, "cm"))
draw(lgd, test = "unequal interval breaks but not label position adjustment")

lgd = Legend(col_fun = col_fun, title = "foo", at = c(0, 0.1, 0.15, 0.5, 0.9, 0.95, 1),
	direction = "horizontal")
draw(lgd, test = "unequal interval breaks")

lgd = Legend(col_fun = col_fun, title = "foo", at = c(0, 0.1, 0.15, 0.5, 0.9, 0.95, 1),
	direction = "horizontal", title_position = "lefttop")
draw(lgd, test = "unequal interval breaks")


lgd = Legend(col_fun = col_fun, title = "foo", at = c(0, 0.1, 0.15, 0.5, 0.9, 0.95, 1),
	direction = "horizontal", title_position = "lefttop", labels_rot = 90)
draw(lgd, test = "unequal interval breaks, label rot 90")

lgd = Legend(col_fun = col_fun, title = "foo", at = c(0, 0.1, 0.5, 0.75, 1),
	labels = c("mininal", "q10", "median", "q75", "maximal"),
	direction = "horizontal", title_position = "lefttop")
draw(lgd, test = "unequal interval breaks with labels")


lgd = Legend(col_fun = col_fun, title = "foo", at = c(0, 0.1, 0.5, 0.75, 1),
	labels = c("mininal", "q10", "median", "q75", "maximal"),
	direction = "horizontal")
draw(lgd, test = "unequal interval breaks with labels")


col_fun = colorRamp2(c(0, 0.05, 0.1, 0.5, 1), c("green", "white", "red", "black", "blue"))
lgd = Legend(col_fun = col_fun, title = "foo", break_dist = 1:4)
draw(lgd, test = "unequal interval breaks")


#### position of legends to heatmaps ##
if(0) {
m = matrix(rnorm(100), 10)
rownames(m) = random_str(10, len = 20)
colnames(m) = random_str(10, len = 20)
Heatmap(m)
}