0

This is my first post so please tell me if I am breaking your rules! My problem is described in the comments of this simplified version of my code.

I want to plot an unrooted phylogenetic tree that neatly highlights selected clades.

I like the results of using geom_hilight() from ggtree with type = 'encircle', but I do not like having to individually edit the node and color values for every input. (see method 1)

method 2 is close to what I want, but with the wrong highlight type (roundrect)

method 3 uses the correct highlight type (encircle) but returns an error.

# I don't think all of these packages are needed to reproduce this problem
library(ape)
library(dplyr)
library(GGally)
library(ggalt)
library(ggforce)
library(ggplot2)
library(ggtree)
library(tidyr)
library(tidytree)
library(treeio)

#my pipeline uses the output of RAxML.  I made this simpler tree for discussion purposes.
sink("test.tree")
cat("((((((t24:0.8024311261,t11:0.7828436729):0.3048173019,(t21:0.4867131179,t18:0.2167164627):0.7519672168):0.5776117099,t5:0.4223263576):0.5963104749,(t17:0.1558260066,t20:0.41109852):0.09447153704):0.2661841849,((((t6:0.009324073093,t12:0.2732205035):0.7790091021,t10:0.08588226303):0.3282297731,t9:0.2075715209):0.664191803,(((t15:0.5832811284,t14:0.8461383074):0.6081165755,t19:0.5950602938):0.7095833826,t8:0.7146228608):0.7801561591):0.6674923887):0.654328516,(((t13:0.6356930537,t3:0.8536336934):0.8644152461,t2:0.1784738901):0.7129137593,t23:0.8907998055):0.3618239218,((t16:0.1825823467,t7:0.8856151809):0.4720220205,(t22:0.672613536,(t1:0.9215354125,(t4:0.9248593273,t25:0.5937075356):0.3007316259):0.6941311779):0.6789765966):0.2112918347);")
sink()

#import tree
tree1 <- read.tree("test.tree")
#choose root nodes and colors for highlighting clades
group.roots <- c(34, 28, 44, 41)
group.colors <- c("#fd00fe",  "#62ce75",  "#9a1073", "#4ad9e1")
#write a data frame
g <- data.frame(gnode = group.roots, gfill = group.colors)
#
tree1unrooted <- ggtree(tree1,layout = 'unrooted')

#method 1: I want my plot to look like this, but I do not want to use so many instances of "geom_hilight()"
tree1unrooted + geom_label(aes(label = node)) +
  geom_hilight(
    node = 34,
    alpha = 1,
    fill = "#fd00fe",
    type = "encircle",
    to.bottom = TRUE
  ) +
  geom_hilight(
    node = 28,
    alpha = 1,
    fill = "#62ce75",
    type = "encircle",
    to.bottom = TRUE
  ) +
  geom_hilight(
    node = 44,
    alpha = 1,
    fill = "#9a1073",
    type = "encircle",
    to.bottom = TRUE
  ) +
  geom_hilight(
    node = 41,
    alpha = 1,
    fill = "#4ad9e1",
    type = "encircle",
    to.bottom = TRUE
  )

#method 2: I have used this method to highlight multiple clades successfully with "type = 'roundrect'", but the highlighed regions overlap.
tree1unrooted + 
  geom_hilight(
    data = g,
    mapping = aes(node = gnode, fill = gfill),
    alpha = 1,
    type='roundrect',
    to.bottom = TRUE
  )

#method 3: I need "type = 'encircle'" for my plot.  This gives the error: "Error in FUN(X[[i]], ...) : object 'x' not found"
tree1unrooted + 
  geom_hilight(
    data = g,
    mapping = aes(node = gnode, fill = gfill),
    alpha = 1,
    type='encircle',
    to.bottom = TRUE
  )

1 Answers1

1

This seems like a bug to me, since one wouldn't think changing the fill shape should cause an error when a different shape works with the same syntax.

It appears that the data passed to the geom_hilight layer gets merged with the plot data, and for some reason this step goes with the "encircle" shape.

Anyway, one obvious solution is to program a list of single geom_hilight layers and add that to the plot:


tree1unrooted + 
  lapply(seq(nrow(g)), function(i) {
    geom_hilight(
      node = g$gnode[i],
      alpha = 1,
      fill = g$gfill[i],
      type = "encircle",
      to.bottom = TRUE
    )
  })

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Thank you, this is very helpful as I am still learning to debug. Which package or function do you think is causing the bug? – Pond.Curtis Aug 28 '22 at 12:42
  • It seems to be in the `geom_hilight` internals. – Allan Cameron Aug 28 '22 at 15:54
  • apparently there is a function called `geom_hilight_encircle` that was added to fix this problem, but it doesn't work (or exist?) in my installed version. Maybe I need to build from source? https://github.com/YuLab-SMU/ggtree/blob/master/R/geom_hilight_encircle.R – Pond.Curtis Aug 28 '22 at 23:53