跟著Nature學(xué)作圖:R語言ggplot2分組折線圖完整實(shí)例

論文

Graph pangenome captures missing heritability and empowers tomato breeding

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

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

今天的推文重復(fù)一下論文中的Figure1c

image.png

今天主要的知識(shí)點(diǎn)是多個(gè)圖例的時(shí)候如何分開放,目前想到的辦法是使用ggpubr這個(gè)R包把圖例單獨(dú)挑出來,然后使用annotation_custom()函數(shù)再把圖例加回去。不知道有沒有更方便的辦法

部分示例數(shù)據(jù)截圖

image.png

讀取數(shù)據(jù)

dat01<-read.delim("data/20220719/Fig1c.txt",
                  sep = "\t",
                  header = TRUE,
                  check.names = FALSE)
dat01

轉(zhuǎn)換成作圖數(shù)據(jù)

library(tidyverse)
library(stringr)


#str_pad('1',2,side = "left",pad = "0")

dat01 %>% filter(`Reference genome`!="p value") %>% 
  mutate(variants=rep(rep(c("SNP","InDel","SV"),each=2),times=3)) %>% 
  pivot_longer(-c(`Reference genome`,variants)) %>% 
  mutate(name=as.numeric(str_replace(name,'x','')))  %>% 
  group_by(`Reference genome`,variants,name) %>% 
  summarise(mean_value=mean(value)) %>% 
  ungroup() -> new.data

最基本的圖

library(ggplot2)

ggplot(data=new.data,aes(x=name,y=mean_value))+
  geom_line(aes(color=variants,lty=`Reference genome`))+
  geom_point(aes(color=variants))
image.png

細(xì)節(jié)調(diào)整

ggplot(data=new.data,aes(x=name,y=mean_value))+
  geom_line(aes(color=variants,lty=`Reference genome`))+
  geom_point(aes(color=variants),size=5)+
  scale_color_manual(values = c("InDel"="#a4d6c1",
                                "SNP"="#b6e0f0",
                                "SV"="#ea6743"))+
  labs(y=TeX(r"(\textit{F}${_1}$ score)"),
       x="Sequencing depth")+
  theme_classic()+
  scale_y_continuous(limits = c(0.4,1),
                     breaks = c(0.4,0.6,0.8,1.0),
                     expand = expansion(mult = c(0.1,0)))
image.png

圖例位置

library(ggpubr)

ggplot(data=new.data,aes(x=name,y=mean_value))+
  geom_line(aes(color=variants,lty=`Reference genome`),
            show.legend = FALSE)+
  geom_point(aes(color=variants),size=5)+
  scale_color_manual(values = c("InDel"="#a4d6c1",
                                "SNP"="#b6e0f0",
                                "SV"="#ea6743"),
                     name="")+
  labs(y=TeX(r"(\textit{F}${_1}$ score)"),
       x="Sequencing depth")+
  theme_classic()+
  scale_y_continuous(limits = c(0.4,1),
                     breaks = c(0.4,0.6,0.8,1.0),
                     expand = expansion(mult = c(0.1,0))) -> p1

as_ggplot(get_legend(p1)) -> legend.01

ggplot(data=new.data,aes(x=name,y=mean_value))+
  geom_line(aes(color=variants,lty=`Reference genome`))+
  geom_point(aes(color=variants),size=5)+
  scale_color_manual(values = c("InDel"="#a4d6c1",
                                "SNP"="#b6e0f0",
                                "SV"="#ea6743"),
                     name="")+
  labs(y=TeX(r"(\textit{F}${_1}$ score)"),
       x="Sequencing depth")+
  theme_classic()+
  scale_y_continuous(limits = c(0.4,1),
                     breaks = c(0.4,0.6,0.8,1.0),
                     expand = expansion(mult = c(0.1,0)))+
  guides(color="none")+
  theme(legend.position = "top",
        legend.title = element_blank()) -> p2

as_ggplot(get_legend(p2)) -> legend.02


ggplot(data=new.data,aes(x=name,y=mean_value))+
  geom_line(aes(color=variants,lty=`Reference genome`))+
  geom_point(aes(color=variants),size=5)+
  scale_color_manual(values = c("InDel"="#a4d6c1",
                                "SNP"="#b6e0f0",
                                "SV"="#ea6743"))+
  labs(y=TeX(r"(\textit{F}${_1}$ score)"),
       x="Sequencing depth")+
  theme_classic()+
  scale_y_continuous(limits = c(0.4,1),
                     breaks = c(0.4,0.6,0.8,1.0),
                     expand = expansion(mult = c(0.1,0))) -> p
p

p + theme(plot.margin = unit(c(1,0.1,0.1,0.1),'cm'),
          legend.position = "none")+
  coord_cartesian(clip = "off")+
  annotation_custom(grob = ggplotGrob(legend.01),
                    xmin = 22,xmax = 22,
                    ymin=0.5,ymax = 0.5)+
  annotation_custom(grob = ggplotGrob(legend.02),
                    xmin = 15,xmax = 15,
                    ymin=1.05,ymax = 1.05)

最終結(jié)果

image.png

封面圖

library(patchwork)
pdf(file = "abc.pdf",
    width = 9.4,height = 4)
pp + pp
dev.off()
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ù)可視化的簡(jiǎn)單小例子;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),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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