跟着Nature学作图:R语言ggplot2分组折线图完整实例

论文

Graph pangenome captures missing heritability and empowers tomato breeding

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

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

今天的推文重复一下论文中的Figure1c

image.png

今天主要的知识点是多个图例的时候如何分开放,目前想到的办法是使用ggpubr这个R包把图例单独挑出来,然后使用annotation_custom()函数再把图例加回去。不知道有没有更方便的办法

部分示例数据截图

image.png

读取数据

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

转换成作图数据

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

细节调整

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)

最终结果

image.png

封面图

library(patchwork)
pdf(file = "abc.pdf",
    width = 9.4,height = 4)
pp + pp
dev.off()
image.png

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

欢迎大家关注我的公众号

小明的数据分析笔记本

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

你可能感兴趣的:(跟着Nature学作图:R语言ggplot2分组折线图完整实例)