跟着BMC genomics学作图:R语言ggplot2+ggt
2021-10-17 本文已影响0人
小明的数据分析笔记本

有读者在公众号留言问这个图的实现办法,今天的推文介绍一下这个图的R语言实现代码
这个图示物种的进化树,热图展示的是kaks的值
首先是进化树的数据

nwk格式的树文件
还有一个物种的分组文件

加载用到的R包
library(ggtree)
library(treeio)
library(readxl)
library(ggplot2)
library(tidyverse)
library(patchwork)
library(see)
左侧进化树作图
tree<-read.tree("sample.nwk")
tip.group<-read.csv("sample_group.csv")
tip.group
tree.a<-full_join(tree,tip.group,by="label")
as_tibble(tree.a) %>%
as.data.frame()
p1<-ggtree(tree.a,
branch.length = "none",
aes(color=group))+
geom_tiplab(offset = 0.1)+
theme(legend.position = "none")+
scale_y_reverse()+
scale_color_manual(values = c("#cc340c",
"#13a983",
"#3f60aa"),
na.value="black")
p1

热图数据

热图作图代码
df<-read_excel("example.xlsx")
df %>% pivot_longer(!sample,
names_to = "sample1",
values_to = "kaks") -> df1
df1$sample<-factor(df1$sample,
levels = rev(df$sample))
df1$sample1<-factor(df1$sample1,
levels = df$sample)
p2<-ggplot(data=na.omit(df1),aes(x=sample,y=sample1))+
geom_tile(aes(fill=kaks),color="grey")+
scale_fill_bluebrown_c()
p2
df1 %>%
filter(sample == sample1) -> df2
p2+
geom_tile(data=df2,aes(x=sample,
y=sample1),
fill="white",color="grey")+
#theme_void() +
theme(axis.text = element_text())-> p2.1
p2.2<-p2.1+theme(axis.title = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
panel.background = element_blank(),
legend.position = c(0.9,0.8))
p2.2

下侧进化树作图
p3<-ggtree(tree.a,
branch.length = "none",
aes(color=group))+
#geom_tiplab()+
theme(legend.position = "none")+
#scale_y_reverse()+
coord_flip()+
scale_color_manual(values = c("#cc340c",
"#13a983",
"#3f60aa"),
na.value="black")
p3

最后是拼图
(p1+xlim(0,8)+p2.2+
plot_spacer()+p3)+
plot_layout(ncol=2,
widths = c(0.5,3),
heights = c(3,0.5))

欢迎大家关注我的公众号
小明的数据分析笔记本
小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!
这里新学到的一个知识点是拼图的时候可以使用plot_spacer()
函数占据一个空白位置