c语言sscanf函数的用法是什么
312
2022-09-21
跟着Nature学作图:R语言ggridges包绘制山脊图
论文是
Environmental factors shaping the gut microbiome in a Dutch population
数据和代码的github主页链接
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
小明的数据分析笔记本
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~