2

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

enter image description here

Amar
  • 1,340
  • 1
  • 8
  • 20

1 Answers1

2

(Update June 2022) One option is to hide the geom_tiplab() legend and replace it with another geom (like geom_polygon()) that doesn't draw anything on the plot, e.g.

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)

Created on 2022-06-02 by the reprex package (v2.0.1)


A potential workaround is to change the label to the unicode 'square' character, e.g.

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

Created on 2021-12-06 by the reprex package (v2.0.1)

With filled squares:

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

example_2.png

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
  • You've solved my issue and this probably deserves another SO question but I was wondering if you knew why saving with ggsave as pdf results in the squares being `...` but saving as png gives the desired result? – Amar Dec 06 '21 at 02:35
  • 2
    Try with the cairo device, e.g. `ggsave(filename = "whatever.pdf", ..., device = cairo_pdf)` – jared_mamrot Dec 06 '21 at 02:37
  • 1
    You're welcome. More details on why cairo is required here: https://www.andrewheiss.com/blog/2017/09/27/working-with-r-cairo-graphics-custom-fonts-and-ggplot/ ("Embedding fonts in PDFs is also fairly easy. Instead of using R’s default PDF-writing engine, you can use the Cairo graphics library (which, nowadays, is conveniently packaged with R). Cairo has full Unicode support and can handle embedding custom fonts just fine.") – jared_mamrot Dec 06 '21 at 02:39