跟着Nature学作图:R语言ggplot2+ggtree树图组合热图

2024-02-03 15:19:21 浏览数 (1)

论文

Genome evolution and diversity of wild and cultivated potatoes

今天的推文复现一下论文中的figure3c

主要的知识点是:

1、进化树中挑选子集 2、进化树默认是左下角到右上角这种布局,如何调整成左上到右下角这种布局 3、进化树把某个clade压缩成三角性状 4、给进化树添加根小尾巴

进化树的代码

代码语言:javascript复制
library(readxl)
library(tidyverse)
library(ggtree)
read_excel("D:/Jupyter/土豆Nature/41586_2022_4822_MOESM7_ESM.xlsx",
           sheet = "Fig. 3c_2",
           skip = 1,
           col_names = FALSE) %>% 
  pull(1) %>% 
  read.tree(text = .) -> tree

ggtree(tree) 
  geom_tiplab()

tree$tip.label
keep.tips<-c("Heinz","PG0009","PG0019",
             "PG1011","PG6241","PG3022",
             "PG5068","PG6002","PG6148",
             "PG6247")

to_drop<-tree$tip.label[-match(keep.tips,tree$tip.label)]
reduced.tree<-treeio::drop.tip(tree,to_drop)

p<-ggtree(reduced.tree,
          branch.length = "none",)
p 
  geom_tiplab()

## 这个代码是给根添加一个小尾巴
## rootedge = 1 这个1是控制小尾巴的长度
p 
  geom_rootedge(rootedge = 1)

## 把指定节点进行旋转
## 把树改成左上角到右下角的形式
new.p<-ggtree::rotate(p,11)
new.p geom_tiplab()

ggtree(reduced.tree) 
  geom_tiplab() 
  geom_nodelab(aes(label=node))

p1<-collapse(new.p,node = 13,"max",fill="white",color="black") %>% 
  collapse(node=14,"max",fill="white",color="black") 

p1

热图代码

代码语言:javascript复制
p2<-read_excel("D:/Jupyter/土豆Nature/41586_2022_4822_MOESM7_ESM.xlsx",
           sheet = "Fig. 3c_1",
           skip = 1,
           col_names = TRUE,
           na="NA") %>% 
  rename("sample_id"="...1") %>% 
  mutate(sample_id=factor(sample_id,levels = rev(sample_id))) %>% 
  pivot_longer(!sample_id) %>% 
  mutate(new_value=scale(value),
         name=factor(name,levels = c("Root","Stem","Leaf","Stolon/Rhizome","Tuber"))) %>% 
  ggplot(aes(x=name,y=sample_id)) 
  geom_tile(aes(fill=new_value),
            color="gray") 
  scale_fill_gradient(low="white",high = "red",
                      name="") 
  theme_bw(base_size = 15) 
  theme(panel.grid = element_blank(),
        axis.text.x = element_text(angle=60,hjust=0,vjust=0),
        axis.ticks = element_blank()) 
  scale_x_discrete(expand = c(0,0),position = "top") 
  scale_y_discrete(expand = c(0,0)) 
  labs(x=NULL,y=NULL)
p2

两个图组合到一起

代码语言:javascript复制
library(patchwork)
p1 p2 
  plot_layout(widths = c(1,10))

0 人点赞