跟著Nature學作圖:R語言ggplot2箱線圖/堆積柱形圖完整示例

論文

Graph pangenome captures missing heritability and empowers tomato breeding

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

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

今天的推文重復一下論文中的 Extended Data Fig7b Extended Data Fig7c 箱線圖和堆積柱形圖

image.png

箱線圖下方的8個矩形可以用拼圖的方式來實現(xiàn)

箱線圖的部分示例數(shù)據(jù)

image.png

這里并沒有提供箱線圖的分組數(shù)據(jù),這里我自己隨便構(gòu)造一列分組數(shù)據(jù)了,所以最終結(jié)果可能和原圖不一致

整理數(shù)據(jù)

library(readxl)
dat01<-read_excel("data/20220711/41586_2022_4808_MOESM10_ESM.xlsx",
                  sheet = "Extend Fig7b",
                  skip = 1)
head(dat01)

library(tidyverse)

dat01 %>% 
  mutate(group_info=sample(c(rep("A",313),
                             rep("B",5),
                             rep("C",8),
                             rep("D",6)),
                           332,
                           replace = FALSE)) -> efig7c

箱線圖代碼

library(ggplot2)
library(latex2exp)
help(package="latex2exp")
dat<-data.frame(x=c(0.5,1:4),
                y=-Inf,
                label=c("n=",313,5,8,5))

ggplot(data=efig7c,
       aes(x=group_info,y=BLUP))+
  geom_boxplot(aes(fill=group_info))+
  scale_fill_manual(values = c("#feb2a9","#fdd79d",
                               "#dbcde4","#c993c7"))+
  geom_jitter(width = 0.4)+
  theme_bw()+
  theme(panel.grid = element_blank())+
  annotate(geom = "text",
           x=4,y=Inf,
           label=TeX(r"(Kruskal Wallis, \textit{P} = 5 \times 10${^-}$${^7}$)"),
           vjust=1,hjust=1)+
  geom_text(data=dat,aes(x=x,y=y,label=label),
            inherit.aes = FALSE,
            vjust=-0.8)+
  ylim(-40,NA)+
  labs(y="BLUP value of expression")+
  theme(axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        legend.position = "none") -> p1
p1
image.png

箱線圖下方的矩形點

dat.2<-data.frame(x=rep(LETTERS[1:4],2),
                  y=rep(c(1,2),each=4),
                  group=c("A","A","A","D","A","B","D","A"))
dat.2

ggplot(data=dat.2,aes(x=x,y=y,fill=group,color=group))+
  geom_point(shape=22,size=5)+
  scale_fill_manual(values = c("#fc8072","#a1d99b",
                               "#4192c6"),
                    labels=c("Reference homozygous",
                             "Heterozygous",
                             "Alternate homozygous"))+
  scale_color_manual(values = c("#fc8072","#a1d99b",
                               "#4192c6"),
                     labels=c("Reference homozygous",
                              "Heterozygous",
                              "Alternate homozygous"))+
  theme_void() +
  theme(legend.title = element_blank())+
  annotate(geom = "text",
           x=1,y=2,label="SV3_42936717",
           hjust=1.2,size=3,vjust=1)+
  annotate(geom = "text",
           x=1,y=1,label="SV3_42954617",
           hjust=1.2,size=3,vjust=0) -> p2
p2
image.png

將兩個圖組合到一起

library(ggpubr)
as_ggplot(get_legend(p2))
library(patchwork)

p1+
  annotation_custom(grob = get_legend(p2),
                    xmin=3.5,xmax=3.5,ymin=-28,ymax=-28)+
  p2+
  theme(legend.position ="none")+
  plot_layout(ncol = 1,heights = c(10,1)) -> p3
p3
image.png

最后是堆積柱形圖的代碼

數(shù)據(jù)集

image.png
dat02<-read_excel("data/20220711/41586_2022_4808_MOESM10_ESM.xlsx",
                  sheet = "Extend Fig7c")
dat02

dat02$x<-factor(dat02$x,
                levels = c("SNPs","Indels","SVs"))
dat02

dat02 %>% 
  group_by(x) %>% 
  mutate(new_col=cumsum(y)) -> dat02

ggplot(data=dat02,aes(x=x,y=y,fill=group))+
  geom_bar(stat="identity",
           position = "stack")+
  scale_fill_manual(values = c("Non-module"="#e99e9c",
                               "Module"="#98c0d7"))+
  geom_text(aes(x=x,y=new_col,label=y),
            vjust=1)+
  labs(x=NULL,y=TeX(r"(\textit{h}${^2}$)"))+
  theme_classic()+
  scale_y_continuous(expand = expansion(mult = c(0,0)))+
  theme(legend.position = c(0.2,0.8),
        legend.title = element_blank(),
        axis.title.y = element_text(angle = 0,vjust = 0.5)) -> p4
p4

image.png

最后是拼圖

p3+p4
ggarrange(p3,p4,ncol = 2)
image.png

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

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

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

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

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

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

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