跟着Nature Communications学作图:R语言ggplot2画流程图

论文

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

今天的推文我们来复现一下论文中的Figure2B

image.png

当然这个图用ppt很容易实现。用ggplot2来做会稍微繁琐一点,但是后续多图组合会方便很多

流程图就是文本和箭头的组合,可以借助geom_text()函数和geom_segment()函数来做

首先是文本标签

library(ggplot2)

ggplot()+
  geom_label(aes(x=0,y=0),label="  VE  ",
             color="white",
             fontface="bold",
             fill="#547e34",
             size=5,
             label.size = 0)
image.png

这里有个问题是背景颜色条和文本的宽度是一致的,如何单独调整背景的颜色条的宽度暂时想不到办法了,我的办法是给文本前后加空格

https://stackoverflow.com/questions/48195587/how-to-set-a-standard-label-size-in-ggplots-geom-label

这个链接里重新自己定义了一个函数,关于自己写ggplot2的绘图函数我还看不明白

添加箭头

ggplot()+
  geom_label(aes(x=0,y=0),label="  VE  ",
             color="white",
             fontface="bold",
             fill="#547e34",
             size=5,
             label.size = 0)+
  geom_label(aes(x=1,y=0),label="  SE  ",
             color="white",
             fontface="bold",
             fill="#547e34",
             size=5,
             label.size = 0)+
  geom_label(aes(x=0.5,y=1),label="  LE  ",
             color="white",
             fontface="bold",
             fill="#a4c98a",
             size=5,
             label.size = 0)+
  geom_label(aes(x=0.5,y=2),label="  P  ",
             color="white",
             fontface="bold",
             fill="#a4c98a",
             size=5,
             label.size = 0)+
  geom_label(aes(x=0.5,y=-1),label="  RE  ",
             color="white",
             fontface="bold",
             fill="#b9860c",
             size=5,
             label.size = 0)+
  geom_label(aes(x=-0.4,y=-1),label="  RS  ",
             color="white",
             fontface="bold",
             fill="#8b814b",
             size=5,
             label.size = 0)+
  geom_label(aes(x=-1.2,y=-1),label="  BS  ",
             color="white",
             fontface="bold",
             fill="#8b814b",
             size=5,
             label.size = 0)+
  xlim(-1.5,1.5)+
  ylim(-1.5,2.2)+
  geom_segment(aes(x=0.2,xend=0.8,y=0.05,yend=0.05),
               arrow = arrow(angle=30,type = "closed",
                             length = unit(3,'mm')),
               color="#516896")+
  geom_segment(aes(x=0.8,xend=0.2,y=-0.05,yend=-0.05),
               arrow = arrow(angle=30,type = "closed",
                             length = unit(3,'mm')),
               color="#516896")+
  geom_segment(aes(x=0,xend=0.5,y=0.1,yend=0.9),
               arrow = arrow(angle=30,type = "closed",
                             length = unit(3,'mm')),
               color="#516896")+
  geom_segment(aes(x=0.4,xend=-0.1,y=0.9,yend=0.1),
               arrow = arrow(angle=30,type = "closed",
                             length = unit(3,'mm')),
               color="#516896")+
  geom_segment(aes(x=1.1,xend=0.6,y=0.1,yend=0.9),
               arrow = arrow(angle=30,type = "closed",
                             length = unit(3,'mm')),
               color="#516896")+
  geom_segment(aes(x=0.5,xend=1,y=0.9,yend=0.1),
               arrow = arrow(angle=30,type = "closed",
                             length = unit(3,'mm')),
               color="#516896") -> p1
p1
image.png

https://cran.r-project.org/web/packages/ggarchery/readme/README.html

添加箭头有一个专门的R包

这样一个一个添加太麻烦,可以把数据整理好

new.dat.01<-data.frame(
  x=   c(0,0.4,1,0.5,0.5,-1.2,-1,-0.2),
  xend=c(0.5,-0.1,0.6,0.9,0.5,0.3,-0.6,0.3),
  y   =c(-0.1,-0.9,-0.1,-0.9,1.9,2,-1,-1),
  yend=c(-0.9,-0.1,-0.9,-0.1,1.1,2,-1,-1))

p1 +
  geom_segment(data=new.dat.01,
               aes(x=x,xend=xend,y=y,yend=yend),
               arrow = arrow(angle=30,type = "closed",
                             length = unit(3,'mm')),
               color="#516896")+
  geom_segment(aes(x=-1.2,xend=-1.2,y=-0.9,yend=2),
               color="#516896")-> p2

p2

image.png

添加数字文本

new.data.02<-data.frame(
  x=c(-0.8,0,-0.5,0.7,-0.9,0.3,1.1,0.5),
  y=c(-0.9,-0.9,2.1,1.5,0.5,0.3,0.4,0.5),
  label=c(93.68,60.61,63.53,88.84,27.06,9.15,1.91,45.61)
)

p2 + 
  geom_text(data = new.data.02,
            aes(x=x,y=y,label=paste0(label,"%"))) -> p3

p3
image.png

拼图

library(patchwork)

p2 + 
  p3+ theme_void()
image.png

结论

好像确实有点繁琐,如果是真实数据不如直接用ppt画,然后借助其他软件拼图划算。如果是学习ggplot2可以尝试

数据和代码直接在推文里复制就可以,如果是学习ggplot2可以自己亲自敲一遍试试

欢迎大家关注我的公众号

小明的数据分析笔记本

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

版权声明:
作者:倾城
链接:https://www.techfm.club/p/141841.html
来源:TechFM
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
149
< <上一篇
下一篇>>