Hi, everyone! I am using ggtree to make a phylogenetic tree with multiple matrix. I find the legends are arranged in a column. Since I have multiple matrix to plot, the legends column is too long. How can the legends be rearranged in a row? I try the cowplot::get_legend and plot_grid functions. But I cannot shrink the margins between the tree and legends.
A producible example quoted from https://yulab-smu.github.io/treedata-book/chapter7.html#gheatmap
nwk <- system.file("extdata", "sample.nwk", package="treeio") tree <- read.tree(nwk) circ <- ggtree(tree, layout = "circular") df <- data.frame(first=c("a", "b", "a", "c", "d", "d", "a", "b", "e", "e", "f", "c", "f"), second= c("z", "z", "z", "z", "y", "y", "y", "y", "x", "x", "x", "a", "a")) rownames(df) <- tree$tip.label df2 <- as.data.frame(matrix(rnorm(39), ncol=3)) rownames(df2) <- tree$tip.label colnames(df2) <- LETTERS[1:3] p1 <- gheatmap(circ, df, offset=.8, width=.2, colnames_angle=95, colnames_offset_y = .25) + scale_fill_viridis_d(option="D", name="discrete\nvalue") library(ggnewscale) p2 <- p1 + new_scale_fill() gheatmap(p2, df2, offset=15, width=.3, colnames_angle=90, colnames_offset_y = .25) + scale_fill_viridis_c(option="A", name="continuous\nvalue")
my script of get_legend and plot_grid
pp <- gheatmap(p2, df2, offset=15, width=.3, colnames_angle=90, colnames_offset_y = .25) + scale_fill_viridis_c(option="A", name="continuous\nvalue")+ theme(legend.position = "none")
leg1 <- get_legend(p1)
leg2 <- get_legend(gheatmap(circ, df2, offset=15, width=.3, colnames_angle=90, colnames_offset_y = .25) + scale_fill_viridis_c(option="A", name="continuous\nvalue"))
plot_grid(pp,leg1,leg2,ncol=3)+theme(plot.background = element_rect(fill="grey"))
Thank you, Kevin! You do me a favor! I think it is moving legends to the bottom that changes legend arrangement. Quite a smart idea!
I just find
legend.box
parameter in thetheme()
function. That is exactly what I am searching for.