R IN ACTION SELF-TUTORIAL-62 作出散点图并显示拟合曲线和公式,添加标注文字(重现NBT-血液代谢组学预测人类肠道菌群α-多样性) 2021-02-05

image.png

文献名称: Blood metabolome predicts gut microbiome α-diversity in humans

文献译名:血液代谢组学预测人类肠道菌群α-多样性

Nature Biotechnology: volume 37, pages1217–1228(2019)
  • 文献链接:https://nature.com/articles/s41587-019-0233-9
  • 原图链接: https://www.nature.com/articles/s41587-019-0233-9/figures/1
image.png

1.数据准备与读取:

  • 数据格式:

image.png
  • 读取excel表里的数据

library(readxl)
DF<-as.data.frame(read_xlsx(file.choose()))
head(DF)
image.png

2.作图与修饰:

  • 作出基本散点图:

library(ggplot2)
p1 <- ggplot(data = DF, aes(x = DF$`mShannon diversity`, y = DF$`Observed Shannon diversity`)) + geom_point() 
p1
image.png
  • 添加拟合线:

p3<-p1+geom_smooth(method = "lm", se=FALSE, color="red", formula = y ~ x) 
p3
image.png
  • 去掉灰色背景:

p4<-p3+  theme_bw()
p4
image.png
  • 去掉大小网格线:

p5<-p4+theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), axis.line = element_line(colour = "black"))
p5
image.png
  • 去掉框架只剩下坐标轴:

p6<-p5+ theme(panel.border = element_blank())
p6
image.png
  • 添加坐标轴标签:

p7<-p6+ggtitle("a") + xlab("mShannon diversity") + ylab("Observed Shannon diversity")
p7
image.png
  • 添加图面上的文字注释:

  • 备注:平方的标注为 label=expression(R^{2})
p8<-p7+annotate(geom="text", x=2.361, y=5.4, label= "Out-of-sample ")+annotate(geom="text", x=2.734, y=5.4, label=expression(R^{2}))+annotate(geom="text", x=2.93, y=5.4, label= "= 0.45") +annotate(geom="text", x=2.5,y=5.1, label= " Pearson’s  r = 0.68")+annotate(geom="text", x=2.461, y=4.7, label= "P = 3.21 × ")+annotate(geom="text", x=2.74, y=4.754, label=expression(10^{-56})) 
p8
image.png
  • 对坐标轴刻度值进行调整:

p9<-p8+scale_x_continuous( breaks = seq(2,5.5,by=0.5))+scale_y_continuous( breaks = seq(2,5.5,by=0.5) )
p9
image.png

3. 保存与输出:

  • 以制定大小输出和保存:
ggsave(plot = p9, width =8, height = 6, dpi = 300, filename = "C:/Users/Mr.R/Desktop/Fig1_a.jpg")

4. 完整的代码为:

library(ggplot2)
p1 <- ggplot(data = DF, aes(x = DF$`mShannon diversity`, y = DF$`Observed Shannon diversity`)) + geom_point() 
p1

p3<-p1+geom_smooth(method = "lm", se=FALSE, color="red", formula = y ~ x) 
p3

p4<-p3+  theme_bw()
p4
p5<-p4+theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), axis.line = element_line(colour = "black"))
p5
p6<-p5+ theme(panel.border = element_blank())
p6
p7<-p6+ggtitle("a") + xlab("mShannon diversity") + ylab("Observed Shannon diversity")
p7

p8<-p7+annotate(geom="text", x=2.361, y=5.4, label= "Out-of-sample ")+annotate(geom="text", x=2.734, y=5.4, label=expression(R^{2}))+annotate(geom="text", x=2.93, y=5.4, label= "= 0.45") +annotate(geom="text", x=2.5,y=5.1, label= " Pearson’s  r = 0.68")+annotate(geom="text", x=2.461, y=4.7, label= "P = 3.21 × ")+annotate(geom="text", x=2.74, y=4.754, label=expression(10^{-56})) 
p8

p9<-p8+scale_x_continuous( breaks = seq(2,5.5,by=0.5))+scale_y_continuous( breaks = seq(2,5.5,by=0.5) )
image.png

你可能感兴趣的:(R IN ACTION SELF-TUTORIAL-62 作出散点图并显示拟合曲线和公式,添加标注文字(重现NBT-血液代谢组学预测人类肠道菌群α-多样性) 2021-02-05)