How to rearrange legends position in phylogenetic tree with multiple associated matrix made by ggtree?
1
0
Entering edit mode
4.6 years ago
YiweiZhu ▴ 30

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.


  1. 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")
    

Example 1

  1. 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"))

enter image description here

ggtree cowplot get_legend plot_grid • 5.5k views
ADD COMMENT
2
Entering edit mode
4.6 years ago

Hey,

You can do this via standard ggplot2 functions:

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 = 'bottom',
    legend.background = element_rect(),
    legend.key = element_blank(), # removes the border
    legend.key.size = unit(0.8, 'cm'), # sets overall area/size of the legend
    legend.text = element_text(size = 12), # text size
    title = element_text(size = 12))

k

So, if you want to make any further modifications, just search the WWW for ggplot2 general functionality.

Kevin

ADD COMMENT
1
Entering edit mode

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!

ADD REPLY
0
Entering edit mode

I just find legend.box parameter in the theme() function. That is exactly what I am searching for.

ADD REPLY

Login before adding your answer.

Traffic: 1175 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6