简体   繁体   中英

Remove the 'a' from ggtree figure

Given a tree with tip labels colored by a group eg:

 library(ggplot2)
 library(ggtree)
 nwk <- system.file("extdata", "sample.nwk", package="treeio")
 tree <- read.tree(nwk)
 meta = data.frame(label = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M"),
              group = c(rep("mammal", 5), rep("insect", 5), rep("bird", 3)))

 p = ggtree(tree) %<+% meta +
     geom_tiplab(aes(color = group))

The legend will contain a instead of the desired square. ggplot2 documentation says to use "override.aes" to override this behavior:

p + guides(color = guide_legend(override.aes = list(size = 4,
                                             label = "",
                                             shape = 15)))

This does not work. The overriding works, I can change the size, colors, remove the a but most importantly I can't use a different shape (in this case a square).

Newer version of ggplot2 have the key_glyph option, but this too has no effect when using geom_tiplab .

Another odd behavior is that when using geom_tipppoint , overriding the legend works. Of note, is that the above strategies previously worked to remove the a , however in the latest R/ggplot2/ggtree the above mentioned strategies do not work. This is related to the latest version of ggtree and not older versions.

Any advice on how to override geom_tiplab() legend shape?

My environment:

R version 4.1.2 (2021-11-01)
ggplot2 version 3.3.5
ggtree version 3.2.1
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Linux Mint 20

在此处输入图像描述

["

(Update June 2022) One option is to hide the geom_tiplab()<\/code> legend and replace it with another geom (like geom_polygon()<\/code> ) that doesn't draw anything on the plot, eg<\/i>

library(ggplot2)
library(ggtree)
#> Registered S3 method overwritten by 'ggtree':
#>   method      from 
#>   identify.gg ggfun
#> ggtree v3.0.4  For help: https://yulab-smu.top/treedata-book/
#> 
#> If you use ggtree in published research, please cite the most appropriate paper(s):
#> 
#> 1. Guangchuang Yu. Using ggtree to visualize data on tree-like structures. Current Protocols in Bioinformatics, 2020, 69:e96. doi:10.1002/cpbi.96
#> 2. Guangchuang Yu, Tommy Tsan-Yuk Lam, Huachen Zhu, Yi Guan. Two methods for mapping and visualizing associated data on phylogeny using ggtree. Molecular Biology and Evolution 2018, 35(12):3041-3043. doi:10.1093/molbev/msy194
#> 3. Guangchuang Yu, David Smith, Huachen Zhu, Yi Guan, Tommy Tsan-Yuk Lam. ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. Methods in Ecology and Evolution 2017, 8(1):28-36. doi:10.1111/2041-210X.12628
nwk <- system.file("extdata", "sample.nwk", package="treeio")
tree <- read.tree(nwk)
meta = data.frame(label = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M"),
                  group = c(rep("mammal", 5), rep("insect", 5), rep("bird", 3)))

ggtree(tree) %<+% meta +
  geom_tiplab(aes(color = group), show.legend = FALSE) +
  geom_polygon(aes(fill = group, x = 0, y = 0)) +
  scale_fill_discrete(na.translate = F)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM