zl程序教程

您现在的位置是:首页 >  其他

当前栏目

跟着Nature Microbiology学作图:R语言ggplot2做散点图添加拟合曲线和p值

2023-03-20 15:38:23 时间

本地文件 s41564-021-00997-7.pdf

论文

Protective role of the Arabidopsis leaf microbiota against a bacterial pathogen

image.png

今天的推文来重复一下论文中的figure3c 散点图添加拟合曲线

image.png

读取数据集

library(readxl)
df<-read_excel("41564_2021_997_MOESM10_ESM.xlsx")
head(df)
colnames(df)

最基本的散点图

library(ggplot2)
ggplot(data=df,aes(x=`mean Protection Score [a.u.]`,
                   y=`mean Colonization [log10(CFU/mg)]`))+
  geom_point(aes(color=Phylum))+
  ggsave(filename = "fig3c.pdf",
         width = 6,
         height = 4,
         family="serif")

添加拟合曲线

ggplot(data=df,aes(x=`mean Protection Score [a.u.]`,
                   y=`mean Colonization [log10(CFU/mg)]`))+
  geom_point(aes(color=Phylum))+
  geom_smooth(method = "lm",
              formula = "y~x",
              se=F,
              color="grey")+
  ggsave(filename = "fig3c.pdf",
         width = 6,
         height = 4,
         family="serif")

计算拟合方程的R和P值

df.lm<-lm(`mean Colonization [log10(CFU/mg)]`~
     `mean Protection Score [a.u.]`,
   data=df)
summary(df.lm)

sqrt(0.242)

ggplot(data=df,aes(x=`mean Protection Score [a.u.]`,
                   y=`mean Colonization [log10(CFU/mg)]`))+
  geom_point(aes(color=Phylum))+
  geom_smooth(method = "lm",
              formula = "y~x",
              se=F,
              color="grey")+
  annotate(geom = "text",
           x=60,y=1.2,
           label=expression(italic(R)~"="~0.49~","~italic(P)~"="~5.4%*%10^-15),
           parse=T)+
  ggsave(filename = "fig3c.pdf",
         width = 6,
         height = 4,
         family="serif")

image.png

添加虚线注释框

ggplot(data=df,aes(x=`mean Protection Score [a.u.]`,
                   y=`mean Colonization [log10(CFU/mg)]`))+
  geom_point(aes(color=Phylum))+
  geom_smooth(method = "lm",
              formula = "y~x",
              se=F,
              color="grey")+
  annotate(geom = "text",
           x=60,y=1.2,
           label=expression(italic(R)~"="~0.49~","~italic(P)~"="~5.4%*%10^-15),
           parse=T)+
  annotate(geom = "rect",
           xmin = 75,
           xmax = 100,
           ymin = 4.5,
           ymax = 7,
           alpha=0,
           color="black",
           lty="dashed")+
  ggsave(filename = "fig3c.pdf",
         width = 6,
         height = 4,
         family="serif")

image.png

最后是调节主题美化

colors<-c("#96d796","#aed75b","#599943",
          "#499ef1","#f18282","#ffdf33")
ggplot(data=df,aes(x=`mean Protection Score [a.u.]`,
                   y=`mean Colonization [log10(CFU/mg)]`))+
  geom_point(aes(fill=Phylum,
                 color=Phylum),
             shape=21,
             key_glyph="rect")+
  geom_smooth(method = "lm",
              formula = "y~x",
              se=F,
              color="grey")+
  annotate(geom = "text",
           x=60,y=1.2,
           label=expression(italic(R)~"="~0.49~","~italic(P)~"="~5.4%*%10^-15),
           parse=T)+
  annotate(geom = "rect",
           xmin = 75,
           xmax = 100,
           ymin = 4.5,
           ymax = 7,
           alpha=0,
           color="black",
           lty="dashed")+
  theme_bw()+
  theme(panel.grid = element_blank(),
        legend.title = element_blank())+
  scale_fill_manual(values = colors)+
  scale_color_manual(values = colors)+
  ggsave(filename = "fig3c.pdf",
         width = 9.4,
         height = 4,
         family="serif")

image.png