跟着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"))
相关文章
- php 伪静态-服务器伪静态和WordPress固定连接设置
- Python:Django框架
- 基于Django的博客开发
- PowerPhotos for Mac(mac专用图片管理工具)
- windows命令行筛选-windows下使用命令行运行PHP
- c 按照位数读取一行-C++习题 倒置排序 OpenJudge
- 笔记本怎样连接投影仪-linux(ubuntu)下外接投影仪或着显示器的方法
- 【毕业设计】单片机红外热成像仪 - 嵌入式 stm32 树莓派
- vc60修改快捷键-如何解决Visual c++不能打开文件以及工程不能添加文件的问题
- c 线程安全的单例模式-设计模式之单例模式(C++版)
- c 线程安全的单例模式-C++单例模式(线程安全、内存释放)
- 嵌入式:ARM相关开发工具概述
- 嵌入式:ARM符号定义伪操作详解
- c 线程安全的单例模式-详解C++实现线程安全的单例模式
- C++ OpenCV手动截取图像做透视变换
- 超简单的pyTorch训练->onnx模型->C++ OpenCV DNN推理(附源码地址)
- pyTorch入门(三)——GoogleNet和ResNet训练
- pyTorch入门(四)——导出Minist模型,C++ OpenCV DNN进行识别
- Github Actions Hexo 自动部署
- c 线程安全的单例模式-std string与线程安全_这才是现代C++单例模式简单又安全的实现