【问题标题】:How can I get ggtree::geom_range to work?我怎样才能让 ggtree::geom_range 工作?
【发布时间】:2021-01-22 19:40:42
【问题描述】:

我正在尝试向节点添加范围栏,如 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。

【问题讨论】:

    标签: r ggtree


    【解决方案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()
    

    【讨论】:

      猜你喜欢
      • 2011-04-26
      • 1970-01-01
      • 2013-07-22
      • 2013-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-24
      相关资源
      最近更新 更多