我正在尝试做一些类似于博客 here 中描述的事情,但是将 R 与 ggtree、ggmap 和 ggplot2 一起使用。
我希望能够将系统发育树的图和显示地理地图上提示的采样位置的地图结合起来,并按段将提示链接到采样位置。那将允许看到即。如果某些集群出现在特定的地理位置(即某个区域的北部、南部),并且还允许显示带有提示颜色/符号的不同数据。这将首先用作探索性图表,但这也可以在以后用于发布......
我想使用 gg* 库(ggplot2、ggtree、ggmap ...)来做到这一点,因为这样很容易修改绘图以显示不同的变量。这是一个虚拟脚本,用于描述到目前为止我是如何做到的。我分别绘制树图和地图图并将它们组合起来。我还希望能够为这两个情节提供一个共同的图例。合并后我卡住了,我不知道如何将树状图中的点链接到带有线段的地图上的点。
任何人有关于如何做到这一点或替代方法的想法/可能的解决方案?
这里是用于说明创建图的虚拟数据集
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 需要 API 密钥来创建地图(抱歉,我不能分享 API 密钥,但你可以在谷歌云上免费制作一个)。我保存了地图对象及其可从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
出于某种原因,我必须添加主题才能看到点的图例,它不是自动创建的。这不应该发生。如果有人看到我在这里做错了什么,请告诉我。
然后我在地图上添加采样位置,并停用与树图例相同的图例
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
然后我将树图和地图图结合在一起。我尝试使用“patchwork”和“ggpubr”包。 到目前为止,用 ggpubr 组合绘图和绘制单个图例似乎更容易,所以这是我目前组合绘图的首选
# 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)
我卡在这一点上了。 我需要一种方法来将树尖端的相应点之间的线段添加到地图上每个尖端的相应采样位置
关于我该怎么做的任何解决方案/想法?
另一个名为 phylogeo 的 R 包可能有助于结合系统发育树和采样位置图。 https://academic.oup.com/bioinformatics/article/31/17/2909/183151