跟着Nature学作图:R语言ggplot2箱线图和堆积柱形图完整示例

论文

Graph pangenome captures missing heritability and empowers tomato breeding

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

s41586-022-04808-9.pdf

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

今天的推文重复一下论文中的 Extended Data Fig. 4箱线图和堆积柱形图

image.png

Extended Data Fig. 4a的部分示例数据截图

image.png

读取数据并作图

library(tidyverse)
extendedfig4a %>% 
  pivot_longer(-ID) %>% 
  mutate(group=name %>% 
           str_extract("linear|graph"),
         x=name %>% str_replace("linear.|graph.","")) -> new.ef4a

new.ef4a$group<-factor(new.ef4a$group,
                       levels = c("linear","graph"))
new.ef4a$x<-factor(new.ef4a$x,
                   levels = c("snps","indels","svs",
                              "snps_indels","snps_indels_svs"))
library(latex2exp)
library(ggplot2)

ggplot(data=new.ef4a,
       aes(x=x,y=value))+
  geom_boxplot(aes(fill=group),
               show.legend = FALSE)+
  scale_fill_manual(values = c("#c0d5e5","#edd2c4"))+
  scale_x_discrete(labels=c("SNPs","Indels","SVs",
                            "SNPs+Indels","SNPs+Indels+SVs"))+
  labs(x=NULL,y=TeX(r"(/textit{h}${^2}$)"))+
  theme_classic()+
  theme(axis.title.y = element_text(angle=0,vjust=0.5))
image.png

Extended Data Fig. 4b的部分示例数据截图

image.png

读取数据并作图

extendedfig4b<-read_excel("data/20220711/41586_2022_4808_MOESM9_ESM.xlsx",
                          sheet = "Extend Fig4b",
                          skip = 1)
head(extendedfig4b)
extendedfig4b %>% 
  pivot_longer(-ID) %>% 
  mutate(group=name %>% str_extract("linear|graph"),
         x=name %>% str_extract("overlapped|uniq")) -> new.ef4b

new.ef4b$group<-factor(new.ef4b$group,
                       levels = c("linear","graph"))

ggplot(data=new.ef4b,aes(x=x,y=value))+
  geom_boxplot(aes(fill=group),key_glyphs="rect")+
  scale_fill_manual(values = c("#c0d5e5","#edd2c4"),
                    labels=c("SL5.0-332","TGG1.1-332"))+
  labs(x=NULL,y=TeX(r"(/textit{h}${^2}$)"))+
  theme_classic()+
  theme(axis.title.y = element_text(angle=0,vjust=0.5),
        legend.position = "bottom",
        legend.direction = "vertical",
        legend.title = element_blank(),
        legend.justification = c(0,0))
image.png

Extended Data Fig. 4c的部分示例数据截图

image.png

作图代码

extendedfig4c<-read_excel("data/20220711/41586_2022_4808_MOESM9_ESM.xlsx",
                          sheet = "Extend Fig4c")
extendedfig4c$group<-factor(extendedfig4c$group,
                            levels = c("SNPs","Indels","SVs"))

stack.bar.label.position<-function(x){
  #x<-rev(x)
  new.x<-vector()
  
  for (i in 1:length(x)){
    if (i == 1){
      new.x<-append(new.x,x[i]/2)
    }
    
    else{
      new.x<-append(new.x,sum(x[1:i-1])+x[i]/2)
    }
  }
  return(new.x)
}

extendedfig4c %>% 
  group_by(x) %>% 
  summarise(y=stack.bar.label.position(value),
            y_label=value) %>% 
  ungroup() -> df.label

df.label

ggplot(data=extendedfig4c,
       aes(x=x,y=value))+
  geom_bar(stat = "identity",
           position = "stack",
           aes(fill=group))+
  scale_fill_manual(values = c("#8ea2cb",
                               "#a6d069",
                               "#ee8a6c"))+
  geom_text(data=df.label,
            aes(x=x,y=y,label=sprintf("%.2f",y_label)))+
  labs(x=NULL,y=TeX(r"(/textit{h}${^2}$)"))+
  theme_classic()+
  scale_y_continuous(expand=expansion(mult = c(0,0)),
                     limits = c(0,0.5),
                     breaks = c(0,0.25,0.5))+
  scale_x_discrete(labels=c("SL5.0-332","TGG1.1-332"))+
  theme(legend.position = "top",
        legend.title = element_blank(),
        axis.title.y = element_text(angle=0,vjust=0.5))

image.png

最后是拼图

library(ggpubr)

ggarrange(ggarrange(p1,labels = "a"),
          ggarrange(p2,p3,labels = c("b","c")),
          ncol = 1)

library(patchwork)
p1/(p2+theme(legend.position = "top",
             legend.direction = "horizontal")+p3)+
  plot_annotation(tag_levels = "a")
image.png

示例数据和代码可以自己到论文中获取,或者给本篇推文点赞,点击在看,然后留言获取

欢迎大家关注我的公众号

小明的数据分析笔记本

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

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

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