前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >跟着Nature Communications学作图:R语言ggplot2平滑曲线折线图

跟着Nature Communications学作图:R语言ggplot2平滑曲线折线图

作者头像
用户7010445
发布2023-01-06 18:39:06
2K0
发布2023-01-06 18:39:06
举报

论文

A highly conserved core bacterial microbiota with nitrogen-fixation capacity inhabits the xylem sap in maize plants

https://www.nature.com/articles/s41467-022-31113-w

本地pdf s41467-022-31113-w.pdf

数据代码链接

https://github.com/PlantNutrition/Liyu

今天的推文我们重复一下论文中的Figure2e

image.png

关于平滑曲线,之前的推文有过介绍,可以参考

我们看下这个论文里是用什么代码来实现的

示例数据集部分截图

image.png

读取数据

代码语言:javascript
复制
top_genus <- read.delim("data/20220616/top_genus.txt",
                       header=T, 
                       row.names=1, 
                       sep="\t",
                       stringsAsFactors = FALSE,
                       check.names = FALSE)

赋予因子水平

代码语言:javascript
复制
top_genus$Genus<-factor(
  top_genus$Genus,
  levels=c("Bacillus","Cronobacter",
           "Unclassified_Enterobacterales",
           "Klebsiella","Pantoea",
           "Pseudomonas","Rosenbergiella"), 
  labels = c("Bacillus","Cronobacter",
             "Unclassified_Enterobacterales",
             "Klebsiella","Pantoea",
             "Pseudomonas","Rosenbergiella"))

准备配色

代码语言:javascript
复制
phy.cols <- c("#85BA8F", "#A3C8DC",
              "#349839","#EA5D2D",
              "#EABB77","#F09594","#2072A8")

普通折线图的代码

代码语言:javascript
复制
library(ggplot2)
p=ggplot(data=top_genus,
         aes(x=Compartment,y=RA,
             group=Genus,colour=Genus))+
  geom_point(size=3)+
  labs(x="Compartments", y="Relative abundance (%)")+
  geom_line()+
  scale_x_discrete(limits=c("RS","RE","VE","SE","LE","P","BS"))+
  scale_colour_manual(values=phy.cols) 
p

image.png

论文中提供的代码没有用BS的数据,所以会有警告信息

image.png

平滑曲线他用到的是 ggalt包中的 geom_xspline函数

代码语言:javascript
复制
library(ggalt)
p2<-ggplot(data=top_genus,aes(x=Compartment,y=RA,
                              group=Genus,colour=Genus))+
  geom_point(size=3)+
  labs(x="Compartments", y="Relative abundance (%)")+
  geom_xspline(spline_shape = -0.5)+
  scale_x_discrete(limits=c("RS","RE","VE","SE","LE","P"))+
  scale_colour_manual(values=phy.cols) 
p2

image.png

拼图

代码语言:javascript
复制
mytheme = theme_bw() +
  theme(axis.text.x = element_text(size = 8),axis.text.y = element_text(size = 8))+
  theme(axis.title.y= element_text(size=12))+theme(axis.title.x = element_text(size = 12))+
  theme(legend.title=element_text(size=5),legend.text=element_text(size=5))+theme(legend.position = "bottom")


library(patchwork)

p+mytheme + p2 + mytheme +
  plot_layout(guides = "collect")+
  plot_annotation(theme = theme(legend.position = "bottom"))

image.png

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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-06-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小明的数据分析笔记本 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 论文
  • 数据代码链接
  • 示例数据集部分截图
  • 读取数据
  • 赋予因子水平
  • 准备配色
  • 普通折线图的代码
  • 拼图
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档