Как добавить легенду к фасету в ggtree `facet_plot`? - PullRequest
1 голос
/ 27 июня 2019

Я использую 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")

Random tree with trait data mapped to tips

Я пытался добавить + guides(fill = guide_legend()) или + scale_fill_discrete(), но безрезультатно.

Как я могу добавить легенду к аспекту Черты? (И, в дополнение, к любому дополнительному аспекту?)

1 Ответ

1 голос
/ 27 июня 2019

Мы можем добавить 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")

enter image description here

...