1

我正在尝试按照treedata book中的描述向节点添加范围栏。这是我想要实现的书中的一个例子:

带酒吧的树

注意红条。

这是应该创建带有红色条的树图像的代码:

library(tidyverse)
library(treeio)
library(ggtree)

# create a tree and add a numeric annotation called 'range'
tree = rtree(3) %>% as.treedata %>% as_tibble %>% 
  mutate(range=0.1) %>% 
  as.treedata

# plot the tree and add red bars with geom_bar()
ggtree(tree)  + geom_range(range='range', color="red")

没有栏杆的树

但是,生成的图没有红条。

为了给每个节点添加红条,我需要做什么?

我正在使用 ggtree v2.2.4、treeio v1.12.0 和 ggplot2 v3.3.2。

4

1 回答 1

1

您只给range. 在您链接的示例中,该列range是一个列表,其中每一行包含一个最小值和最大值。所以你可能想要这样的东西:

library(tidyverse)
library(treeio)
library(ggtree)

# create a tree and add a numeric annotation called 'range'
tree = rtree(3) %>% as.treedata %>% as_tibble %>% 
  mutate(number = 1:5,
        range = lapply(number, function(x) c(-0.1, 0.1) + x)) %>% 
  as.treedata

# plot the tree and add red bars with geom_bar()
ggtree(tree)  + 
  geom_range("number", range='range', color="red", size = 3, alpha = 0.3) + 
  theme_tree2()

在此处输入图像描述

于 2020-10-07T20:12:53.727 回答