-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfigure.util.R
93 lines (74 loc) · 2.4 KB
/
figure.util.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
require(grid)
require(gridExtra)
require(gtable)
require(ggplot2)
match.widths.grob <- function(g.list) {
max.width <- g.list[[1]]$widths[2:7]
for(j in 2:length(g.list)) {
max.width <- grid::unit.pmax(max.width, g.list[[j]]$widths[2:7])
}
for(j in 1:length(g.list)) {
g.list[[j]]$widths[2:7] <- as.list(max.width)
}
return(g.list)
}
match.widths <- function(p.list) {
g.list <- lapply(p.list, ggplotGrob)
return(match.widths.grob(g.list))
}
grid.vcat <- function(p.list, ...) {
g.list <- match.widths(p.list)
ret <- grid.arrange(grobs = g.list, ncol = 1, newpage = FALSE, ...)
return(ret)
}
match.heights.grob <- function(g.list, stretch = TRUE) {
max.height <- g.list[[1]]$heights[2:7]
if(stretch) {
for(j in 2:length(g.list)) {
max.height <- grid::unit.pmax(max.height, g.list[[j]]$heights[2:7])
}
}
for(j in 1:length(g.list)) {
g.list[[j]]$heights[2:7] <- as.list(max.height)
}
return(g.list)
}
match.heights <- function(p.list, stretch = FALSE) {
g.list <- lapply(p.list, ggplotGrob)
return(match.heights.grob(g.list, stretch))
}
grid.hcat <- function(p.list, ...) {
g.list <- match.heights(p.list, stretch = TRUE)
ret <- grid.arrange(grobs = g.list, nrow = 1, newpage = FALSE, ...)
return(ret)
}
row.order <- function(mat) {
require(cba)
require(proxy)
if(nrow(mat) < 3) {
return(1:nrow(mat))
}
D <- proxy::dist(mat, method = function(a,b) 1 - cor(a,b, method = 'spearman'))
D[!is.finite(D)] <- 0
h.out <- hclust(D)
o.out <- cba::order.optimal(D, h.out$merge)
return(o.out$order)
}
gg.plot <- function(...) {
ggplot(...) + theme_bw() + theme(plot.background = element_blank(),
panel.background = element_blank(),
strip.background = element_blank(),
legend.background = element_blank())
}
order.pair <- function(pair.tab) {
require(tidyr)
require(dplyr)
M <- pair.tab %>%
dplyr::select(row, col, weight) %>%
tidyr::spread(key = col, value = weight, fill = 0) %>%
as.data.frame()
ro <- row.order(M %>% dplyr::select(-row) %>% as.matrix())
co <- row.order(t(M %>% dplyr::select(-row) %>% as.matrix()))
cc <- colnames(M)[-1]
list(rows = M[ro, 1], cols = colnames(M)[-1][co])
}