前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >跟着Nature Plants学作图:R语言ggplot2分组散点图添加置信区间/图片上插入表格

跟着Nature Plants学作图:R语言ggplot2分组散点图添加置信区间/图片上插入表格

作者头像
用户7010445
发布2024-04-03 20:32:42
1170
发布2024-04-03 20:32:42
举报

论文

Heritable microbiome variation is correlated with source environment in locally adapted maize varieties

https://www.nature.com/articles/s41477-024-01654-7

论文中提供了作图用到的原始数据,我们可以试着用原始数据复现论文中的图,今天的推文我们来复现一下论文中的Figure1a

image.png

今天推文比较重要的知识点是

  • 分组添加置信椭圆
  • 在图片上插入表格

插入表格主要想实现上图文字的效果:插入多行文字,有的文字是常规字体,有的文字带斜体上下标这种

参考这个链接 https://cran.r-project.org/web/packages/gridExtra/vignettes/tableGrob.html

部分示例数据

image.png

加载需要用到的R包

代码语言:javascript
复制
library(readxl)
library(tidyverse)
library(gridExtra)

gridExtra用来在图片上插入表格

读取数据

代码语言:javascript
复制
read_excel("D:/R_4_1_0_working_directory/env001/data/20240322/41477_2024_1654_MOESM4_ESM.xlsx",
           sheet = "Fig1a") -> fig1a.dat

作图代码

代码语言:javascript
复制
fig1a.dat %>% 
  filter(Compartment=="Rhizosphere") %>% 
  ggplot(aes(x=CAP1,y=CAP2))+
  geom_point(aes(fill=Treatment),
             shape=21,size=5,
             color="gray",
             alpha=0.8)+
  stat_ellipse(aes(group = Treatment), 
               size = 0.5, 
               level = 0.95,
               show.legend = F,
               lty="dashed")+
  labs(x="CAP1 (22.6%)",y="CAP2 (6.8%)")+
  theme_bw(base_size = 15)+
  theme(panel.grid = element_blank(),
        panel.border = element_blank(),
        axis.line = element_line())+
  annotate(geom="text",x=-0.2,y=0.3,
           label="Rhizosphere bacteria",
           size=5,hjust=0)-> plot.a1
plot.a1

image.png

(论文中的图的配色不是很好看,这里就不改配色了)

添加表格

代码语言:javascript
复制
plot.a1 +
  annotation_custom(tableGrob(mytable,rows = NULL,cols = NULL,
                              theme = ttheme_minimal(core=list(fg_params=list(hjust=0,
                                                                              parse=TRUE,
                                                                              x=0,
                                                                              fontsize=15)))),
                    xmin=0.25, xmax=0.3, ymin=0, ymax=0.3)

image.png

第二个图也是一样的代码

代码语言:javascript
复制
fig1a.dat %>% 
  filter(Compartment=="Root") %>% 
  ggplot(aes(x=CAP1,y=CAP2))+
  geom_point(aes(fill=Treatment),
             shape=22,size=5,
             color="gray",
             alpha=0.8)+
  stat_ellipse(aes(group = Treatment), 
               size = 0.5, 
               level = 0.95,
               show.legend = F,
               lty="dashed")+
  labs(x="CAP1 (22.6%)",y="CAP2 (6.8%)")+
  theme_bw(base_size = 15)+
  theme(panel.grid = element_blank(),
        panel.border = element_blank(),
        axis.line = element_line())+
  annotate(geom="text",x=-0.2,y=0.3,
           label="Rhizosphere bacteria",
           size=5,hjust=0)+
  xlim(NA,1.1)+
  annotation_custom(tableGrob(mytable,rows = NULL,cols = NULL,
                              theme = ttheme_minimal(core=list(fg_params=list(hjust=0,
                                                                              parse=TRUE,
                                                                              x=0,
                                                                              fontsize=15)))),
                    xmin=1.05, xmax=1.1, ymin=0, ymax=0.3) -> plot.a2

组合图

代码语言:javascript
复制
library(patchwork)
pdf(file = "Rplot2.pdf",width = 18.8,height = 8)
plot.a1+plot.a2+
  plot_layout(guides="collect")& theme(legend.position = 'bottom')
dev.off()
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2024-03-22,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 论文
  • 部分示例数据
  • 加载需要用到的R包
  • 读取数据
  • 作图代码
  • 组合图
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档