How do I colour specific lines joined by taxa?
The lines connect through matching tip labels on individual trees and I want to colour the lines via pre defined groups. e.g G1, G2 etc.
I have been following this guide: guide I have managed to create a test figure that using the following:
library(ape)
library(tidytree)
library(phytools)
library(dplyr)
library(ggtree)
library(ggplot2)
noroot_tree1 <- read.tree("1_HAgenotyping.aln.fasta.treefile.nwk")
noroot_tree2 <- read.tree("8_NAgenotyping.aln.fasta.treefile.nwk")
noroot_tree3 <- read.tree("7_NSgenotyping.aln.fasta.treefile.nwk")
x <- midpoint.root(noroot_tree1)
y <- midpoint.root(noroot_tree2)
z <- midpoint_root(noroot_tree3)
p1 <- ggtree(x)
p2 <- ggtree(y)
d1 <- p1$data
d2 <- p2$data
d2 <- fortify(y)
d3 <- fortify(z)
d2$x <- d2$x + max(d1$x) + 1
d3$x <- d3$x + max(d2$x) + 1
dd = bind_rows(d1, d2, d3) %>%
filter(!is.na(label))
p1 + geom_tree(data = d2) + geom_tree(data = d3) +
geom_line(aes(x, y, group=label, color=node), data=dd, alpha=.3)
This has produced a tibble format for the phylogeny as shown below
As you can see in the table the Labels start with G1|, G2| etc. is there a way to add a 'group' heading to this table and colour by group?
I have attempted to edit the color section of geom_line(aes(x, y, group=label, color=node), data=dd, alpha=.3) as the tutorial uses geom_line(aes(x, y, group=label, color=node < 15), data=dd, alpha=.3) to colour based on Node but this does like plotting multiple ranges of node numbers.
Ideally you should create a new column for the colors.
But a hacky way to do it would be:
Your legend will look very strange. To fix it, add the following: