簡體   English   中英

如何將圖例添加到 ggtree 的`facet_plot` 中的構面?

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

我正在使用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())+ 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")

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM