跟著Nature學(xué)作圖: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

沒有找到論文里的作圖的代碼,但是找到了部分做圖數(shù)據(jù),我們可以用論文中提供的原始數(shù)據(jù)模仿出論文中的圖

今天的推文重復(fù)一下論文中的 Extended Data Fig. 4箱線圖和堆積柱形圖

image.png

Extended Data Fig. 4a的部分示例數(shù)據(jù)截圖

image.png

讀取數(shù)據(jù)并作圖

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的部分示例數(shù)據(jù)截圖

image.png

讀取數(shù)據(jù)并作圖

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的部分示例數(shù)據(jù)截圖

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

示例數(shù)據(jù)和代碼可以自己到論文中獲取,或者給本篇推文點(diǎn)贊,點(diǎn)擊在看,然后留言獲取

歡迎大家關(guān)注我的公眾號(hào)

小明的數(shù)據(jù)分析筆記本

小明的數(shù)據(jù)分析筆記本 公眾號(hào) 主要分享:1、R語言和python做數(shù)據(jù)分析和數(shù)據(jù)可視化的簡單小例子;2、園藝植物相關(guān)轉(zhuǎn)錄組學(xué)、基因組學(xué)、群體遺傳學(xué)文獻(xiàn)閱讀筆記;3、生物信息學(xué)入門學(xué)習(xí)資料及自己的學(xué)習(xí)筆記!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容