-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tblog.R
60 lines (49 loc) · 1.86 KB
/
Tblog.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
#code based on functions from: https://thackl.github.io/ggtree-composite-plots
library(ggtree)
library(patchwork)
no_legend <- function() theme(legend.position="none")
tree_y <- function(ggtree, data){
if(!inherits(ggtree, "ggtree"))
stop("not a ggtree object")
left_join(select(data, label), select(ggtree$data, label, y)) %>%
pull(y)
}
# overwrite the default expand for continuous scales
scale_y_tree <- function(expand=expand_scale(0, 0.6), ...){
scale_y_continuous(expand=expand, ...)
}
# get the range of the ggtree y-axis data
tree_ylim <- function(ggtree){
if(!inherits(ggtree, "ggtree"))
stop("not a ggtree object")
range(ggtree$data$y)
}
# plot data next to a ggtree aligned by shared labels
ggtreeplot <- function(ggtree, data = NULL, mapping = aes(), flip=FALSE,
expand_limits=expand_scale(0,.6), ...){
if(!inherits(ggtree, "ggtree"))
stop("not a ggtree object")
# match the tree limits
limits <- tree_ylim(ggtree)+c(-5,0)#JZ added
limits[1] <- limits[1] + (limits[1] * expand_limits[1]) - expand_limits[2]
limits[2] <- limits[2] + (limits[2] * expand_limits[3]) + expand_limits[4]
if(flip){
mapping <- modifyList(aes_(x=~x), mapping)
data <- mutate(data, x=tree_y(ggtree, data))
gg <- ggplot(data=data, mapping = mapping, ...) +
scale_x_continuous(limits=limits, expand=c(0,0))
}else{
mapping <- modifyList(aes_(y=~y), mapping)
data <- mutate(data, y=tree_y(ggtree, data))
gg <- ggplot(data=data, mapping = mapping, ...) +
scale_y_continuous(limits=limits, expand=c(0,0))
}
gg
}
# get rid of superfluous axis - this works after coord_flip, so it also works
# for the rotated histogram
no_y_axis <- function ()
theme(axis.line.y = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())