I am creating tanglegrams with the following code:
library(ggtree)
library(ape)
tree1 <- read.tree(text='(((A:4.2,B:4.2):3.1,C:7.3):6.3,D:13.6);')
tree2 <- read.tree(text='(((B:4.2,A:4.2):3.1,C:7.3):6.3,D:13.6);')
p1 <- ggtree(tree1)
p2 <- ggtree(tree2)
d1 <- p1$data
d2 <- p2$data
d2$x <- max(d2$x) - d2$x + max(d1$x) + 1
pp <- p1 + geom_tree(data=d2)
dd <- bind_rows(d1, d2) %>%
filter(!is.na(label))
final_plot <- pp + geom_line(aes(x, y, group=label), data=dd, color='grey')
What I want to do is to color the lines based on the position of the nodes. In other words, if the line is straight, meaning that they have the same position in both trees, the color should be x, while if they have changed, it should be y.
It would also be nice to get a legend for this to explain the colors.