1

给定一棵树,其提示标签按组着色,例如:

 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

在此处输入图像描述

4

1 回答 1

1

我不确定是否有一个聪明的方法可以做到这一点,但一个潜在的解决方法是将标签更改为 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 包于 2021-12-06 创建(v2.0.1)

填充方块:

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

示例_2.png

于 2021-12-06T00:24:53.680 回答