跟着Nature学作图:R语言ggridges包绘制山脊图

论文是

Environmental factors shaping the gut microbiome in a Dutch population

数据和代码的github主页链接

https://github.com/GRONINGEN-MICROBIOME-CENTRE/DMP

这个也是数据代码的下载链接,可以看目录结构

https://zenodo.org/record/5910709#.YmAcp4VBzic

今天的推文重复一下论文中的 figureS3a山脊图和S3b小提琴图加箱线图

image.png

因为论文中提供的不是真实数据集,是一个模拟数据集,所以出图和论文原图相差比较大

首先是山脊图的数据

image.png

读取数据集

dag3_species_plot<-readr::read_csv("newdataset/FigureS3a.csv")
head(dag3_species_plot)

赋予因子水平

主要是用来调节Y轴的顺序

dag3_species_plot$Level <-
  factor(dag3_species_plot$Level,
         levels = c("Bacteroides_vulgatus",
                    "Bacteroidales_bacterium_ph8",
                    "Alistipes_shahii",
                    "Sutterella_wadsworthensis",
                    "Alistipes_onderdonkii",
                    "Bacteroides_uniformis",
                    "Subdoligranulum_unclassified",
                    "Prevotella_copri",
                    "Faecalibacterium_prausnitzii",
                    "Alistipes_putredinis"))

对数值进行-log2转化

dag3_species_plot$Relative=-log2(dag3_species_plot$Relative)

生成配色

library(randomcoloR)

palette <- distinctColorPalette(10)

这里randomcoloR这个R包是第一次使用,主要作用是生成比较容易区分的颜色

作图

library(ggridges)
library(ggplot2)

ggplot(dag3_species_plot, 
            aes(x = Relative, y = Level,fill=Level,color=Level)) + 
  theme_classic() +
  geom_density_ridges(alpha=0.8,scale = 2,show.legend = FALSE)+
  scale_fill_manual(values = palette) +
  scale_color_manual(values = palette)+
  ylab("")+
  xlab("Norm. Rel. Abundance (-log2)")
image.png

这个图可能是因为数据分布的原因看起来不是很美观,重新构造一份数据集画个图

Level<-rep(c("Bacteroides_vulgatus",
             "Bacteroidales_bacterium_ph8",
             "Alistipes_shahii",
             "Sutterella_wadsworthensis",
             "Alistipes_onderdonkii",
             "Bacteroides_uniformis",
             "Subdoligranulum_unclassified",
             "Prevotella_copri",
             "Faecalibacterium_prausnitzii",
             "Alistipes_putredinis"),
           rep(2000,10))
library(tidyverse)
Relative<-10:19 %>% 
  map(function(x)rnorm(n=2000,mean=10,sd=x)) %>% unlist()

dat01<-data.frame(Level,Relative)
head(dat01)

dat01$Level<-
  factor(dat01$Level,
         levels = c("Bacteroides_vulgatus",
                    "Bacteroidales_bacterium_ph8",
                    "Alistipes_shahii",
                    "Sutterella_wadsworthensis",
                    "Alistipes_onderdonkii",
                    "Bacteroides_uniformis",
                    "Subdoligranulum_unclassified",
                    "Prevotella_copri",
                    "Faecalibacterium_prausnitzii",
                    "Alistipes_putredinis"))

ggplot(dat01, 
       aes(x = Relative, y = Level,fill=Level,color=Level)) + 
  theme_classic() +
  geom_density_ridges(alpha=0.8,scale = 2,show.legend = FALSE)+
  scale_fill_manual(values = palette) +
  scale_color_manual(values = palette)+
  ylab("")+
  xlab("Norm. Rel. Abundance (-log2)")+
  theme(axis.text.y = element_text(face="italic"),
        axis.text.x = element_text(face="bold"))
image.png

这个就看起来好看了很多

接下来是小提琴图

library(ggsci)
library(ggplot2)
cluster_plot<-readr::read_csv("newdataset/cluster_plot.csv",
                              col_types = "ccd")
g <- ggplot(cluster_plot, 
            aes(x=Cluster, y=Prevotella_copri,fill=Cluster)) + 
  geom_violin()+
  geom_boxplot(width=0.1, fill="white")+
  scale_fill_npg()+theme_classic()+
  ylab("Prevotella Copri, Norm. Rel. Abundance (-log2)")
print(g)
image.png

制作封面图


library(patchwork)
g+theme(legend.position = "none")+figureS3A
image.png

示例数据和代码可以 点赞 点击在看 然后后台回复20220519获取

欢迎大家关注我的公众号

小明的数据分析笔记本

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

你可能感兴趣的:(跟着Nature学作图:R语言ggridges包绘制山脊图)