从 ggtree 图中删除 'a'

Remove the 'a' from ggtree figure

给定一棵树,其尖端标签由一组着色,例如:

 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))

图例将包含 a 而不是所需的正方形。 ggplot2 文档说使用“override.aes”来覆盖此行为:

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

这不起作用。最重要的是,我可以更改大小、颜色、删除 a 但最重要的是我不能使用不同的形状(在本例中为正方形)。

较新版本的 ggplot2 有 key_glyph 选项,但这在使用 geom_tiplab 时也没有效果。

另一个奇怪的行为是,当使用 geom_tipppoint 时,覆盖图例有效。值得注意的是,上述策略以前适用于删除 a,但在最新的 R/ggplot2/ggtree 中,上述策略不起作用。这与 ggtree 的最新版本有关,与旧版本无关。

关于如何覆盖 geom_tiplab() 图例形状有什么建议吗?

我的环境:

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

(2022 年 6 月更新)一种选择是隐藏 geom_tiplab() 图例并将其替换为另一个未在图上绘制任何内容的几何图形(如 geom_polygon()),例如

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)

reprex package (v2.0.1)

创建于 2022-06-02

一个可能的解决方法是将标签更改为 unicode 'square' 字符,例如

library(tidyverse)
library(ggtree)
#> 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
#> 
#> Attaching package: 'ggtree'
#> The following object is masked from 'package:tidyr':
#> 
#>     expand

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)) +
  guides(color = guide_legend(override.aes = list(label = "\u25A1", size = 6)))
p

reprex package (v2.0.1)

于 2021-12-06 创建

填充方块:

p = ggtree(tree) %<+% meta +
  geom_tiplab(aes(color = group)) +
  guides(color = guide_legend(override.aes = list(label = "\u25A0", size = 7)))
p