2

facet_plot()用来在树的尖端旁边添加一个带有特征值的条形图。我需要有条形图的图例,但在文档类似问题中找不到如何做到这一点。这似乎facet_plot()使这有点棘手。

这是我的代码:

library(ggtree)
library(tidyverse)
library(ggstance) # for horizontal versions of geoms

# create some random tree and trait data
tree <- rtree(5)
traits <- tibble(
  node  = paste0("t", rep(1:5, 4)),
  trait = rep(LETTERS[1:4], 5),
  value = rnorm(n = 20, mean = 10, sd = 2))

# tree plot with barplot facet
treeplot <- ggtree(tree) + geom_tiplab(align = T)
facet_plot(treeplot,
           panel = "Trait",
           data = traits,
           geom = geom_barh,
           mapping = aes(x = value, fill = trait),
           stat = "identity")

特征数据映射到提示的随机树

我尝试添加+ guides(fill = guide_legend())or + scale_fill_discrete(),但无济于事。

如何将图例添加到 Trait facet?(而且,延伸到任何其他方面?)

4

1 回答 1

2

我们可以添加theme(legend.position="bottom")以获得所需的情节。

facet_plot(treeplot,
           panel = "Trait",
           data = traits,
           geom = geom_barh,
           mapping = aes(x = value, fill = trait),
           stat = "identity", show.legend = TRUE) +
  theme(legend.position = "bottom")

在此处输入图像描述

于 2019-06-27T12:02:37.197 回答