如何将图例添加到 ggtree 的“facet_plot”中的一个方面?

How do I add a legend to a facet in ggtree's `facet_plot`?

我正在使用 facet_plot() 在我的树的尖端旁边添加一个带有特征值的条形图。我需要条形图的图例,但在 documentation or in 中找不到如何做到这一点。似乎 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())+ scale_fill_discrete(),但没有成功。

如何向 Trait 方面添加图例? (并且,扩展到任何其他方面?)

我们可以添加 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")