我需要幫助才能在情節中添加資訊。
這是情節:

(獲取下面這個圖的代碼)
我想添加一個新元素,其中包含存盤在此選項卡中的家庭資訊:
labels family
1 t2 Hominidae
2 t4 Hominidae
3 t3 Hominidae
4 t10 Equidae
5 t5 Equidae
6 t7 Muridae
7 t6 Muridae
8 t1 Crisetidae
9 t8 Crisetidae
10 t9 Outgroup
(輸入格式)
structure(list(labels = c("t2", "t4", "t3", "t10", "t5", "t7",
"t6", "t1", "t8", "t9"), family = c("Hominidae", "Hominidae",
"Hominidae", "Equidae", "Equidae", "Murinidae", "Muridae", "Crisetidae",
"Crisetidae", "Outgroup")), class = "data.frame", row.names = c(NA,
-10L))
并得到一個新的數字,如 ;

如您所見,我使用存盤在選項卡中的資訊添加豎線,后跟family每個labels.
我發現這篇文章相關(http://www.randigriffin.com/2017/05/11/primate-phylogeny-ggtree.html),但它不使用geom_bars,它只是在樹標簽旁邊添加家庭注釋......
library(ggplot2)
library(ggtree)
set.seed(2019-10-31)
tr <- rtree(10)
d1 <- data.frame(
# only some labels match
label = c(tr$tip.label[sample(9, 9)], "A"),
value = sample(1:10, 10))
d2 <- data.frame(
label = rep(tr$tip.label, 5),
category = rep(LETTERS[1:5], each=10),
value = rnorm(50, 0, 3))
g <- ggtree(tr) geom_tiplab(align=TRUE) hexpand(.01)
d2$label <- factor(d2$label, levels = rev(get_taxa_name(g)))
d1$label <- factor(d1$label, levels = rev(get_taxa_name(g)))
p1 <- ggplot(d1, aes(label, value)) geom_col(aes(fill=label))
geom_text(aes(label=label, y= value .1))
coord_flip() theme_tree2() theme(legend.position='none')
p2 <- ggplot(d2, aes(x=category, y=label))
geom_tile(aes(fill=value)) scale_fill_viridis_c()
theme_minimal() xlab(NULL) ylab(NULL) theme(legend.position='none')
cowplot::plot_grid(g, p2, p1, ncol=3)
uj5u.com熱心網友回復:
使用 aleft_join和構面:
d1 <- dplyr::left_join(d1, tab, by = c(label = "labels"))
p1 <- ggplot(d1, aes(label, value)) geom_col(aes(fill=label))
geom_text(aes(label=label, y= value .1))
coord_flip() theme_tree2() theme(legend.position='none')
facet_grid(family~., scales = "free_y", space = "free_y")
scale_x_discrete(expand = c(0, 0), position = "top")
theme(plot.margin = margin(10, 10, 10, 0),
axis.line.y.right = element_line(size = 3),
strip.background = element_blank(),
strip.placement = "outside")
p2 <- ggplot(d2, aes(x=category, y=label))
geom_tile(aes(fill=value))
scale_fill_viridis_c()
theme_minimal()
xlab(NULL)
ylab(NULL)
theme(legend.position='none')
cowplot::plot_grid(g, p2, p1, ncol=3)

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/462762.html
