跟着ISEM学作图:R语言ggtree+ggplot2组合进化树和气泡图

2022-05-23 15:39:36 浏览数 (1)

论文

Conserved and reproducible bacterial communities associate with extraradical hyphae of arbuscular mycorrhizal fungi

image.png

今天的推文我们来重复一下论文中的 Figure 2

image.png

没有找到论文提供的原始数据,这里数据我自己构造一份

首先是左侧的进化树文件

代码语言:javascript复制
(((A8:0.9735669859,((A5:0.7219205995,A9:0.5385038075):0.6462689626,A1:0.3329299772):0.03139768029):0.06015197909,A10:0.4923124262):0.4108513703,(((A6:0.8690173309,A4:0.5830903472):0.309844861,(A3:0.5314095169,A7:0.8771070722):0.7551963951):0.5753640204,A2:0.7064118343):0.2585936433);

给进化树准备一个分组文件

image.png

右侧的气泡图数据

image.png

数据没有实际意义是随便构造的,本期推文的示例数据和代码可以给推文打赏一元获得

首先是做进化树的代码

代码语言:javascript复制
library(ggtree)
library(treeio)
library(readxl)
library(tidyverse)

tree<-read.tree("ISEM/example.tree")
dat01<-read_excel("ISEM/dat01.xlsx")
tree<-full_join(tree,dat01,by="label")

ggtree(tree) 
  geom_tiplab(align = TRUE,
              fontface=3) 
  geom_tippoint(aes(color=groupinfo),
                show.legend = F,
                size=5) 
  scale_color_manual(values = c('group1'='#df237b',
                                'group2'='#11926a')) 
  xlim(0,3)-> p1

p1

image.png

然后是右侧气泡图的代码

代码语言:javascript复制
dat02<-read_excel("ISEM/dat02.xlsx")

dat02$species<-
  factor(dat02$species,
         labels = c('G. versiformenDryden',
                    'G. versiformenFlorence',
                    'G. versiformenPendleton',
                    'R. irregularisnDryden'))

dat02$tiplabel<-
  factor(dat02$tiplabel,
         levels = p1$data %>% na.omit() %>% arrange(y) %>% pull(label))

ggplot(data=dat02,aes(x=log2foldchange,y=tiplabel)) 
  geom_point(aes(size=`Mean relative abundance`,
                 fill=groupinfo),
             shape=21) 
  geom_vline(xintercept = 0,lty="dashed",color="grey") 
  facet_wrap(~species,nrow = 1,
             scales = 'free') 
  scale_fill_manual(values = c('group1'='#df237b',
                               'group2'='#11926a')) 
  guides(size=guide_legend(order = 1),
         fill=guide_legend(order = 2)) 
  theme_minimal() 
  theme(legend.position = 'bottom',
        legend.direction = 'vertical',
        legend.justification = c(0,0),
        axis.line.x = element_line(),
        axis.ticks.x = element_line(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        panel.grid = element_blank(),
        strip.background = element_rect(fill = "grey")) -> p2

p2

image.png

最后是拼图的代码

代码语言:javascript复制

library(patchwork)
help(package="patchwork")

p1 p2 
  plot_layout(widths = c(1,4))

image.png

这里拉丁文的斜体没有通过代码设置,出图后再编辑可能比较方便

0 人点赞