2

I am trying to do something similar to what is described in the blog here but using R with ggtree, ggmap, and ggplot2.

I want to be able to combine the plots of the phylogenetic tree and the map showing the sampling locations of the tips on a geographical map, and link the tips to the sampling locations by segments. That would allow to see ie. if some clusters appears to specific geographical locations (ie north, south of an area) and would allow also to display different data with tips colors/symbols. This would be at first used as exploratory graphs, but this can also be used later on for publication ...

I would like to use the gg* libraries (ggplot2, ggtree, ggmap ...) to do that, because then it is easy to modify plots to display different variables. Here is a dummy script to describe how I do that so far. I do the tree and map plot separately and combine them. I want also to be able to have a common legend for the two plots. I am stuck after combining, I do not find out how to link the points from the tree plot to the points on the map plot with segments.

Anyone with ideas / possible solutions on how to do that or an alternative approach ?

Here is the dummy dataset to illustrate for creating the plots

library(patchwork)
library(ggpubr)
library(ggtree)
library(tidyverse)
library(ggmap)
library(ggplot2)

mytree <- ggtree::rtree(100)


mymap <- ggmap::get_map(c(left = 0.903, bottom = 44.56, right = 6.72, top = 49.38), 
                 scale = 4, maptype = "terrain",
                 source = "stamen",
                 color = "bw")

save(mymap, file = "dummy_map.Rdata")

ggmap require API key to create the map (sorry I cannot share the API key, but you can make one for free on google cloud). I saved the map object and its downloadable from here.

# Loading the map
load("dummy_map.Rdata")

# creating dummy metadata
mytree_data <- tidytree::as_tibble(mytree)

mymetadata <- mytree_data %>% 
    dplyr::filter(!is.na(label)) %>%
    tibble::add_column(year = sample(seq(1990, 2020, by = 1), 100, replace = T), 
               lon = sample(seq(0.91, 6.7, by = 0.01), 100, replace = T),
               lat = sample(seq(44.56, 49.38, by = 0.01), 100, replace = T)) %>%
    dplyr::rename(id = label) %>%
    dplyr::select(id, year, lat, lon) 

# plotting the phylogenetic tree
# phylogenetic tree example     
mytree_plot <- 
    ggtree::ggtree(mytree, layout = "rectangular", ladderize = T, lwd = .2) %<+%
    mymetadata +
    geom_tippoint(aes(color = year), size = 1, show.legend = T) +
    scale_color_gradient(low='red', high="blue", space = "Lab",
                         limits = c(NA, NA), na.value = "black",
                         n.breaks = 8,
                         guide = "colorbar") +
    geom_tiplab(aes(label = label), size = 1, offset = -1E-10) +    
    geom_treescale(fontsize = 2, linesize = 0.5, offset = 1) +
    theme(legend.position = c(0.9,0.15),
          legend.title = element_text(size = 8),
          legend.text = element_text(size = 6),
          plot.title = element_text(hjust = 1))

mytree_plot

For some reason, I have to add the theme to be able to see the legend for the points, it is not created automatically. This should not occur. If anyone see what I am doing wrong here please let me know.

Then I add the sampling locations on the map, and deactivate the legend that is common with the tree legend

mymap_plot <- ggmap(mymap, n_pix = 340, darken = c(0.6, "white"))+
    geom_point(data = mymetadata, 
               aes(x = lon, y = lat, color = year), 
               size = 2, alpha = .8, na.rm = T) +
    scale_color_gradient(low='red', high="blue", space = "Lab",
                         limits = c(NA, NA),
                         n.breaks = 8,
                         guide = "colorbar") +
    guides(color = F)
mymap_plot

Then I combine the tree plot and the map plot together. I tried with "patchwork" and "ggpubr" packages. So far it appear easier to combine plots and draw a single legend with ggpubr, so this is currently my first choice at combining plot

# combining plots with patchwork
combined_plot <- mytree_plot + mymap_plot 

# combining plots with ggpubr
# which I like better because it allows to combine the legends which is usefull 
# when more variables are used ie shape for uncertainty location
other_combined <- ggarrange(mytree_plot, mymap_plot,
                      ncol = 2,
                      labels = c("A", "B"),
                      align = "hv",
                      legend = "bottom",
                      common.legend = T)  

Here is the combined plot of the phylogenetic tree (ggtree) and the map (ggmap) obtained with ggpubr.

I am stuck at this point. I need a way to add segments between corresponding points at the tips of the tree to the corresponding sampling locations of each tip on the map

Any solutions/ideas on how I could do that?

Nova
  • 5,423
  • 2
  • 42
  • 62
Eve
  • 21
  • 1

0 Answers0