将族注释添加到 r 中 geom_bar 旁边的 ggtree

芯片中心

我需要帮助才能在情节中添加信息。

这是情节:

在此处输入图像描述

(获取下面这个图的代码)

我想添加一个新元素,其中包含存储在此选项卡中的家庭信息:

   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) 
艾伦·卡梅伦

使用 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) 

在此处输入图像描述

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将图例添加到ggtree(ggplot)R中

使用R / ggplot2将标签添加到geom_bar()中的各个%

如何使用 ggplot / R 将 geom_bar 添加到该图中?

在 R 中的 ggplot、geom_bar 图表底部添加统计表

将组均值线添加到geom_bar图并包括在图例中

R:在 geom_bar(position = 'fill') 中显示值

从geom_bar图例ggplot r中删除点

R:更改geom_bar中的条形标签

R ggtree根据分支长度注释节点

使用共享的y轴将ggtree对象添加到已存在的ggplot中

您可以在R中的geom_point图上组合geom_bar饼图吗?

R ggtree:如何使用ggtree标记单个树梢,类似于使用geom_cladelabel标记节点

重新排列facet_grid并将其添加到ggplot2的geom_bar中

用R中的ggplot2更改geom_bar中的条形图颜色

ggplot R中geom_bar中离散值的自定义颜色

R:堆叠的geom_bar(ggplot)中每个X轴元素的多列

在ggplot2 r中操纵geom_bar和coord_polar的描述

R geom_bar图例中的“ N = 1”框是什么,如何删除?

如何使用geom_bar()在R中创建两个分组列

在ggplot2中的geom_segment旁边添加文本

R geom_bar:如何将 Y 轴标题返回到我的图表?

在带有面的geom_bar中的列上方添加百分比

geom_bar 中的条不等宽

jQuery-将属性添加到已知类旁边的<div>中

将QR码中的文字添加到生成的PNG旁边

将geom_line添加到R中的堆叠条形图

通过R中的geom_text将小于符号“ <”添加到ggplot

将图例添加到R中的geom_line()图

如何使用ggplot2在R中的每个条形图上在geom_bar上放置标签