zl程序教程

您现在的位置是:首页 >  其他

当前栏目

跟着Nature Communications学作图:R语言ggplot2热图(heatmap)并添加文字标签

2023-02-19 12:27:46 时间

论文

MiDAS 4: A global catalogue of full-length 16S rRNA gene sequences and taxonomy for studies of bacterial communities in wastewater treatment plants

https://www.nature.com/articles/s41467-022-29438-7

数据链接

https://figshare.com/articles/dataset/Dueholm2021a_data_zip/16566408/1

代码链接

https://github.com/msdueholm/MiDAS4

今天的推文重复一下论文中的Figure4b ggplot2做热图并添加文本标签

image.png

论文中没有直接提供这个作图数据,需要运行一系列代码获得,这里我不介绍前面获取作图数据的代码了,感兴趣的可以自己去找来代码试试,如果运行的话需要比较大的内存

加载需要用到的R包

library(ggplot2)
library(readr)
library(tidyverse)
library(stringr)

部分示例数据集截图

image.png

用来调节因子水平的文本我也放到了一个文件里

image.png

作图代码

df1<-read_csv("fig4b1.csv")
df1$Genus<-factor(df1$Genus,
                  levels = readLines("fig4b1_levels.txt"))
fig4b1face<-ifelse(str_starts(readLines("fig4b1_levels.txt"),"m|CL|Ca"),
                   "plain","italic")
p1 <- ggplot(df1, aes(Amplicon, Genus)) +
  geom_tile(aes(fill = Abundance)) + 
  geom_text(aes(label = round(Abundance, 2))) +
  scale_fill_gradientn(colors = rev(
    c("#a50026","#d73027", "#f46d43", "#fdae61",
               "#fee090","#ffffbf","#e0f3f8",
               "#abd9e9","#74add1","#4575b4","#313695")),
               trans = "log10",
    breaks = c(0.001, 0.01, 0.1, 1), 
    limits=c(0.001,1),
    name="Relative\nabundance(%)") +
  theme_bw()+
  labs(x=NULL,y=NULL)+
  theme(axis.text.y = element_text(face=fig4b1face))
p1

image.png

这里一个小知识点是y轴的文本标签有斜体有正常字体,这个是可以设置theme(axis.text.y = element_text(face=fig4b1face))face的值是可以设置多个的,但是会有警告信息,可以忽略

第二个图的示例数据和代码都是一样的

df2<-read_csv("fig4b2.csv")


df2$Genus <-factor(df2$Genus,
                   levels = readLines("fig4b2_levels.txt"))
fig4b2face<-ifelse(str_starts(readLines("fig4b2_levels.txt"),"mi|Ag|BD|SH"),
                   "plain","italic")

p2 <- ggplot(df2, aes(Amplicon, Genus)) +
  geom_tile(aes(fill = Abundance)) + 
  geom_text(aes(label = round(Abundance, 2))) +
  scale_fill_gradientn(colors = rev(
    c("#a50026", "#d73027", "#f46d43", "#fdae61",
               "#fee090","#ffffbf","#e0f3f8","#abd9e9","#74add1",
               "#4575b4","#313695")), 
               trans = "log10",, 
    breaks = c(0.001, 0.01, 0.1, 1),
    limits=c(0.001,1),
    name="Relative\nabundance(%)") +
  theme_bw()+
  labs(x=NULL,y=NULL)+
  theme(axis.text.y = element_text(face=fig4b2face))
p2

image.png

最后是拼图

library(patchwork)

p1+
  coord_flip()+
  theme(axis.text.x = element_text(angle=60,
                                   vjust=1,hjust=1),
        legend.position = "top")+
  guides(fill=guide_legend(title.position = "top"))-> p1.1
p2+
  coord_flip()+
  theme(axis.text.x = element_text(angle=60,
                                   vjust=1,hjust=1),
        legend.position = "top") +
  guides(fill=guide_legend(title.position = "top"))-> p2.1
p1.1/p2.1 +  plot_layout(guides = "collect")+
  plot_annotation(theme = theme(legend.position = "top",
                                legend.direction = "horizontal"))