Remove the 'a' from legend for geom_tiplab

363 views
Skip to first unread message

Amar Ansar

unread,
Dec 2, 2021, 8:40:45 PM12/2/21
to ggtree
Hi there,

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

     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. ggplot 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.

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

Amar Ansar

unread,
Dec 6, 2021, 10:12:32 PM12/6/21
to ggtree
I've found the solution, use unicode for the label:

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

Hope this helps someone else.
Reply all
Reply to author
Forward
0 new messages