跟着Nature Communications学作图:R语言ggplot2散点图及添加文字标签

论文

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

今天的推文重复一下论文中的Figure4a

image.png

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

作图数据部分截图

image.png

读取数据

library(ggrepel)
library(tidyverse)
library(ggplot2)
library(ggtext)
library(ggrepel)

gV134<-read_csv("fig4a.csv")

作图代码

p <- ggplot(gV134, 
            aes_string(x = "V13_mean_abundance", 
                       y= "V4_mean_abundance", 
                       fill="Bias"),
            color="black") + 
  geom_point(size = 2,shape=21) +
  geom_abline(size = 0.5) +
  geom_text_repel(data = 
              filter(gV134, 
                     gV134$FC>1 & gV134$V4_mean_abundance>0.1), 
            aes(label = Genus), 
            #vjust = 1.5, 
            colour="grey30") +
  geom_text_repel(data = 
              filter(gV134, 
                     gV134$FC<(-1) & gV134$V13_mean_abundance>0.1), 
            aes(label = Genus), 
            #vjust = 1.5, 
            colour="grey30") +            
  xlab("Mean V13 amplicon genus read abundance (%)") + 
  ylab("Mean V4 amplicon genus read abundance (%)") +
  scale_x_log10(limits=c(0.001,2)) +
  scale_y_log10(limits=c(0.001,2)) +
  theme_bw() +
  theme(legend.position = "none",
        plot.title = element_markdown())+
  scale_fill_manual(values = c('Equally detected'="#898989",
                                'More abundant with V1-V3'="#2a7cb6",
                                'More abundant with V4'="#d41b21"))+
  labs(title="352 genera equally abundant (gray)
112 genera more abundant with V4 (red)
111 genera more abundant with V1-V3 (blue)") p
image.png

这里遇到的问题是添加的文本标签有点多,彼此之间会有重叠,使用ggrepel这个R包也调节不出比较好的效果,只能出图后再编辑图片了

这里标题的文本只有一部分添加了颜色,可以借助ggtext这个R包的markdown语法实现

示例数据和代码可以自己到论文中下载

欢迎大家关注我的公众号

小明的数据分析笔记本

小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!

你可能感兴趣的:(跟着Nature Communications学作图:R语言ggplot2散点图及添加文字标签)