【问题标题】:Remove the 'a' from ggtree figure从 ggtree 图中删除“a”
【发布时间】:2023-03-30 08:26:02
【问题描述】:

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

 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

【问题讨论】:

    标签: r ggplot2 ggtree


    【解决方案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 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
    

    【讨论】:

    • 您已经解决了我的问题,这可能值得另一个 SO 问题,但我想知道您是否知道为什么使用 ggsave 保存为 pdf 会导致正方形为 ... 但另存为 png 会给出所需的结果?
    • 尝试使用 cairo 设备,例如ggsave(filename = "whatever.pdf", ..., device = cairo_pdf)
    • 不客气。关于这里为什么需要 cairo 的更多详细信息:andrewheiss.com/blog/2017/09/27/…(“在 PDF 中嵌入字体也相当容易。您可以使用 Cairo 图形库(如今,它与 R ). Cairo 具有完整的 Unicode 支持,并且可以很好地处理嵌入自定义字体。")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    • 2011-09-19
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    相关资源
    最近更新 更多