zl程序教程

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

当前栏目

跟着Nature学作图:R语言ggplot2箱线图叠加蜂群图完整示例

2023-02-19 12:27:44 时间

论文

Graph pangenome captures missing heritability and empowers tomato breeding

https://www.nature.com/articles/s41586-022-04808-9#MOESM8

没有找到论文里的作图的代码,但是找到了部分做图数据,我们可以用论文中提供的原始数据模仿出论文中的图

今天的推文重复一下论文中的 Figure4b Figure4c 箱线图叠加蜂群图

Figure4b的部分数据截图

image.png

读取数据

library(readxl)

dat.fig4b<-read_excel("data/20220711/41586_2022_4808_MOESM8_ESM.xlsx",
                      sheet = "Fig4b",
                      skip = 1)
head(dat.fig4b)

作图代码

(ggplot2)
library(latex2exp)
library(ggbeeswarm)

segment.data<-data.frame(x=c(0.8,1.8,2.8),
                         xend=c(1.2,2.2,3.2),
                         y=c(73,97,83)+1,
                         yend=c(73,97,83)+1)


ggplot(data=dat.fig4b,aes(x=VarID,
                          y=`Standardized gene expression`,
                          color=Genotype))+
  geom_boxplot(show.legend = FALSE)+
  geom_beeswarm(dodge.width = 0.8,shape=21)+
  theme_bw()+
  theme(panel.grid = element_blank(),
        legend.position = c(0.1,0.95),
        legend.title = element_blank(),
        legend.background = element_rect(fill="transparent"),
        legend.key = element_rect(fill="transparent"))+
  scale_y_continuous(breaks = c(-50,0,50,100),
                     limits = c(-50,100))+
  scale_x_discrete(labels=paste0("SV",unique(dat.fig4b$VarID)))+
  labs(x=NULL)+
  annotate(geom = "text",x=0.8,y=70,label="(n=177)")+
  annotate(geom = "text",x=1.2,y=45,label="(n=9)")+
  annotate(geom = "text",x=1.8,y=60,label="(n=174)")+
  annotate(geom = "text",x=2.2,y=95,label="(n=14)")+
  annotate(geom = "text",x=2.8,y=60,label="(n=155)")+
  annotate(geom = "text",x=3.2,y=80,label="(n=134)")+
  geom_segment(data=segment.data,
               aes(x=x,xend=xend,y=y,yend=yend),
               inherit.aes = FALSE)+
  annotate(geom = "text",x=1,y=76,
           label=TeX(r"(\textit{P} = 0.76)"),vjust=0)+
  annotate(geom = "text",x=2,y=99.5,
           label=TeX(r"(\textit{P} = 8.37 \times 10${^-}{^3}$)"),vjust=0)+
  annotate(geom = "text",x=3,y=85.5,
           label=TeX(r"(\textit{P} = 6.84 \times 10${^-}{^8}$)"),vjust=0)

image.png

Figure4c数据的部分截图

image.png

读取数据

library(readxl)

dat.fig4c<-read_excel("data/20220711/41586_2022_4808_MOESM8_ESM.xlsx",
                      sheet = "Fig4c",
                      skip = 1)
head(dat.fig4c)

作图代码

dat.fig4c$Type<-factor(dat.fig4c$Type,
                       levels = c("non-favourable","favourable"))

dat.fig4c$Variation<-factor(dat.fig4c$Variation,
                       levels = c("SV2","SV4","SV2+SV4"))

x<-c(0.7,1.3,1.7,2.3,2.7,3.3)
y<-c(0.24,0.42,0.15,0.42,0.24,0.42)
label_z<-c(" = 174)"," = 14)"," = 155)"," = 34)"," = 178)"," = 10)")
ggplot(data = dat.fig4c,
       aes(x=Variation,y=BLUP))+
  geom_boxplot(aes(color=Type),show.legend = FALSE)+
  geom_beeswarm(aes(color=Type),
                dodge.width = 0.8,shape=21)+
  scale_color_manual(values = c("#648fff","#d36b1c"),
                     name="",
                     label=c("Non-favourable alleles",
                             "Favourable alleles"))+
  scale_x_discrete(label=c("SV2_44168216",
                           "SV4_54067283",
                           "Both"))+
  labs(x=NULL,y="BLUP of SSC")+
  theme_bw()+
  theme(panel.grid = element_blank(),
        legend.position = "bottom",
        legend.justification = c(0,0)) -> p2.1

for (i in 1:6){
  p2.1+
    annotate(geom = "text",x=x[i],y=y[i],label=TeX(r"((\textit{n})"),
             vjust=0,hjust=1) -> p2.1
}

for (i in 1:6){
  p2.1+
    annotate(geom = "text",x=x[i],y=y[i],label=label_z[i],
             vjust=0,hjust=0) -> p2.1
}
p2.1

image.png

最后是拼图

library(patchwork)
p1+p2.1+
  theme(legend.position = "none")

image.png

示例数据和代码可以自己到论文中获取