I have been following the manual in order to plot tree with a bar graph alongside.
I can use the facet plot method but ideally I would like to change the relative widths of the plot.
Is there a method to plot only the tree and the barplot side by side, without the heatmap in the middle? I have attached an image of what I am aiming for.
I found a workaround, by adding a blank dummy plot in between. See the code below. However, I was wondering if there was a simpler way of doing this?
library(ggplot2)
library(ggtree)
library(cowplot)
library(aplot)
set.seed(2019-10-31)
tr <- rtree(10)
# dataset 1
d1 <- data.frame(
# only some labels match
label = c(tr$tip.label[sample(5, 5)], "A"),
value = sample(1:6, 6))
# dataset 2
d2 <- data.frame(
label = rep(tr$tip.label, 5),
category = rep(LETTERS[1:5], each=10),
value = rnorm(50, 0, 3))
# tree
g <- ggtree(tr) + geom_tiplab(align=TRUE)
# plot 1
p1 <- ggplot(d1, aes(label, value)) + geom_col(aes(fill=label)) +
geom_text(aes(label=label, y= value+.1)) +
coord_flip() + theme_tree2() + theme(legend.position='none')
# plot 2
p2 <- ggplot(d2, aes(x=category, y=label)) +
geom_tile(aes(fill=value)) + scale_fill_viridis_c() +
theme_tree2()
# plot side by side
p2 %>% insert_left(g) %>% insert_right(p1, width=.5)
# nether work as intended
g %>% insert_right(p1)
p1 %>% insert_left(g)
# create dummy data to sit in between
d2_dummy <- data.frame(label = tr$tip.label,
category = LETTERS[1:5])
# dummy plot
p2_dummy <- ggplot(data = d2, aes(x=category, y=label)) + theme_void()
# plot side by side with dummy plot in between
p2_dummy %>% insert_left(g, width = 10) %>% insert_right(p1, width=5)